mkl-public / testarea-pdfbox2

Test area for public PDFBox v2 issues on stackoverflow etc
Apache License 2.0
83 stars 44 forks source link

Differ page a bit to reassure we handle multiple identical page objets #17

Closed elvisbegovic closed 7 months ago

elvisbegovic commented 7 months ago

Make optimize method less agressive to fix issue when page are identical. For that we make all pages of a PdDocument different by adding custom entry to the page dictionary (page counter). Result: file output will be a few bytes larger.

elvisbegovic commented 7 months ago

What you think @mkl-public , if needed we can fix this optionally :

         /* @param differIdenticalPage : when we have have identical page objet we should enable this option to fix som reader tool */
     public static void optimize(PDDocument pdDocument, Boolean differIdenticalPage) throws IOException {
        // reassure we keep multiple identical page objet
        if(differIdenticalPage != null && differIdenticalPage == true) {
            for (int i = 0; i < pdDocument.getPages().getCount(); i++) {
                PDPage page = pdDocument.getPages().get(i);
                COSDictionary customDict = new COSDictionary();
                customDict.setInt("CustomCounter", i + 1);
                page.getCOSObject().setItem("CustomCounterEntry", customDict);
            }

        }
        optimize(pdDocument);
    }
mkl-public commented 7 months ago

Thanks!