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?
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.