// Copyright (C) 2003-2026, Foxit Software Inc..
// All Rights Reserved.
//
// http://www.foxitsoftware.com
//
// The following code is copyrighted and contains proprietary information and trade secrets of Foxit Software Inc..
// You cannot distribute any part of Foxit PDF SDK to any third party or general public,
// unless there is a separate license agreement with Foxit Software Inc. which explicitly grants you such rights.
//
// This file contains an example to demonstrate how to use Foxit PDF SDK to convert images to PDF files.

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.fxcrt.PointF;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.common.fxcrt.FileReaderCallback;
import com.foxit.sdk.common.fxcrt.FileWriterCallback;
import com.foxit.sdk.addon.conversion.Convert;

import java.io.File;
import java.io.*;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.pdf.PDFDoc.e_SaveFlagNoOriginal;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;

public class image2pdf {
    private static String key = "8f3g1sFttW8NAgfSNv+BvaJWy4Pp8flcm7ncNDkdvCCO4JwUFbGLyZdujReMaiplbYJwwxam3lr7uHtQaEf0xTSu8j2ZBdrZ8FS4OQkEUjOI9UEGb3Ic0GqmlP9T9OnSItOYqoZZA/OnYg5EaVP+c/ha32nUquOk+5riEdhYgINAZDdgJkPc+YSWsrfixOIROf6t34j3eRcyFnOY0MS7tSXi1AEPE0VZdyu+5bfmBSGvLI+WR5lQ+oOQR5hRGwqf990WEdBelNO7dxhZELVhEZ+/L60PpDf5y9A8qmrY6PiEU8nhqIyLpks0BxeIsYyZ3Cr82jjfbItPGyEARsIVzluBSgVMD4oGjojz8T6wGDEXUbmZH/pra94XAs8wZ28mzbmIEppWnhBqX0c6Ov8iXCfFfUZr4KNuPxMEgk4VOYTMOd70GEjGnylivw18hbn7ZpkWh2Zql+uU2HejvOgn+WHvT0HF9NhK0Xb5vH8aFkgBP92PJx8obIbxBVeeExczxvsqWufuPdXS2sPNIgMYZ2TtvqVVsiUToeGB2FQo09fzZOSH5Vi18u/siyvC3FmjeMf4+OYWPuDPNCcrb21NgcHam7XI8AAoGLTGtLLWR+fxynbBBcg2d2ZPcZ5CMFIfOBHRWCkcA0sDT3cl9nsBppHNJze/jVxHy+lREeQEA/etSIgohTGAWW3ZlpibRXqMxlmVWEPifCubbWTzQ6U54mRMpW2UghH3gvCNFNgdday3Av7EtlIJOdUuiMq3zmmd5J4Jt4C82CKnVewA0DtZ4EEtwRG9IYuLrfpuZlnjBAtoeXRV1lqUZ6BcCe0E3m9jO28sGkALuIeJQ74XG7X8lMC7lMWWQYiYD7D6G9QT6X3qV4vXJwNKRKpYc/rI+amEawTSnDYAMEaYPwtZ0rb/7BL+atsZZaI+ncebdfVfjDJZbCZAc+JnkuNlUn2xrNRuObw8h/G0lm2pNOEJ10S8R4+yplo5B1QmC2dCDhaTaW67QJPqnJPKbVmjw7i6KGBi+CDP3JnAKlOX/YUu8uk56JfeQUuGcQg/KaBMGctnchH2n1i3+oGbibhurQj22+OO66Q6mijKoCRP/QJm5WTGcO4c0iVX0B8dIlai/uv/pGU8Wps9MxysH5ViVIj56LWKuNfy1FNO9+wx4iHYnUxfW0Iccq3YJYAlOBkMv8VZIJl9aTEEFJ626LJEw/UbEBdR9CdlwnylPf4RuNW/lcGYMgq3xNVpp/bzYKwZivvQOFgMdX1tv8D33SHvAm3zrRCU+v94BJXnBi9k25fnKsc705IMwv+I6bueXA+mDnagj1oTqsWXZ0WnPAYI3WUO7sBf7XUH230wfYXnTSVhujJxiEP/2oWqi2813gtQkKwz9N2p6yT8Vsh3+BaAwwDvtOZMJKUFcabTBSL6NoouzmzwItJ4mIboT6KXaef66rs8jCU=";
    private static String sn = "Rew1vB0TqoMIxX444UcpScARWdhgJpsJfzmyMLR32a0+EgrCjp2NaQ==";
    private static String output_path = "../output_files/image2pdf/";
    private static String input_path = "../input_files/";

