How to print PDF file in a Java application?

15,966

Solution 1

Here some source code that will allow you print any text file:

public void print() {
    //The desktop api can help calling other applications in our machine
    //and also many other features...
    Desktop desktop = Desktop.getDesktop();
    try {
    //desktop.print(new File("DocXfile.docx"));
        desktop.print(new File("Docfile.pdf"));
    } catch (IOException e) {           
        e.printStackTrace();
    }
}

Maybe it suit your needs since you did not give more details.

Solution 2

Try PDF Renderer. It's open source and there are a couple of examples on the site on how to render to a printer device.

Solution 3

I've used PDFBox before for a similar task like yours. It's an excellent library from the Apache Software Foundation. The class you are probably going to use is called: PDFTextStripper . The javadoc for the class can be found here.

Share:
15,966
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin about 2 years

    How do I print a PDF file from a Java application?