使用Java中的PrinterJob打印PDF文件

use*_*844 16 java printing java-print

尝试使用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()
Run Code Online (Sandbox Code Playgroud)


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

谁能告诉我如何设置PrintRequestAttributeSet

Ren*_*Ren 25

我的打印机不支持原生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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 最好关闭PDDocument,因此你需要一个try catch和一个最终,但总体来说还可以! (2认同)

Mir*_*ert 18

这对我来说可以用普通的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();        
}
Run Code Online (Sandbox Code Playgroud)

  • 该代码仅适用于能够自行解释PDF的打印机.我找到了一些这样做的打印机(例如,一些HP LaserJet型号),但我也经历过一些打印机打印PDF文件的原始内容(作为文本).在这种情况下,您需要将PDF转换为图像(例如,使用Ghostview). (3认同)
  • 这只能在 Linux 上运行,对吗?我相信 Windows 上没有 Java 中的 PDF 渲染器,对吗? (2认同)
  • @Mirko Seifert 这段代码不起作用,我的打印机只打印不可读的字符,而不是我的 PDF 文档。 (2认同)
  • 虽然这个回复是在很多年前发布的,但今天仍然有效,并节省了我数小时的开发工作。谢谢你,先生。无论您今天身在何处,谢谢您! (2认同)

Lin*_*nga 0

试试这个代码:

\n\n
FileInputStream fis = new FileInputStream(\xe2\x80\x9cC:/mypdf.pdf\xe2\x80\x9d);\nDoc pdfDoc = new SimpleDoc(fis, null, null);\nDocPrintJob printJob = printService.createPrintJob();\nprintJob.print(pdfDoc, new HashPrintRequestAttributeSet());\nfis.close();\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n