我们有许多系统可以生成需要打印的PDF。它们存储在中央文档存储中。然后,消息将进入文档需要打印的JMS队列。用Java编写的服务会选择这些服务,然后调用本机命令。这是使用/ t标志调用Adobe Reader。这会导致文档打印而不显示GUI。
然而,由于断电,这不再有效。在此期间,我们不得不手动打印数百个文档。我们最初尝试使用Java打印,但PDF格式不正确。
对此有什么更好的解决方案?
答案 0 :(得分:3)
此代码仅在打印机支持PDF时有效。否则,您需要使用本机打印机或Java库。在http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java
上有一篇博客文章答案 1 :(得分:1)
向我们展示代码。我记得使用Java Print API打印PDF没有任何问题。下面是代码,可能需要一些修改,但应该按原样运行,
InputStream in = new FileInputStream(file);
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// find the printing service
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("FX", null));
attributeSet.add(new Copies(1));
PrintService[] services = PrintServiceLookup.lookupPrintServices(
DocFlavor.INPUT_STREAM.PDF, attributeSet);
//create document
Doc doc = new SimpleDoc(in, flavor, null);
// create the print job
PrintService service = services[0];
DocPrintJob job = service.createPrintJob();
// monitor print job events
PrintJobWatcher watcher = new PrintJobWatcher(job);
System.out.println("Printing...");
job.print(doc, null);
// wait for the job to be done
watcher.waitForDone();
System.out.println("Job Completed!!");
注意:
Flavor
,1个地方就足够了。你找到了。PrintJobWatcher
是一个嵌套类,用于添加PrintJobListener
。答案 2 :(得分:1)
尝试使用ICEpdf。以下是documentation page的示例:
Document pdf = new Document();
pdf.setFile(filePath);
// create a new print helper with a specified paper size and print
// quality
PrintHelper printHelper = new PrintHelper(null, pdf.getPageTree(),
0f, MediaSizeName.NA_LEGAL, PrintQuality.DRAFT);
// try and print pages 1 - 10, 1 copy, scale to fit paper.
printHelper.setupPrintService(selectedService, 0, 0, 1, true);
// print the document
printHelper.print();
答案 3 :(得分:1)
自Java 1.5以来,Sun开发了一个用于处理PDF的pdf渲染器库。现在这个留给Swing Labs。并且不确定这个是否会被添加到未来的Java API中。 http://java.net/projects/pdf-renderer/
用于查看或打印pdf文件。要打印pdf文件,您可以调用此libray。这是代码的一部分。
File input = new File(docName);
FileInputStream fis = new FileInputStream(input);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile curFile=null;
PDFPrintPage pages=null;
curFile = new PDFFile(bb); // Create PDF Print Page
pages = new PDFPrintPage(curFile);
PrinterJob pjob = PrinterJob.getPrinterJob();
PrintService[] services = pjob.lookupPrintServices();
for(PrintService ps:services){
String pName = ps.getName();
if(pName.equalsIgnoreCase("PrinterName")){
pjob.setPrintService(ps);
System.out.println(pName);
break;
}
}
pjob.setJobName(docName);
Book book = new Book();
PageFormat pformat = PrinterJob.getPrinterJob().defaultPage();
book.append(pages, pformat, curFile.getNumPages());
pjob.setPageable(book);
// print
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// Print it
pjob.print(aset);
答案 4 :(得分:0)
您可以使用Apache PDFBox。例子:
Pageable
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPageable pdfPageable = new PDFPageable(pdDocument);
SimpleDoc doc = new SimpleDoc(pdfPageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
printJob.print(doc, null);
Printable
此选项的优势在于您可以通过修改pageFormat
变量来控制页面尺寸,边距等。
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPrintable pdfPrintable = new PDFPrintable(pdDocument);
Book book = new Book();
book.append(pdfPrintable, pageFormat);
SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
printJob.print(doc, null);