    // You can also use System.load("filename") instead. The filename argument must be an absolute path name.
    static {
      String os = System.getProperty("os.name").toLowerCase();
      String lib = "fsdk_java_";
      if (os.startsWith("win")) {
          lib += "win";
      } else if (os.startsWith("mac")) {
          lib += "mac";
      } else {
          lib += "linux";
      }
        
      if (System.getProperty("sun.arch.data.model").equals("64")) {
        if(System.getProperty("os.arch").equals("aarch64")){
          if(os.startsWith("mac")){
             lib += "arm";
           }
           else{
             lib += "armv8";
           }
         }
         else{
            lib += "64";
         }
      } else {
         if(System.getProperty("os.arch").equals("arm")){
            lib += "armv7";
         }
         else{
            lib += "32";
         }
      }
      System.loadLibrary(lib);
    }

    private static void createResultFolder(String output_path) {
        File myPath = new File(output_path);
        if (!myPath.exists()) {
            myPath.mkdir();
        }
    }


    static void image2PDF(String input_file, String output_file) throws PDFException {
        Image image = new Image(input_file);
        int count = 1;
        if (image.getType() != Image.e_JBIG2) {
            count = image.getFrameCount();
        }

        PDFDoc doc = new PDFDoc();
        for (int i = 0; i < count; i++) {
            PDFPage page = doc.insertPage(i, image.getWidth(), image.getHeight());
            page.startParse(e_ParsePageNormal, null, false);
            // Add image to page
            page.addImage(image, i, new PointF(0, 0), image.getWidth(), image.getHeight(), true);
        }

        doc.saveAs(output_file, e_SaveFlagNoOriginal);

    }

