Search This Blog

2013-05-19

用pdfbox给pdf文件生成书签

以前稍微看过jcr规范,几百页的文档竟然没有书签,所以把前面几页目录拷贝出来,用pdfbox生成了书签。如果有别的pdf文档需要生成书签,那稍作改写即可使用。
代码是用官方例子修改而成,几乎没改什么。下面的代码是从以前的测试工程拿出来的。
Pre[-]
package pdfbox;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDPage;
import org.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;

public class AddBookmarks {
    public static void main(String[] args) throws Exception {
        test2();
    }

    private static void test2() throws Exception {
        PDDocument document = null;
        final String inputPath = "D:/download/jsr-283-fcs/spec/jcr-spec.pdf", outputPath = "D:/download/jsr-283-fcs/spec/jcr-spec_2.pdf";
        try {
            document = PDDocument.load(inputPath);
            if (document.isEncrypted()) {
                System.err.println("Error: Cannot add bookmarks to encrypted document.");
                System.exit(1);
            }
            PDDocumentOutline outline = new PDDocumentOutline();
            document.getDocumentCatalog().setDocumentOutline(outline);
            final String bookmarkPath = "D:/download/jsr-283-fcs/spec/jcr-spec-bookmarks.txt";
            String line = null;
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(bookmarkPath)));
            PDOutlineItem titleOutline = null;
            Pattern pattern = Pattern.compile("\t?([0-9. \\-:a-zA-Z]+)\\s+(\\d+)");
            List pages = document.getDocumentCatalog().getAllPages();
            while ((line = br.readLine()) != null) {
                if (line.isEmpty())
                    continue;
                Matcher m = pattern.matcher(line);
                if (m.matches()) {
                    System.out.println(line);
                    PDPage page = (PDPage) pages.get(Integer.parseInt(m.group(2)) - 1);
                    PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
                    dest.setPage(page);
                    PDOutlineItem bookmark = new PDOutlineItem();
                    bookmark.setDestination(dest);
                    bookmark.setTitle(m.group(1));
                    if (line.startsWith("\t") == false) {
                        titleOutline = bookmark;
                        outline.appendChild(titleOutline);
                    } else {
                        titleOutline.appendChild(bookmark);
                    }
                } else {
                    throw new RuntimeException("not match, line=" + line);
                }
            }
            br.close();
            outline.openNode();

            document.save(outputPath);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}

参考资料

=文章版本=

20130518

No comments:

Post a Comment