0

How can I transform a PDF with forms made in Adobe Livecycle to a simple image PDF using Java? I tried using Apache PDFBox but it can't save as image a PDF with forms. This is what I tried(from this question: Convert PDF files to images with PDFBox)

    String pdfFilename = "PDForm_1601661791_587488.pdf";
    try (PDDocument document = PDDocument.load(new File(pdfFilename))) {
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        for (int page = 0; page < document.getNumberOfPages(); ++page) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
            ImageIOUtil.writeImage(bim, pdfFilename + "-" + (page+1) + ".png", 300);
        }
    } catch (IOException ex) {
        Logger.getLogger(StartClass.class.getName()).log(Level.SEVERE, null, ex);
    }

But is not working, the result is an image where it writes that "The document you are trying to load requires Adobe Reader 8 or higher.

  • 1
    That means that you have a PDF with a pure XFA form definition. XFA forms in pdfs are supported by hardly any non-Adobe pdf processor and have been obsoleted/deprecated three years ago but they are still are used in particular by government agencies. Pdfbox does not support such forms, it merely allows retrieving and setting its xml representation. – mkl Oct 02 '20 at 19:38

1 Answers1

0

I guess is not possible, I tried many libraries and none worked.

This is how I solved the problem:

I used an external tool - PDFCreator. In PDFCreator I created a special printer that prints and saves the PDF without asking any questions(you have these options in PDFCreator). This is simple to reproduce in PDFCreator because in the Debug section you have an option to load a config file, so I have this file prepared, I just install PDFCreator and load the config file.

If you will use my INI file in the link above you should know that the resulted PDF is automatically saved in the folder: "current user folder/Desktop/temporary".

The rest of the job is done from Java using Adobe Reader, the code is in my case:

ProcessBuilder pb = new ProcessBuilder(adobePath, "/t", path+"/"+filename, printerName);
Process p = pb.start();

This code opens my PDF in AdobeReader, prints the PDF to the specified virtual printer, and exists automatically.

  • "adobePath" is the path to the adobe executable
  • path+"/"+filename is the path to my PDF.
  • "printerName" is the name of the virtual printer created in PDFCreator

So this is not a pure Java solution and in the future, I intend to use Apache PDFBox to generate my PDF's in a format that is compatible with browsers and all readers...but this works also.