Laurent Loots
Laurent Loots

Reputation: 145

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.)

Now, I want to save this file to PDF so that it can be mailed directly to the customer. Seems kinda hard in Java. To make it easier, I just want to print my source file using the Windows Printing Dialog, and then select a PDF printer.

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...

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?

Upvotes: 0

Views: 969

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

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

Upvotes: 1

Related Questions