// 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 enumerate and modify bookmarks
// in PDF document.

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.pdf.Bookmark;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.actions.Destination;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.pdf.Bookmark.e_PosLastChild;
import static com.foxit.sdk.pdf.Bookmark.e_StyleItalic;
import static com.foxit.sdk.pdf.PDFDoc.e_SaveFlagNoOriginal;

public class bookmark {


    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/bookmark/";
    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 showBookmarkInfo(Bookmark bookmark, FileWriter doc, int depth) throws PDFException, IOException {
        if (depth > 32)
            return;
        if (bookmark.isEmpty())
            return;
        for (int i = 0; i < depth; i++) {
            doc.write("\t");
        }
        // Show bookmark's title.
        doc.write(String.format("%s\t", bookmark.getTitle()));

        // Show bookmark's color.
        doc.write(String.format("color: %x\r\n", bookmark.getColor()));
        showBookmarkInfo(bookmark.getFirstChild(), doc, depth + 1);
        showBookmarkInfo(bookmark.getNextSibling(), doc, depth);
    }

    static void showBookmarksInfo(PDFDoc doc, String info_path) throws IOException, PDFException {
        FileWriter text_doc = new FileWriter(info_path, false);
        Bookmark root = doc.getRootBookmark();
        if (!root.isEmpty()) {
            showBookmarkInfo(root, text_doc, 0);
        } else {
            text_doc.write("No bookmark information!\r\n");
        }
        text_doc.close();
    }


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

        String input_file = input_path + "AboutFoxit.pdf";
        String output_file1 = output_path + "bookmark_add.pdf";
        String output_file2 = output_path + "bookmark_change.pdf";
        String bookmark_info_file = output_path + "bookmark_info.txt";

        try {
            PDFDoc doc = new PDFDoc(input_file);
            error_code = doc.load(null);
            if (error_code != e_ErrSuccess) {
                System.out.println(String.format("The Doc [%s] Error: %d\n", input_file, error_code));
                return;
            }

            // Show original bookmark information.
            showBookmarksInfo(doc, bookmark_info_file);

            // Get bookmark root node or Create new bookmark root node.
            Bookmark root = doc.getRootBookmark();
            if (root.isEmpty()) {
                root = doc.createRootBookmark();
            }
            for (int i = 0; i < doc.getPageCount(); i += 2) {
                Destination dest = Destination.createFitPage(doc, i);

                String ws_title = String.format("A bookmark to a page (index: %d)", i);
                Bookmark child = root.insert(ws_title,
                        e_PosLastChild);
                child.setDestination(dest);
                child.setColor(i * 0xF68C21);
            }
            doc.saveAs(output_file1, e_SaveFlagNoOriginal);

            // Get first bookmark and change properties.
            Bookmark first_bookmark = root.getFirstChild();
            first_bookmark.setStyle(e_StyleItalic);
            first_bookmark.setColor(0xFF0000);
            first_bookmark.setTitle("Change bookmark title, style, and color");

            // Remove next sibling bookmark
            if (!first_bookmark.getNextSibling().isEmpty()) {
                doc.removeBookmark(first_bookmark.getNextSibling());
            }

            bookmark_info_file = output_path + "bookmark_info1.txt";
            showBookmarksInfo(doc, bookmark_info_file);

            doc.saveAs(output_file2, e_SaveFlagNoOriginal);
            System.out.println("Bookmark demo.");


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

        }
        Library.release();
        return;
    }

}
