zno2

使用 apache pdfbox 合并pdf文件

 

import java.io.File;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.junit.Test;

public class PDFUtilsTest {

    @Test
    public void mergePdfs() throws Exception {
        // 合并下面的pdf ,每个都是单独的一章,带标签
        
        // E:\test\parts\01.pdf
        // E:\test\parts\02.pdf
        // E:\test\parts\03.pdf
        // E:\test\parts\04.pdf
        // E:\test\parts\05.pdf
        // E:\test\parts\06.pdf
        // E:\test\parts\07.pdf
        // E:\test\parts\08.pdf
        // E:\test\parts\09.pdf
        // E:\test\parts\10.pdf

        File file = new File("E:\\test\\parts");
        IOFileFilter onlypdf = FileFilterUtils.suffixFileFilter(".pdf");
        IOFileFilter noFolder = FileFilterUtils.falseFileFilter();
        Iterator<?> pdfFiles = FileUtils.iterateFiles(file, onlypdf, noFolder);
        PDDocument target = new PDDocument();
        PDDocumentCatalog documentCatalog = target.getDocumentCatalog();
        // 创建标签(outline)
        PDDocumentOutline rootOutline = new PDDocumentOutline();
        documentCatalog.setDocumentOutline(rootOutline);

        try {
            while (pdfFiles.hasNext()) {
                File part = (File) pdfFiles.next();
                PDDocument doc = PDDocument.load(part);
                PDDocumentOutline subOutline = doc.getDocumentCatalog().getDocumentOutline();
                PDOutlineItem firstChild = subOutline.getFirstChild();
                rootOutline.addLast(firstChild);
                PDPageTree pages = doc.getPages();
                for (PDPage pdPage : pages) {
                    target.addPage(pdPage);
                }
            }

            target.save(new File("E://test/Design Patterns 中文版.pdf"));
        } finally {
            target.close();
        }

    }
}

 

http://www.netlikon.de/docs/PDFBox-0.7.2/docs/userguide/bookmarks.pdf

posted on 2023-06-06 13:20  zno2  阅读(263)  评论(0编辑  收藏  举报

导航