// 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 export data, import data and reset form for
// XFA document.

import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.WStringArray;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.addon.xfa.*;
import com.foxit.sdk.addon.xfa.XFADoc.*;
import com.foxit.sdk.common.fxcrt.*;
import com.foxit.sdk.common.WStringArray;
import com.foxit.sdk.PDFException;
import java.io.File;

public class xfa_form {

    private static String output_path = "../output_files/xfa_form/";
    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();
		}
	}

	public static void main(String[] args) throws PDFException {
		String key = "8f3g1sFttW8NAgfSNv+BvQJm2vDtTY+wGQ/1PcmGtqKTwvvI8mLE4KvuHocWH+XZnP3U+Vh1BnqpgQIjO0jwRz7evDdl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elvd2NnxZ2+3FK2e0xNx+rBMru/i/HHLG6WZvz6Jz7mjmibNfBExydtEI5lWUZ1Wsy0r17kyvlME9Bot7XXPkx/+cKTcxfNWSb3kaPROYIgSpGjrXxfrphBnr1mioJPq3WrtP9E5lZBeMd8jxXT4NlX3T0frsci6Xy1ChB/BytU7pvn6bMfddTnEDd/QpkW+6WYOnHHrIYVk2ngKjUBl5nGOkp37wpgUpWp5tcICjpbjfNdY43VfTyHRf4dGRR";
		String sn = "nkyvo37UxrKdu5Peq294Eb+g2ihRldgPQEP6MFtAM4LwtxXXJ+T7MQ==";
		createResultFolder(output_path);
		// Initialize library
		int error_code = Library.initialize(sn, key);
		
		if (error_code != Constants.e_ErrSuccess) {
			System.out.printf("Library Initialize Error: %d\n", error_code);
			return;
		}

		try {
			XFAAppHandler xfa_app = new XFAAppHandler();
			Library.registerXFAAppProviderCallback(xfa_app);
			String input_file = input_path + "xfa_dynamic.pdf";
			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;
			}
            XFADocHandler xfa_dochandler = new XFADocHandler();
			XFADoc xfa_doc = new XFADoc(doc, xfa_dochandler);
			xfa_doc.startLoad(null);
			String output_xml = output_path + "xfa_form.xml";
			xfa_doc.exportData(output_xml, 0);

			xfa_doc.resetForm();
			doc.saveAs(output_path + "xfa_dynamic_resetform.pdf", PDFDoc.e_SaveFlagNormal);

			xfa_doc.importData(output_xml);
			doc.saveAs(output_path + "xfa_dynamic_importdata.pdf", PDFDoc.e_SaveFlagNormal);
			
			System.out.println("Xfa test.");
		} catch (PDFException e) {
			System.out.println(e.getMessage());
			return;
		}
        Library.release();
	}

}

class XFAAppHandler extends AppProviderCallback
{
  	@Override
	public void release(){ }
	
	@Override 
	public String  getAppInfo(int  app_info){ return "Foxit SDK"; }
	
	@Override 
	public void beep(int type){}
	
	@Override 
	public int msgBox(String message, String title, int icon_type, int button_type) { return 0;}
	
	@Override 
	public String  response(String question, String title, String default_answer, boolean is_mask){	return "answer"; }
	
	@Override 
	public FileReaderCallback  downLoadUrl (String url){	return null;}
		
	@Override 
	public String postRequestURL(String url, String data, String content_type, String encode, String header) { 	return "PostRequestUrl"; }
	
	@Override 
	public boolean putRequestURL(String url, String data, String encode){ 	return true; }
	
	@Override 
	public String  loadString(int string_id){ 	return "LoadString"; }
	
	@Override 
	public WStringArray showFileDialog(String string_title, String string_filter, boolean is_openfile_dialog){ return new WStringArray(); }
	
};

class XFADocHandler extends  DocProviderCallback
{
	@Override
	public void release(){ };

	@Override
	public void invalidateRect(int page_index, RectF rect, int flag) {};

	@Override
	public void displayCaret(int page_index, boolean is_visible, RectF rect){};
	
	@Override	
	public boolean getPopupPos (int page_index, float min_popup, float max_popup, RectF rect_widget, RectF inout_rect_popup){return false;};
	
	@Override	
	public boolean popupMenu(int page_index, PointF rect_popup){return false;};
	
	@Override	
	public int getCurrentPage(XFADoc doc){return 0;};
	
	@Override	
	public void setCurrentPage(XFADoc doc, int current_page_index){};
	
	@Override	
	public String getTitle(XFADoc doc){return "";};
	
	@Override	
	public void exportData(XFADoc doc, String file_path){};

	@Override	
	public void importData(XFADoc doc, String file_path){};

	@Override	
	public  void gotoURL(XFADoc doc, String url){};
	
	@Override	
	public  void print(XFADoc doc, int start_page_index, int end_page_index, int options){}

	@Override	
	public  long getHighlightColor(XFADoc doc){return 0;}
	
	@Override	
	public boolean submitData (XFADoc doc, String target, int format, int text_encoding, String content){return false;}
	
	@Override	
	public  void setFocus(XFAWidget xfa_widget, boolean is_relyaout){};
	
	@Override	
	public  void pageViewEvent (int page_index, int page_view_event_type) {};

	@Override
	public void setChangeMark(XFADoc doc) {};

	@Override
	public void widgetEvent(XFAWidget xfa_widget, int widget_event_type) {};
};
