// 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 implement the conversion between OFD
// file and PDF file and to render OFD page.

import java.io.File;

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.addon.conversion.OFDConvertParam;
import com.foxit.sdk.addon.conversion.Convert;
import com.foxit.sdk.addon.ofd.*;
import com.foxit.sdk.common.Progressive;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.common.fxcrt.RectI;

import static com.foxit.sdk.common.Constants.*;

public class ofd {
	static String key = "8f3g1sFttW8NAgfSNv+BvQJm2vDtTY+wGQ/1PcmGtqKTwvvI8mLE4KvuHocWH+XZnP3U+Vh1BnqpgQIjO0jwRz7evDdlijTdutpBEsozaXEvFpLbixmsSGvtznflVsuEmsu5ytLwVJiTap2VcCiiSbyuvQwQu88I8BxJ+XXv3LPXN5v/+00zDrZ+V5yLefi+bk7JX5FRE2jfSnOJIqEaVkHOpCIjwf9aszl01YoY7x0EAehg0ILal+v2QLLo7HnkAVt/TA31rddZexhq/2hwAwfy4OfvkJPnESppD7D+SAsX5CfXoU55P1vmyRe+lKBM6pOcarUbuaN4Vjj62SD0sTDbJZ2Ypyp+kcIxZ9wcD+Mg8aSimr5+hg8wZJT0u0mIAdZhTJ++A9sq9OmVAfL2QQ6bh2ISyKPGJPv29u738bAokRjqzZy9bi30mYfGLEwKChEd2R2NBDKZTDOVxFwpzD1IbF0/wN/DDbMO2gS0xUvgSbJyuXfA3ALtmoUH9I9iScaBVon5oYRRy6OTnFMgDgVM+nFTzWbaGKZfO+PMBnreaQ/Ezr9wH7YKJ+LrM4r6xVQjcHDUyx4dlVLe3zQlDRKGGChhyvymVDBuZR8B0/M9Y/DKr1sQAzp/W1Wsz+LZb+JUIxLBrJzgRLtOliuLF+JOTy036W0shWcXYYc2bwLdPWP/BXygmnopzUTCtuo/QLVTRNdENFqciYYvSHEZxgV1sUFpuBqIMUEigWR/2gMlMDiTVq0udEqn8g9bkw1SImVy50v2gwEbUHkEa243BP/3/Dae0YhH03NDUlTSVBKX6w1UVQK6/7aDL3A4EEg47YmKgh6NsABzRsgSvLBhBQNqzOoUvnKaf7YMlw0ns06BdFswP6DaY1KmuRR2Xn6MtDTshGNtT9+azHPqaAWaZs1rHz3fGYb5pfJHptJlx+r9609bY+ZhCERLYG+0lTNWCFnDxdI8Vj1BG4iGWaDBkIC5qdJI3qDLVDABdtIk43fMhgfr+TUqds+G6KBDapS9p32nKZc6lBqvlNN12Pv1AJCC1rwfECzmNQi3MXogQCxzi5pHAgd/BGyGwo4RWhFrKFN869H0whvDVC8NJM2upFrEEJET4fecQyMLWHuTCWvjRZclLC/GGOj9PAXHOcEdFkq4UkloMulXLGsQzkzHB3Elvd2NnxZ2+3FK2e0xNx+rBMru/i/HHLG6WZvz6Jz7mjmibNfBExydtEI5lWUZ1Wsy0r17kyvlME9Bot7XXPkx/+cKTcxfNWSb3kaPROYIgSpGjrXxfrphBnr1mioJPq3WrtP9E5lZBeMd8jxXT4NlX3T0frsci6Xy1ChB/BytU7pvn6bMfddTnEDd/QpkW+6WYOnHHrIYVk2ngKjUBl5nGOkp37wpgUpWp5tcICjpbjfNdY43VfTyHRf4dGRR";
	static String sn = "nkyvo37UxrKdu5Peq294Eb+g2ihRldgPQEP6MFtAM4LwtxXXJ+T7MQ==";
	static String output_path = "../output_files/ofd/";
	static String input_path = "../input_files/ofd/";
	static String src_ofd_path = input_path + "wm_txttiled.ofd";
	static String src_pdf_path = input_path + "test.pdf";
	static String render_file_path = input_path + "content_flag.ofd";

    // 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")){
             lib += "armv8";
         }
         else{
            lib += "64";
         }
      } else {
         if(System.getProperty("os.arch").equals("arm")){
            lib += "armv7";
         }
         else{
            lib += "32";
         }
      }
	  System.loadLibrary(lib);
	}

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

		// "engine_path" is the path of ofd engine file. Please refer to Developer Guide for more details.
		String engine_path = "";

		if (engine_path == "") {
			System.out.println("engine_path is still empty. Please set it with a valid path to OFD engine path.");
			Library.release();
			return;
		}

		// Initialize OFD engine.
		error_code = Library.initializeOFDEngine(engine_path);
		if (error_code != e_ErrSuccess) {
			switch(error_code){
				case e_ErrInvalidLicense:
					System.out.println("[Failed] OFD module is not contained in current Foxit PDF SDK keys.");
				break;
				default:
					System.out.println("Fail to initialize OFD engine. Error: " + error_code);
				break;
			}
			Library.release();
			return;
		}
		System.out.println("OFD Engine is initialized.");

		// Convert PDF document to OFD document, and convert OFD document to PDF document.
		{
			OFDConvertParam convert_param = new OFDConvertParam(false);
			// Convert OFD document to PDF document.
			Convert.fromOFD(src_ofd_path, "", output_path + "ofd2pdf.pdf", convert_param);
			// Convert PDF document to OFD document.
			Convert.toOFD(src_pdf_path, "", output_path + "pdf2ofd.ofd", convert_param);
		}
		// Render OFD document to bitmap.
		{
			OFDDoc doc = new OFDDoc(render_file_path, "");
			OFDPage ofd_page = doc.getPage(0);
			// Get the size of the page.
			float w = ofd_page.getWidth();
			float h = ofd_page.getHeight();
			Bitmap bitmap = new Bitmap((int)w, (int)h, Bitmap.e_DIBArgb, null, 0);
			RectI fRect = new RectI(0, (int)h, (int)w, 0);
			bitmap.fillRect(0xFFFFFFFF, fRect);
			// Get the display matrix of the page.
			Matrix2D matrix_1 = ofd_page.getDisplayMatrix(0, 0, (int)w, (int)h, e_Rotation0);
			OFDRenderer ofd_render = new OFDRenderer(bitmap);

			Progressive progressive = ofd_render.startRender(ofd_page, matrix_1);
			String sSaveFilePath = output_path + "renderBitmap.bmp";
			// Add the bitmap to image and save the image.
			Image image = new Image();
			image.addFrame(bitmap);
			image.saveAs(sSaveFilePath);
			
			ofd_page.release();
			doc.release();
		}

		// Release OFD engine and PDF library
		Library.releaseOFDEngine();
		Library.release();
		System.out.println("END: OFD demo.");
	}
	
	private static void createResultFolder(String output_path) {
		File myPath = new File(output_path);
		if (!myPath.exists()) {
			myPath.mkdir();
		}
	}
}
