title: Convert a XFA PDF to an image with PDFBox
body: <p>Is there a way to convert a <strong><a href="http://en.wikipedia.org/wiki/XFA" rel="nofollow noreferrer">XFA</a></strong> PDF into a set of images (png or jpeg) with Apache's <a href="https://pdfbox.apache.org/" rel="nofollow noreferrer">PDFBox</a>?</p>
  
  <p>I use version (1.8.6) which is supposed to have XFA support.</p>
  
  <p>The PDF file to convert is a <strong>dynamic PDF form</strong> (XFA). Converting a static PDF form does not make any problem. </p>
  
  <p>I was successful with PDFBox V 1.8.6 by calling the <code>Page.convertToImage()</code> method.</p>
  
  <p>My attempt with an XFA PDF resulted in this image: </p>
  
  <p><img src="https://i.stack.imgur.com/9O4vF.png" alt="enter image description here"></p>
  
  <p>Here is the code I used to test the PDF conversion:</p>
  
  <pre><code>public void convertToImages(File sourceFile, File destinationDir){
      if (!destinationDir.exists()) {
          destinationDir.mkdir();
          System.out.println("Folder Created -&gt; "+ destinationDir.getAbsolutePath());
      }
      if (sourceFile.exists()) {
          System.out.println("Images copied to Folder: "+ destinationDir.getName());             
          PDDocument document = null;
          try {
              //The classical way to lod the PDF document doesn't work here
              //document = PDDocument.load(sourceFile);
              File scratch = new File(destinationDir, "scratch");
              if(scratch.exists())scratch.delete();
              document = PDDocument.loadNonSeq(sourceFile, new RandomAccessFile(scratch, "rw"));
              //Doesn't seem to have an effect in my case but I keep it ;-)
              document.setAllSecurityToBeRemoved(true);
              @SuppressWarnings("unchecked")
              List&lt;PDPage&gt; list = document.getDocumentCatalog().getAllPages();
              System.out.println("Total files to be converted -&gt; "+ list.size());
  
              String fileName = sourceFile.getName();
              int pos = fileName.lastIndexOf('.');
              fileName = fileName.substring(0, pos);
              int pageNumber = 1;
              for (PDPage page : list) {
                  File outputfile = new File(destinationDir, fileName +"_"+ pageNumber +".png");
                  try {
                      BufferedImage image = page.convertToImage();
                      ImageIO.write(image, "png", outputfile);
                      pageNumber++;
                      if(outputfile.exists()){
                          System.out.println("Image Created -&gt; "+ outputfile.getName());
                      } else {
                          System.out.println("Image NOT Created -&gt; "+ outputfile.getName());
  
                      }
                  } catch (Exception e) {
                      System.out.println("Error while creating image file "+ outputfile.getName());
                      e.printStackTrace();
                  }
              }
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } finally {
              if(document != null){
                  try {
                      document.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
  
          }
          System.out.println("Converted Images are saved at -&gt; "+ destinationDir.getAbsolutePath());
      } else {
          System.err.println(sourceFile.getName() +" File does not exists");
      }
  }
  </code></pre>
  
  <p>Is there anything special to do in that case? </p>
  
  <p>I tried the GIMP despite the fact that it should be used on a server but it doesn't work with dynamic PDF either.</p>
  
  <p>I also have tried ImageMagick but it didn't work at all. Since I would be very surprised that it could solve anything I gave up further investigations.</p>