// 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.

using System;

using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.addon.conversion;
using System.IO;
using System.Runtime.InteropServices;

namespace image2pdfCS
{
  public class FileRead : FileReaderCallback
  {
    private FileStream file_ = null;
    private bool is_large_file_;

    public FileRead()
    {
    }

    public bool loadFile(String file_path, bool is_large_file)
    {
      this.is_large_file_ = is_large_file;
      this.file_ = new FileStream(file_path, FileMode.Open);
      return true;

    }

    public override long GetSize()
    {
      file_.Seek(0, SeekOrigin.End);
      long len = file_.Length;
      file_.Seek(0, SeekOrigin.Begin);
      return len;
    }

    public override bool ReadBlock(IntPtr buffer, long offset, uint size)
    {
      int read_size = 0;
      file_.Seek(offset, SeekOrigin.Begin);
      byte[] array = new byte[size + 1];
      read_size = file_.Read(array, 0, (int)size);

      Marshal.Copy(array, 0, buffer, (int)size);
      return read_size == size ? true : false;

    }

    public override void Release()
    {
      try
      {
        file_.Close();
      }
      catch (IOException e)
      {
        Console.WriteLine(e.Message);
      }
    }
  }

  class Filewriter : FileWriterCallback
  {
    private FileStream file_ = null;

    public Boolean loadFile(String file_path)
    {
      file_ = new FileStream(file_path, FileMode.Create);
      return true;
    }

    public override void Release()
    {
      this.file_.Close();
    }

    public override bool Flush()
    {
      return true;
    }

    public override long GetSize()
    {
      return this.file_.Length;
    }

    public override bool WriteBlock(IntPtr pData, long offset, uint size)
    {
      file_.Seek(offset, SeekOrigin.Begin);

      byte[] ys = new byte[size];
      Marshal.Copy(pData, ys, 0, (int)size);
      file_.Write(ys, (int)0, (int)size);
      return true;
    }
  }
  class image2pdf
    {
        public static void Image2PDF(string input_file, string output_file)
        {
            using (Image image = new Image(input_file))
            {
                int count = 1;
                if (image.GetType() != Image.Type.e_JBIG2)
                {
                    count = image.GetFrameCount();
                }
                using (PDFDoc doc = new PDFDoc())
                {
                    for (int i = 0; i < count; i++)
                    {
                        using (PDFPage page = doc.InsertPage(i, image.GetWidth(), image.GetHeight()))
                        {
                            page.StartParse((int)PDFPage.ParseFlags.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, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
                }
            }
        }
        static void Main(String[] args)
        {
            string input_path ="../input_files/";
            string output_path = "../output_files/image2pdf/";
            System.IO.Directory.CreateDirectory(output_path);
            string sn = "Rew1vB0TqoMIxX444UcpScARWdhgJpsJfzmyMLR32a0+EgrCjp2NaQ==";
            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=";
            // Initialize library
            ErrorCode error_code = Library.Initialize(sn, key);
            if (error_code != ErrorCode.e_ErrSuccess)
            {
                Console.WriteLine("Library Initialize Error: {0}\n", error_code);
                return;
            }
            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);
                    Console.WriteLine("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);
                    Console.WriteLine("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);
                    Console.WriteLine("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);
                    Console.WriteLine("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);
                    Console.WriteLine("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);
                    Console.WriteLine("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);
                    Console.WriteLine("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);
                    Console.WriteLine("Convert JPX file to PDF file.");
                }
            } catch (foxit.PDFException e) {
              Console.WriteLine( e.Message);
           }
            Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("Convert PNG 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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("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";
                foxit.addon.conversion.Convert.FromImage(input_file, output_file);
                Console.WriteLine("Convert JPX file to PDF file.");
              }

            }
            catch (foxit.PDFException e)
            {
              Console.WriteLine(e.Message);
            }
            Console.WriteLine("End: convert file from file path.");

            Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("Convert PNG 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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("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);
                foxit.addon.conversion.Convert.FromImage(filereader, filewriter);
                Console.WriteLine("Convert JPX file to PDF file.");
              }

            }
            catch (foxit.PDFException e)
            {
              Console.WriteLine(e.Message);
            }
      Console.WriteLine("End: convert file from  stream.");

      Library.Release();
        }
    }
}
