简体   繁体   English

用Java简单打印

[英]Simple printing in Java

I'm working on some invoice software. 我正在开发一些发票软件。 So far, I managed to create an excel file (XLSX) with all the info I need (customer info, VAT, pricing etc.) 到目前为止,我设法用我需要的所有信息(客户信息,增值税,价格等)创建了一个Excel文件(XLSX)。

Now, I want to save this file to PDF so that it can be mailed directly to the customer. 现在,我想将此文件保存为PDF,以便可以直接邮寄给客户。 Seems kinda hard in Java. 在Java中似乎有点困难。 To make it easier, I just want to print my source file using the Windows Printing Dialog, and then select a PDF printer. 为了简化操作,我只想使用Windows打印对话框打印源文件,然后选择PDF打印机。

This little piece of code works, but it starts the printing job immediately using the default printer, without showing any dialog whatsoever. 这小段代码有效,但是它立即使用默认打印机开始打印作业,而不显示任何对话框。 Not what I want. 不是我想要的

desktop.print(new File("Docfile.pdf"));

This piece of code displays the printing dialog, but it's not clear to me (looking at the documentation) how I can tell a PrintJob to print a File or FileInputStream... 这段代码显示了打印对话框,但是我不清楚(请参阅文档)我如何告诉PrintJob打印File或FileInputStream ...

PrinterJob pj = PrinterJob.getPrinterJob();
pj.print();

Either the first code should display the dialog box, or the second one should give me the ability to select a file. 第一个代码应显示对话框,或者第二个代码应使我能够选择文件。 Can't seem to fix it. 似乎无法解决。 Anyone got any ideas? 任何人有任何想法吗?

Start by taking a look at the Printing Trail and in particular Using Print Setup Dialogs 首先查看“ 打印轨迹” ,尤其是使用“打印设置”对话框

From the linked tutorials.... 从链接的教程中...。

PrinterJob pj = PrinterJob.getPrinterJob();
...
    if (pj.printDialog()) {
        try {pj.print();}
        catch (PrinterException exc) {
            System.out.println(exc);
         }
     }   
...    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM