Hi,

If this happened with 3.0.0 or 3.0.1 please retry with 3.0.2. If not, then please find a non confidential file where that happens. Also make sure that src and dst are different files.

Tilman

On 19.03.2024 15:26, Frédéric Ravetier wrote:
Hello,

I am trying to Flatten a PDF using PDFBox3 by doing :

private static void flattenPDF(String src, String dst) throws IOException {
     PDDocument doc = Loader.loadPDF(new RandomAccessReadBufferedFile( src ));

     PDDocumentCatalog catalog = doc.getDocumentCatalog();
     PDAcroForm acroForm = catalog.getAcroForm();
     if (acroForm == null){
         logger.debug("This document does not contains any form,
nothing to do...");
     }else {
         acroForm.setNeedAppearances(false);
         acroForm.flatten();// Flatten using pdfbox3
     }
     doc.save(dst);
     doc.close();
}

It works but it creates in some cases a document that is not readable using
PDFBox 2 where I get this error:
java.io.IOException: COSObject{525, 0} cannot be assigned to offset
1528842, this belongs to COSObject{4196, 0}

at
org.apache.pdfbox.pdfparser.COSParser.parseDictObjects(COSParser.java:736)
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:185)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:231)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1233)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1133)

With the following code :

System.out.printf("\n*\n* demo.pdf\n*\n");
try (
     InputStream resource =
getClass().getResourceAsStream("/mkl/testarea/pdfbox2/extract/bad-annot-1.pdf")
    ) {
     //OutputStream result = new FileOutputStream(new
File(RESULT_FOLDER, "bad-pdf-sign.pdf"));
     PDDocument pdDocument = PDDocument.load(resource);
     System.out.printf("Producer of document : %s\n",
pdDocument.getDocumentInformation().getProducer());
     AccessPermission accessPermission = 
pdDocument.getCurrentAccessPermission();
     if (accessPermission.isReadOnly()) {
         System.out.printf("The document cannot be modified (read-only)");
     }

     if (!accessPermission.canModify()) {
         System.out.printf("Cannot modify the document");
     }

     if (!accessPermission.canModifyAnnotations()) {
         System.out.printf("Cannot modify the annotation");
     }

     if (!accessPermission.canFillInForm()) {
         System.out.printf("Cannot fill in form");
     }

}


Do you have any ideas why ?

I can not share the document (confidential) :(

Best regards,
Fred



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to