    public static void main(String[] args) throws PDFException {
        // Initialize library
        int error_code = Library.initialize(sn, key);
        if (error_code != e_ErrSuccess) {
            System.out.println("Library Initialize Error: " + error_code);
            return;
        }
        createResultFolder(output_path);

        try {
            {
                // Convert .bmp file to PDF document.
                String input_file = input_path + "watermark.bmp";
                String output_file = output_path + "watermark_bmp.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert BMP file to PDF file.");
            }
            {
                // Convert .jpg file to PDF document.
                String input_file = input_path + "image_samples.jpg";
                String output_file = output_path + "image_samples_jpg.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert JPG file to PDF file.");
            }
            {
                // Convert .tif file to PDF document.
                String input_file = input_path + "TIF2Pages.tif";
                String output_file = output_path + "TIF2Pages_tif.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert TIF file to PDF file.");
            }
            {
                // Convert .gif file to PDF document.
                String input_file = input_path + "image005.gif";
                String output_file = output_path + "image005_gif.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert GIF file to PDF file.");
            }
            {
                // Convert .png file to PDF document.
                String input_file = input_path + "1.png";
                String output_file = output_path + "1_png.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert PNG file to PDF file.");
            }
            {
                // Convert .jb2 file to PDF document.
                String input_file = input_path + "STR_039.jb2";
                String output_file = output_path + "STR_039_jb2.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert JB2 file to PDF file.");
            }
            {
                // Convert .jp2 file to PDF document.
                String input_file = input_path + "1.jp2";
                String output_file = output_path + "1_jp2.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert JP2 file to PDF file.");
            }
            {
                // Convert .jpx file to PDF document.
                String input_file = input_path + "1.jpx";
                String output_file = output_path + "1_jpx.pdf";
                image2PDF(input_file, output_file);
                System.out.println("Convert JPX file to PDF file.");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        System.out.println("Start: convert file from file path.");
        try {
            {
                // Convert .bmp file to PDF document.
                String input_file = input_path + "watermark.bmp";
                String output_file = output_path + "watermark_bmp_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert BMP file to PDF file.");
            }
            {
                // Convert .jpg file to PDF document.
                String input_file = input_path + "image_samples.jpg";
                String output_file = output_path + "image_samples_jpg_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert JPG file to PDF file.");
            }
            {
                // Convert .tif file to PDF document.
                String input_file = input_path + "TIF2Pages.tif";
                String output_file = output_path + "TIF2Pages_tif_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert TIF file to PDF file.");
            }
            {
                // Convert .gif file to PDF document.
                String input_file = input_path + "image005.gif";
                String output_file = output_path + "image005_gif_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert GIF file to PDF file.");
            }
            {
                // Convert .png file to PDF document.
                String input_file = input_path + "1.png";
                String output_file = output_path + "1_png_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert PNG file to PDF file.");
            }
            {
                // Convert .jb2 file to PDF document.
                String input_file = input_path + "STR_039.jb2";
                String output_file = output_path + "STR_039_jb2_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert JB2 file to PDF file.");
            }
            {
                // Convert .jp2 file to PDF document.
                String input_file = input_path + "1.jp2";
                String output_file = output_path + "1_jp2_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert JP2 file to PDF file.");
            }
            {
                // Convert .jpx file to PDF document.
                String input_file = input_path + "1.jpx";
                String output_file = output_path + "1_jpx_frompath.pdf";
                Convert.fromImage(input_file, output_file);
                System.out.println("Convert JPX file to PDF file.");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        System.out.println("End: convert file from file path.");

        System.out.println("Start: convert file from stream.");
        try {
            {
                // Convert .bmp file to PDF document.
                String input_file = input_path + "watermark.bmp";
                String output_file = output_path + "watermark_bmp_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert BMP file to PDF file.");
            }
            {
                // Convert .jpg file to PDF document.
                String input_file = input_path + "image_samples.jpg";
                String output_file = output_path + "image_samples_jpg_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert JPG file to PDF file.");
            }
            {
                // Convert .tif file to PDF document.
                String input_file = input_path + "TIF2Pages.tif";
                String output_file = output_path + "TIF2Pages_tif_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert TIF file to PDF file.");
            }
            {
                // Convert .gif file to PDF document.

                String input_file = input_path + "image005.gif";
                String output_file = output_path + "image005_gif_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert GIF file to PDF file.");
            }
            {
                // Convert .png file to PDF document.

                String input_file = input_path + "1.png";
                String output_file = output_path + "1_png_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert PNG file to PDF file.");
            }
            {
                // Convert .jb2 file to PDF document.
                String input_file = input_path + "STR_039.jb2";
                String output_file = output_path + "STR_039_jb2_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert JB2 file to PDF file.");
            }
            {
                // Convert .jp2 file to PDF document.
                String input_file = input_path + "1.jp2";
                String output_file = output_path + "1_jp2_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert JP2 file to PDF file.");
            }
            {
                // Convert .jpx file to PDF document.
                String input_file = input_path + "1.jpx";
                String output_file = output_path + "1_jpx_stream.pdf";
                FileRead filereader = new FileRead();
                filereader.loadFile(input_file,false);
                Filewriter filewriter = new Filewriter();
                filewriter.loadFile(output_file);
                Convert.fromImage(filereader, filewriter);
                System.out.println("Convert JPX file to PDF file.");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        System.out.println("End: convert file from  stream.");

        Library.release();
    }
}


class FileRead extends FileReaderCallback {
    private RandomAccessFile file_ = null;
    private boolean is_large_file_ = false;

    FileRead() {

    }

    public boolean loadFile(String file_path, boolean is_large_file) throws FileNotFoundException {
        this.is_large_file_ = is_large_file;
        file_ = new RandomAccessFile(file_path, "r");
        return true;

    }

    @Override
    public long getSize() {
        try {
            return this.file_.length();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return 0;
    }

    @Override
    public boolean readBlock(byte[] buffer, long offset, long size) {
      try {
            file_.seek(offset);
            int read = file_.read(buffer, 0, (int) size);
            return read == size ? true : false;
        } catch (IOException e) {
            e.printStackTrace();
        }
      return false;
    }

    @Override
    public void release() {
        try {
             this.file_.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

class Filewriter extends FileWriterCallback {
    private RandomAccessFile file_ = null;

    boolean loadFile(String file_path) throws FileNotFoundException {
        File path_file = new File(file_path);
        if(path_file.exists() == true) path_file.delete();
        file_ = new RandomAccessFile(file_path, "rw");
        return true;
    }

    @Override
    public long getSize() {
        try {
            return this.file_.length();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public boolean writeBlock(byte[] buffer, long offset, long size) {
        try {
            file_.seek(offset);
            file_.write(buffer, 0, (int) size);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean writeBlock(byte[] buffer, long size) {
        return writeBlock(buffer, getSize(), size);
    }

    @Override
    public boolean flush() {
        return true;
    }
    
    public void release() {
        try {
            this.file_.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}