Clip and create new pdf from existing pdf with page numbers as input - pdfbox

冷暖自知 提交于 2019-12-11 02:54:54

问题


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();

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?


回答1:


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();

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

Then use PDFSplit and PDFMerge.



来源:https://stackoverflow.com/questions/27684355/clip-and-create-new-pdf-from-existing-pdf-with-page-numbers-as-input-pdfbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!