/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jt.tool.pdf;

import com.google.common.base.Splitter;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.util.PDFTextStripper;

import java.io.IOException;
import java.util.List;

/**
 * This is an example on how to add bookmarks to a PDF document.  It simply
 * adds 1 bookmark for every page.
 * Usage: java org.apache.pdfbox.examples.pdmodel.CreateBookmarks &lt;input-pdf&gt; &lt;output-pdf&gt;
 *
 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
 * @version $Revision: 1.2 $
 */
public class CreateBookmarks {
    private CreateBookmarks() {
        //utility class
    }

    static Splitter on = Splitter.on("\r\n").omitEmptyStrings();


    /**
     * This will print the documents data.
     *
     * @param args The command line arguments.
     * @exception Exception If there is an error parsing the document.
     */
    public static void main(String[] args) throws Exception {
        String fileName = "d:/pdf/go.pdf";
        String regex = "^\\d{1,2}\\.\\d{1,2}\\.\\s.*+";
        createBookmark(fileName, regex);
    }

    public static void createBookmark(String args, String reg) throws Exception {
        PDDocument document = null;
        try {
            document = PDDocument.load(args);
            if (document.isEncrypted()) {
                System.err.println("Error: Cannot add bookmarks to encrypted document.");
                System.exit(1);
            }
            PDDocumentOutline outline = new PDDocumentOutline();
            document.getDocumentCatalog().setDocumentOutline(outline);
            PDOutlineItem pagesOutline = new PDOutlineItem();
            pagesOutline.setTitle("All Pages");
            outline.appendChild(pagesOutline);
            List pages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < pages.size(); i++) {
                String pageText = getPageText(document, i+1, 0);
                String[] strings = matchTitle(pageText, reg);
                if (makeBookmark(strings)) {
                    PDPage page = (PDPage) pages.get(i);
                    PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
                    dest.setPage(page);
                    PDOutlineItem bookmark = new PDOutlineItem();
                    bookmark.setDestination(dest);
                    bookmark.setTitle(strings[0]);
                    pagesOutline.appendChild(bookmark);
                    System.out.println("add " + strings[0]);
                }
                continue;

            }
            pagesOutline.openNode();
            outline.openNode();
            document.save(args);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }

    public static String getPageText(PDDocument document, int start, int offset) throws Exception {
        PDFTextStripper stripper = new PDFTextStripper();
        stripper.setStartPage(start);
        stripper.setEndPage(start + offset);
        return stripper.getText(document);
    }

    public static String[] matchTitle(String text, String regex) {
        Iterable<String> split = on.split(text);
        final String[] s = {null};
        split.forEach(word -> {
            if (word.matches(regex)) {
                s[0] = word;
            }
        });
        return s;
    }

    public static boolean makeBookmark(String[] str) {
        if (str[0] != null) {
            return true;
        }
        return false;
    }

}

@@ -866,65 +866,8 @@
 er;%0A
-import org.apache.pdfbox.exceptions.COSVisitorException;%0A
 impo
@@ -1281,36 +1281,8 @@
 r;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -1373,598 +1373,329 @@
 ent.
-  It simply%0A * adds 1 bookmark for every page.%0A * Usage: java org.apache.pdfbox.examples.pdmodel.CreateBookmarks &lt;input-pdf&gt; &lt;output-pdf&gt;%0A *%0A * @author %3Ca href=%22mailto:ben@benlitchfield.com%22%3EBen Litchfield%3C/a%3E%0A * @version $Revision: 1.2 $%0A */%0Apublic class CreateBookmarks %7B%0A    private CreateBookmarks() %7B%0A        //utility class%0A    %7D%0A%0A    static Splitter on = Splitter.on(%22%5Cr%5Cn%22).omitEmptyStrings();%0A%0A%0A    /**%0A     * This will print the documents data.%0A     *%0A     * @param args The command line arguments.%0A     * @exception Exception If there is an error parsing the document
+%0A * 1. found your pdf title, and create regex pattern%0A * 2. input filePath and title regex pattern and run%0A *%0A * Attention!!! the process would remove the old bookmarks%0A */%0Apublic class CreateBookmarks %7B%0A%0A    static Splitter on = Splitter.on(%22%5Cr%5Cn%22).omitEmptyStrings();%0A%0A%0A    /**%0A     * This will print the documents data
 .%0A  
@@ -1754,32 +1754,52 @@
 ows Exception %7B%0A
+        //file path%0A
         String f
@@ -1825,16 +1825,39 @@
 o.pdf%22;%0A
+        //title regext%0A
         
@@ -2781,17 +2781,19 @@
 ument, i
-+
+ + 
 1, 0);%0A 
@@ -3388,35 +3388,8 @@
   %7D%0A
-                continue;%0A%0A
     
@@ -3613,32 +3613,66 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * parse text%0A     */%0A
     public stati
@@ -3938,32 +3938,74 @@
 cument);%0A    %7D%0A%0A
+    /**%0A     * match line by line%0A     */%0A
     public stati
@@ -4282,32 +4282,76 @@
 eturn s;%0A    %7D%0A%0A
+    /**%0A     * make bookmark or not%0A     */%0A
     public stati

change comment