本文介绍了从现有的pdf中剪切并创建新的pdf,并以页码作为输入-pdfbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10页的pdf文件,我需要将页面从2剪切到5,然后创建一个新的pdf.我正在做的事情如下:

I have a pdf file with 10 pages, I need to clip the pages from 2 to 5 and create a new pdf. What I am doing is like the following:

PDDocument pddDocument=PDDocument.load(new File("sample.pdf")); 
PDFTextStripper textStripper=new PDFTextStripper(); 
String text = textStripper.getText(pddDocument).toString();

我只是在阅读pdf文件并写入一个新文件.如何剪辑上下限的页码?请指导我?

I am simply reading the pdf file and writing into a new file. How can clip with upper and lower bound as page numbers? Please guide me?

推荐答案

此解决方案(适用于PDFBox 1.8.*)创建具有所需内容的PDF文件.请注意,页面是从零开始的.

This solution (for PDFBox 1.8.*) creates a PDF file with the contents like you asked for. Note that pages are zero-based.

    File originalPdf = new File("{File Location}");
    PDDocument srcDoc = PDDocument.load(originalPdf);
    PDDocument dstDoc = new PDDocument();

    List<PDPage> srcPages = srcDoc.getDocumentCatalog().getAllPages();

    for (int p = 0; p < srcPages.size(); ++p)
    {
        if (p >= 1 && p <= 4)
            dstDoc.addPage(srcPages.get(p));
    }

    dstDoc.save(file2);
    dstDoc.close();
    srcDoc.close();

如果要改为从命令行工作,请查看此处: https://pdfbox.apache.org/commandline/

If you want to work from the command line instead, look here:https://pdfbox.apache.org/commandline/

然后使用PDFSplit和PDFMerge.

Then use PDFSplit and PDFMerge.

这篇关于从现有的pdf中剪切并创建新的pdf,并以页码作为输入-pdfbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 11:13