title: Converting dynamic XFA to static PDF
body: <p>Requirement: View XFA based PDFs on mobile devices.<br/>
  Options I've tried:</p>
  
  <ul>
  <li>Since XFA aren't supported on Adobe mobile reader, I get to flatten an XFA as a static PDF. I tried but dynamic XFAs can't be converted to static PDFs using iText.</li>
  <li> Later I tried printing XFA forms using 'Adobe PDF' as print service. That works expectedly while performed manually, but somehow clears form data while performed through code.</li>
  </ul>
  
  <p>Below is the sample code for print task. Adobe Acrobat DC is installed for 'Adobe PDF' print service.</p>
  
  <pre><code>import java.awt.print.PrinterJob;
  import javax.print.PrintService;
  import javax.print.PrintServiceLookup;
  
  ...
  private static void writePDF(long uid, Path path) throws Exception {
          final String inFile = path.toString();
          PDDocument pdfdoc = PDDocument.load(inFile);
          PrinterJob printJob = PrinterJob.getPrinterJob();
  
          printJob.setPageable(new PDPageable(pdfdoc));
          // printJob.setPrintable(new PDPageable(pdfdoc));
          printJob.setPrintService(getSystemPrinter("Adobe PDF"));
  
          printJob.setJobName(path.getFileName().toString());
          pdfdoc.silentPrint(printJob);
      }
  
  private static PrintService getSystemPrinter(final String printerName) {
          PrintService desiredPrinter = null;
          for (PrintService printer : PrintServiceLookup.lookupPrintServices(null, null)) {
              if (printerName.equalsIgnoreCase(printer.getName())) {
                  desiredPrinter = printer;
                  break;
              }
          }
          return desiredPrinter;
      }
  </code></pre>
  
  <p>Someone please suggest a workaround to achieve the desired. Thanks!</p>