I'm having trouble with PDFBox. I have a blank page in PDF and I want to insert images into it. Because I also work with signed PDFs, all changes have to be saved as "saveIncremental".
When I insert only one image everything is fine (image has been inserted). When I try to insert another image in this PDF, it has not been inserted and when opened in Adobe Acrobat Reader it says "An error exists on this page. Adobe may not display the page correctly ...".
Weird thing - when PDF is not only blank page but e.g. blank page with image, everything is fine (first and also second image has been inserted correctly with saveIncremental).
Code of inserting and saving image:
PDImageXObject pdImage = PDImageXObject.createFromFile(tmpSig.getFileName(), doc);
PDPageContentStream contentStream = new PDPageContentStream(doc, tmpPage, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.drawImage(pdImage, finalX, (finalPageHeight - finalY - finalHeight), finalWidth, finalHeight);
contentStream.close();
// update before save
tmpPage.getCOSObject().setNeedToBeUpdated(true);
tmpPage.getResources().getCOSObject().setNeedToBeUpdated(true);
doc.getDocumentCatalog().getPages().getCOSObject().setNeedToBeUpdated(true);
doc.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true);
// save
doc.saveIncremental(new FileOutputStream(pdfFile));
All files available here
Using PDFBox version 2.0.7 but I also tried the newest (2.0.15) but it didn't help.
Thanks for all ideas!
EDIT: I tried to update XObject and Resources as this (added this code under comment "update before save"):
pdImage.getCOSObject().setNeedToBeUpdated(true);
PDResources pdResources = tmpPage.getResources();
for (COSName name : pdResources.getXObjectNames()) {
pdResources.getXObject(name).getCOSObject().setNeedToBeUpdated(true);
}
Problem still remains, nothing changed...