PDFBox: How to print pdf with specified printer?

33,723

Solution 1

PDDocument also offers other print methods than the parameterless print():

public void print(PrinterJob printJob) throws PrinterException;
public void silentPrint() throws PrinterException;
public void silentPrint(PrinterJob printJob) throws PrinterException;

The silentPrint methods don't show the dialog.

You may get what you want by first selecting a printer and then call silentPrint with PrinterJob instances initialized accordingly.

Solution 2

 import java.awt.print.PrinterException;

 import java.io.IOException;

 import org.apache.pdfbox.pdmodel.PDDocument;

 public class Print {

public static void main(String[] args) throws IOException, PrinterException
{
    PDDocument pdf=PDDocument.load("d:\\filename.pdf");
            pdf.print();
}

}

use the above code to print pdf using apache Pdfbox

EDIT: version 2.0.0

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class JPrint {

  public static void main(String[] args) throws IOException, PrinterException {
    String filename;
    filename = "C:\\pdf.pdf";

    try {
      PDDocument pdf = PDDocument.load(new File(filename));
      PrinterJob job = PrinterJob.getPrinterJob();
      job.setPageable(new PDFPageable(pdf));
      job.print();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Solution 3

PDDocument doc = PDDocument.load(new FileInputStream(System.getProperty("java.io.tmpdir") + "\\pdf.pdf"));  //read pdf file.
String printerNameDesired = "VENDOR THERMAL PRINTER";

javax.print.PrintService[] service = PrinterJob.lookupPrintServices(); 
DocPrintJob docPrintJob = null;

int count = service.length;
for (int i = 0; i < count; i++) {
    if (service[i].getName().equalsIgnoreCase(printerNameDesired)) {
        docPrintJob = service[i].createPrintJob();
        i = count;
    }
}

PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setPrintService(docPrintJob.getPrintService());
pjob.setJobName("job");
doc.silentPrint(pjob);

Solution 4

You can use the setPrintService() method on the PrinterJob Object.

public static void main(String args[]) throws Exception {

    PDDocument document = PDDocument.load(new File("C:/temp/example.pdf"));

    PrintService myPrintService = findPrintService("My Windows printer Name");

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(new PDFPageable(document));
    job.setPrintService(myPrintService);
    job.print();

}

private static PrintService findPrintService(String printerName) {
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    for (PrintService printService : printServices) {
        if (printService.getName().trim().equals(printerName)) {
            return printService;
        }
    }
    return null;
}
Share:
33,723
Firzen
Author by

Firzen

Updated on July 12, 2020

Comments

  • Firzen
    Firzen almost 4 years

    I want to use PDFBox for printing PDF files created by iText. I have tried this successfully with PDDocument class and its method print(). You can find documentation here: http://pdfbox.apache.org/apidocs/.

    (I am using this code:)

    public static void printPDF(String fileName)
            throws IOException, PrinterException {
        PDDocument doc = PDDocument.load(fileName);
        doc.print();
    }
    

    The method print() works great, but there is one problem: When I need to print multiple files, the method asks me to select printer for each one of documents..

    Is there any way how to set printer only once?

    For printer selection I can use this code for example:

    public static PrintService choosePrinter() {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        if(printJob.printDialog()) {
            return printJob.getPrintService();          
        }
        else {
            return null;
        }
    }
    

    Thanks in advance


    Solution:

    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);
    }
    
  • Firzen
    Firzen almost 11 years
    Thank you very much! I was blind.
  • mkl
    mkl over 10 years
    That's exactly what the OP wrote he did and which did not work in a satisfying manner.
  • Admin
    Admin over 8 years
    Can you please show example of the silentPrint(). Nobody explained about the silentPrint.
  • Admin
    Admin over 8 years
    How do i select specific printer before pdf.print(); ?
  • mkl
    mkl over 8 years
    @YumYumYum which PdfBox version do you use? Some 1.8.x version or the current 2.0.0 release candidate? The whole API has changed considerably...
  • Admin
    Admin over 8 years
    Sir, here is my 2.0.0 code: paste.ubuntu.com/15155477, i have to print it silent mode without any popup. can you please advise kindly ?
  • mkl
    mkl over 8 years
    As mentioned above, the API has changed considerably in 2.0.0. I'll have to search before I can say anything substantial. You should consider asking an actual stack overflow question on this matter, referring to this one and asking how to do the same with PdfBox 2.0.0.