簡體   English   中英

在 Java 中使用 PrinterJob 打印 PDF 文件

[英]Print a PDF file using PrinterJob in Java

嘗試使用 Java 打印 PDF 文件時遇到問題。 這是我的代碼:

PdfReader readFtp = new PdfReader();    // This class is used for reading a PDF file
PDDocument document = readFtp.readFTPFile(documentID);

printRequestAttributeSet.add(new PageRanges(1, 10));

job.setPageable(document);
job.print(printRequestAttributeSet);    // calling for print

document.close()


我使用document.silentPrint(job); job.print(printRequestAttributeSet); - 它工作正常。 如果我使用document.silentPrint(job); - 我無法設置PrintRequestAttributeSet

誰能告訴我如何設置PrintRequestAttributeSet

我的打印機不支持原生 PDF 打印。

我使用開源庫 Apache PDFBox https://pdfbox.apache.org來打印 PDF。 打印本身仍然由 Java 的 PrinterJob 處理。

import java.awt.print.PrinterJob;
import java.io.File;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class PrintingExample {

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

這對我用普通 JRE 打印 PDF 很有用:

public static void main(String[] args) throws PrintException, IOException {
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
    PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet();
    patts.add(Sides.DUPLEX);
    PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts);
    if (ps.length == 0) {
        throw new IllegalStateException("No Printer found");
    }
    System.out.println("Available printers: " + Arrays.asList(ps));

    PrintService myService = null;
    for (PrintService printService : ps) {
        if (printService.getName().equals("Your printer name")) {
            myService = printService;
            break;
        }
    }

    if (myService == null) {
        throw new IllegalStateException("Printer not found");
    }

    FileInputStream fis = new FileInputStream("C:/Users/John Doe/Desktop/SamplePDF.pdf");
    Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    DocPrintJob printJob = myService.createPrintJob();
    printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
    fis.close();        
}

以下內容對我有用,可以使用打印對話框打印多個 PDF 文檔:

public void printPDF()
{
    PrinterJob printerJob = PrinterJob.getPrinterJob();

    PrintService printService;
    if(printerJob.printDialog())
    {
        printService = printerJob.getPrintService();
    }
    DocFlavor docType = DocFlavor.INPUT_STREAM.AUTOSENSE;

    for (//fetch documents to be printed)
    {
        DocPrintJob printJob = printService.createPrintJob();
        final byte[] byteStream = // fetch content in byte array;
            Doc documentToBePrinted = new SimpleDoc(new ByteArrayInputStream(byteStream), docType, null);
        printJob.print(documentToBePrinted, null);
    }
}

試試這個代碼:

FileInputStream fis = new FileInputStream(“C:/mypdf.pdf”);
Doc pdfDoc = new SimpleDoc(fis, null, null);
DocPrintJob printJob = printService.createPrintJob();
printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
fis.close();

您也可以按照以下步驟操作

import java.awt.print.PrinterJob;
import java.io.File;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

public class Main {

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

        String filename = "Path for PDF File"; 
        PDDocument document = PDDocument.load(new File (filename));

        //takes standard printer defined by OS
        PrintService myPrintService = PrintServiceLookup.lookupDefaultPrintService();
        myPrintService = findPrintService("Your 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;
    }

使用 Apache PDFBox 2.0.4 版。 如果使用 Maven 項目,請在 XML 文件中包含以下內容。

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.4</version>
</dependency>

不推薦使用 PDDocument 的可分頁實現,改用 PDPageable 適配器類並嘗試 setPrintable 而不是 setPageable:

job.setPrintable(new PDPageable(document));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM