PDFBox:如何使用指定的打印机打印pdf?

时间:2013-09-05 12:40:41

标签: java pdf printing pdfbox

我想使用PDFBox打印由iText创建的PDF文件。我已经使用PDDocument类及其方法print()成功地尝试了这个。您可以在此处找到文档:http://pdfbox.apache.org/apidocs/

(我正在使用此代码:)

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

方法print()效果很好,但有一个问题:当我需要打印多个文件时,该方法要求我为每个文档选择打印机..

有没有办法如何只设置一次打印机?

对于打印机选择,我可以使用此代码:

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

提前致谢


解决方案:

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);
}

5 个答案:

答案 0 :(得分:12)

PDDocument还提供了除无参数print()之外的其他打印方法:

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

silentPrint方法不显示对话框。

首先选择一台打印机,然后通过相应初始化的silentPrint个实例调用PrinterJob,即可获得所需内容。

答案 1 :(得分:3)

 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();
}

}

使用上面的代码使用apache Pdfbox打印pdf

编辑:版本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);
    }
  }
}

答案 2 :(得分: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);

答案 3 :(得分:1)

您可以在 PrinterJob 对象上使用 setPrintService()方法

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;
}

答案 4 :(得分:0)

这对我来说很好。但是是旧版本的pdfbox。新版本不支持.load和.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();
    }
}


}