You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Frédéric Ravetier <fr...@vikta.com> on 2024/03/19 14:26:52 UTC
Flatten using PDFBOX3
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
Re: Flatten using PDFBOX3
Posted by Frédéric Ravetier <fr...@vikta.com>.
I'm using PDFBox 3.0.2 and src and dst are not the same.
I will try to get a non confidential file.
Le mar. 19 mars 2024 à 15:36, Tilman Hausherr <TH...@t-online.de> a
écrit :
> 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-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>
Re: Flatten using PDFBOX3
Posted by Tilman Hausherr <TH...@t-online.de>.
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-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org