title: PDFBox: How to print pdf with specified printer?
body: <p><strong>I want to use PDFBox for printing PDF files</strong> created by iText. I have tried this successfully with PDDocument class and its method print(). You can find documentation here: <a href="http://pdfbox.apache.org/apidocs/" rel="noreferrer">http://pdfbox.apache.org/apidocs/</a>.</p>
  
  <p>(I am using this code:)</p>
  
  <pre><code>public static void printPDF(String fileName)
          throws IOException, PrinterException {
      PDDocument doc = PDDocument.load(fileName);
      doc.print();
  }
  </code></pre>
  
  <p>The method print() works great, but <strong>there is one problem: When I need to print multiple files, the method asks me to select printer for each one of documents..</strong></p>
  
  <p><strong>Is there any way how to set printer only once?</strong></p>
  
  <p>For printer selection I can use this code for example:</p>
  
  <pre><code>public static PrintService choosePrinter() {
      PrinterJob printJob = PrinterJob.getPrinterJob();
      if(printJob.printDialog()) {
          return printJob.getPrintService();          
      }
      else {
          return null;
      }
  }
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <hr>
  
  <h2>Solution:</h2>
  
  <pre><code>public static PrintService choosePrinter() {
      PrinterJob printJob = PrinterJob.getPrinterJob();
      if(printJob.printDialog()) {
          return printJob.getPrintService();          
      }
      else {
          return null;
      }
  }
  
  public static void printPDF(String fileName, PrintService printer)
          throws IOException, PrinterException {
      PrinterJob job = PrinterJob.getPrinterJob();
      job.setPrintService(printer);
      PDDocument doc = PDDocument.load(fileName);
      doc.silentPrint(job);
  }
  </code></pre>