Hello,
I'm trying to create multiple copies of the pages in a PDF document. The
reason is that I need to split the pages in half - on one copy, I set the
mediaBox to just the left half of the page, and on the other copy, I take
the right.
protected static void splitInHalf(PDDocument doc) throws IOException {
PDDocument outDoc = new PDDocument();
outDoc.setDocumentInformation(doc.getDocumentInformation());
outDoc.getDocumentCatalog().setViewerPreferences(
doc.getDocumentCatalog().getViewerPreferences()
);
List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
for(PDPage page : pages)
{
PDPage newPage;
newPage = outDoc.importPage(page);
newPage.setCropBox(page.findCropBox());
newPage.setResources(page.findResources());
newPage.setRotation(page.findRotation());
newPage.setMediaBox(page.findMediaBox());
newPage = outDoc.importPage(page);
newPage.setCropBox(page.findCropBox());
newPage.setResources(page.findResources());
newPage.setRotation(page.findRotation());
newPage.setMediaBox(page.findMediaBox());
}
pages = outDoc.getDocumentCatalog().getAllPages();
boolean right = false;
for(PDPage page : pages)
{
PDRectangle mBox;
float newWidth;
mBox = page.findMediaBox();
newWidth = mBox.getWidth() / 2;
if(right)
mBox.setLowerLeftX(mBox.getLowerLeftX() + newWidth);
else
mBox.setUpperRightX(mBox.getUpperRightX() - newWidth);
page.setMediaBox(mBox);
right = !right;
}
try {
outDoc.save("C:\\Users\\perigee\\Desktop\\fixedPDF.pdf");
outDoc.close();
} catch(COSVisitorException cve) {
System.out.println("unable to save: " + cve.getMessage());
}
Unfortunately, pdfbox doesn't seem to be able to handle multiple copies of
the same page. I've tried PDDocument::importPage, addPage, and even writing
my own little function to try to make it work - but I don't get anywhere.
How would I go about making multiple copies of the same page (i.e. copying
the contents of a page)?