// 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 add various annotations to PDF document.

import java.io.File;

import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.common.Constants.e_ErrInvalidLicense;

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.addon.ocr.OCREngine;
import com.foxit.sdk.addon.ocr.OCR;

import static com.foxit.sdk.pdf.PDFDoc.e_SaveFlagNoOriginal;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;

public class ocr {
	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/ocr/";
	static String input_path = "../input_files/ocr/";
	static String input_file = input_path + "AboutFoxit_ocr.pdf";

    // 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 += "arm";
	      }
	      else{
	          lib += "64";
	      }
	  } 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;
		}

		// "ocr_resource_path" is the path of ocr resources. Please refer to Developer Guide for more details.
		String ocr_resource_path = "";

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

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

		// Set log for OCREngine. (This can be opened to set log file if necessary)
		// OCREngine.setLogFile(output_path+"ocr.log");
		// Set languages.
		OCREngine.setLanguages("English");

		// OCR PDF document, only for search, not editable.
		{
			PDFDoc doc = new PDFDoc(input_file);
			error_code = doc.load(null);
			if (error_code != e_ErrSuccess) {
				System.out.println("The Doc " + input_file + " Error: " + error_code);
				return;
			}
			OCR ocr_operator = new OCR();
			ocr_operator.oCRPDFDocument(doc, false, null);

			String save_pdf_path = output_path + "ocr_doc_searchable.pdf";
			doc.saveAs(save_pdf_path, e_SaveFlagNoOriginal);
		}

		// OCR PDF document, editable.
		{
			PDFDoc doc = new PDFDoc(input_file);
			error_code = doc.load(null);
			if (error_code != e_ErrSuccess) {
				System.out.println("The Doc " + input_file + " Error: " + error_code);
				return;
			}
			OCR ocr_operator = new OCR();
			ocr_operator.oCRPDFDocument(doc, true, null);

			String save_pdf_path = output_path + "ocr_doc_editable.pdf";
			doc.saveAs(save_pdf_path, e_SaveFlagNoOriginal);
		}
		
		// OCR PDF page, only for search, not editable.
		{
			PDFDoc doc = new PDFDoc(input_file);
			error_code = doc.load(null);
			if (error_code != e_ErrSuccess) {
				System.out.println("The Doc " + input_file + " Error: " + error_code);
				return;
			}
			
			PDFPage page = doc.getPage(0);
			page.startParse(e_ParsePageNormal, null, false);
			
			OCR ocr_operator = new OCR();
			ocr_operator.oCRPDFPage(page, false, null);

			String save_pdf_path = output_path + "ocr_page_searchable.pdf";
			doc.saveAs(save_pdf_path, e_SaveFlagNoOriginal);
		}
		
		// OCR PDF page, editable.
		{
			PDFDoc doc = new PDFDoc(input_file);
			error_code = doc.load(null);
			if (error_code != e_ErrSuccess) {
				System.out.println("The Doc " + input_file + " Error: " + error_code);
				return;
			}
			
			PDFPage page = doc.getPage(0);
			page.startParse(e_ParsePageNormal, null, false);
			
			OCR ocr_operator = new OCR();
			ocr_operator.oCRPDFPage(page, true, null);

			String save_pdf_path = output_path + "ocr_page_editable.pdf";
			doc.saveAs(save_pdf_path, e_SaveFlagNoOriginal);
		}

		System.out.println("END: OCR demo.");

		// Release OCR engine and PDF library
		OCREngine.release();
		Library.release();
	}
	
	private static void createResultFolder(String output_path) {
		File myPath = new File(output_path);
		if (!myPath.exists()) {
			myPath.mkdir();
		}
	}
}
