Search code examples
javapdfbox

In PDFBox, annotations appear in the rendered image but not in an imported object


In the latest version 2.0.30 PDFBox, I found an inconsistency between image rendering and the LayerUtility for rendering a page to an object.

My application transforms a PDF document into an image to work in the application. At the end of the process, we want to print a document with the same quality as the initial PDF. To do this, we obtain a PdFormXObject, but when we add it to a new PDF document, the annotations disappear.

As you can see in the following function, we can compare the result between an image and the cloned PDF.

How to add an annotation to the PDFormXObject or how to ignore the annotation in image processing so that the two tools are consistent. Or maybe it's an error in my code?

Image rendered, you can see annotation : enter image description here

PDF result, annotations doesn't appears (different scale) : enter image description here

    public void pdfbox(File url, float dpi, int idPage) {
        try {
             url = new File("");
             // Helper only for JavaFX user
             url = new FileChooser().showOpenDialog(null);
             
             
            // Transform a pdf to image
            PDDocument document = PDDocument.load(url);
            PDPage page = document.getPage(idPage);
            PDFRenderer renderer = new PDFRenderer(document);
            BufferedImage bim = renderer.renderImageWithDPI(idPage, dpi);
            // Specific fonction to display a buffered image in a JavaFX window.
            Outil.display(new ImageView(SwingFXUtils.toFXImage(bim, null)));


            // Transform a pdf to a layer in a new PDF
            PDDocument newDocument = new PDDocument();
            PDPage newPage = new PDPage( new PDRectangle(0f, 0f, 2000, 2000));
            newDocument.addPage(newPage);
            LayerUtility utility = new LayerUtility(newDocument);
            PDDocument PDFDocument2 = PDDocument.load(url);
            PDFormXObject pdFormXObject = utility.importPageAsForm(PDFDocument2, 0);
            PDPageContentStream stream = new PDPageContentStream(newDocument, newPage, true, false);
            stream.drawForm(pdFormXObject);
            stream.close();
            File tmpFile = Files.createTempFile("test", ".pdf").toFile();
            newDocument.save(tmpFile);
            newDocument.close();
            System.out.println("File : " + tmpFile);
            // Open the PDF file if a default application is supported
            if(Desktop.isDesktopSupported()) Desktop.getDesktop().open(tmpFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I have try to update PDFBox at 2.0.30 version. I have search different option in LayerUtility or in PDFrenderer with out any result.

PS: I don't know how to share you the PDF file with annotation ?


Solution

  • LayerUtility doesn't transfer page annotations.

    To render without annotations, do this:

    PDFRenderer renderer = new PDFRenderer(document);
    renderer.setAnnotationsFilter(new AnnotationFilter()
    {
        @Override
        public boolean accept(PDAnnotation annotation)
        {
            return false;
        }
    });