We have an app that can generate multi-page PDF Files. We recently ran into a
problem where the library we were using would keep ALL the pages in memory.
For a quick workaround we have it write out single-page PDF files, then use
PDFBox to combine them.
We recently found a bug in the way that the pages get modified when combined
into a single PDF.
When we generate the pages, sometimes the MediaBox starts at negative
coordinates. When PDFBox adds that page to a document, it offsets it by that
negative amount - which moves the page content up and to the right.
Out page combining code looks like this.
try (PDDocument doc = new
PDDocument(MemoryUsageSetting.setupTempFileOnly())) {
for (File pagFile : srcPages) {
log.debug("make: page {}",
pagFile.getAbsolutePath());
PDPage page = new PDPage();
doc.addPage(page);
try (PDPageContentStream contents = new
PDPageContentStream(doc, page)) {
try (PDDocument sourceDoc =
Loader.loadPDF(pagFile, MemoryUsageSetting.setupTempFileOnly())) {
PDPage srcPage =
sourceDoc.getPage(0);
page.setUserUnit(srcPage.getUserUnit());
page.setMediaBox(srcPage.getMediaBox());
page.setCropBox(srcPage.getCropBox());
page.setTrimBox(srcPage.getTrimBox());
// Create a Form XObject from
the source document using LayerUtility
LayerUtility layerUtility = new
LayerUtility(doc);
PDFormXObject form =
layerUtility.importPageAsForm(sourceDoc, 0);
// draw the full form
contents.drawForm(form);
}
}
}
doc.save(outPDF);
}
The original Page pdf has a TrimBox[0,0,1296,864], MediaBox[-72,-72,1368,936]
The page in the PDFBox combined output has the same TrimBox and MediaBox, BUT
the /Form1 it uses to place the contents has a BBox[-72,-72,1368,936] and a
Matrix[1,0,0,1,72,72].
I'm not sure why it's adding a Matrix to offset the content.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]