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 docu
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;
}
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);
}
}
}
This works fine for me. But is a old version pdfbox. The new version not support .load and .silentprint
public static void print(String nomImpresora, int cantVia) throws Exception {
String aux;
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
int selectedService = -1;
if (nomImpresora != null) {
for (int i = 0; i < services.length; i++) {
aux = services[i].getName();
Log.addLog(Log.tipoMensaje.ErrorGenerico, "El valor de aux: " + aux + ".");
if (services[i].getName().toUpperCase().contains(nomImpresora.toUpperCase())) {
/*If the service is named as what we are querying we select it */
selectedService = i;
}
}
}
if (selectedService == -1) {
new Exception("Impresora no encontrada " + nomImpresora);
}
File fileToPrint = new File(rutaNombreArchivo);
PDDocument load = PDDocument.load(fileToPrint.toString());
try {
PrinterJob printJob = PrinterJob.getPrinterJob();
Log.addLog(Log.tipoMensaje.ErrorSQL, "selected service" + selectedService);
printJob.setPrintService(services[selectedService]);
printJob.setJobName(fileToPrint.getName());
final HashPrintRequestAttributeSet printRequestAttributes = new HashPrintRequestAttributeSet();
printJob.print(printRequestAttributes);
for (int i = 1; i <= cantVia; i++) {
load.silentPrint(printJob);
}
} catch (final PrinterException e) {
e.printStackTrace();
} finally {
if (load != null) {
load.close();
}
}
}
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.
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);