Print a PDF Document in Java
For some time now I have searched for a way to print a PDF document from Java code. Up to now, I have had to shell out and invoke Acrobat from the command line but this hack has been painful to support in the field because of the multiple version of Acrobat and its quirks.
A new PDF Renderer project has recently been released on java.net which can in addition to rendering and viewing a PDF document, it can be used to print a PDF document. If you download the PDF Renderer you can run the jar to start a sample PDF viewer application which can print PDF documents. All the code listed below is inspired from the PDF Renderer sample application.
The following code uses classes from the PDF Renderer library and standard Java. Like usual, I have omitted all import statements.
One critical class required to print a PDF document using PDF Renderer is the PDFPrintPage. The PDFPrintPage implements the Printable print method. The PDFPrintPage is included in the PDFRenderer.jar, but in the source distribution it is found under the demos project. I have included the source to PDFPrintPage print method here…
A final note, PDF Renderer does require Java 1.5 or greater.
Technorati Tags: java, jre, pdf, acrobat, renderer, print, pdffile
















Cool project, I have to check that out…
Have you looked into iText from Lowagie?
http://www.lowagie.com/iText/
[…] It’s very interesting and it answers a question that turns up on the mailing list once and again. […]
@Mike - I looked into iText sometime back, but if I remember correctly it is used to create PDF documents, not print them. Before writing this article, I did google for tutorials on how to print PDF documents from Java and all I got was forum posts asking for help with this…
Indeed, currently the biggest problem in the client area related with pdf’s is printing. there is no real library can be used by closed source applications. the commercial ones are extremely pricey. thanks for your example, it is nice. but unfortunately pdf renderer still requires a lot of work for complicated pages. it is definitely on the right track tough.
@TechKnow - I wrote something like the PrintPDFPage impl above. If there is something iText can give you that this framework doesn’t, it’s worth the effort. If not, this looks like a good bet.
[…] On the blog Juixe TechKnow you can read how to print a PDF document from a Java program. […]
This is sweet! Thanks for sharing.
Jay
Hi - am I missing something crusial here?
I mean - I can do “lp test.pdf” on my Mac (from a shell/Terminal), Windows is obviously not a problem, and most scripting frameworks (PHP, Ruby(OnRails), Perl, et al) on webservers will allow me a shell escape or the ability to run system commands of some sorts. In which case I’m full circle to my “lp test.pdf”.
Excuse my ignorance if I am missing the entire focus of this post
I am attempting to print a simple pdf with the above code fragment.
The fc.size is 3583 and pdfFile.getNumPages is 2. However the printed pages are blank. If I try to view it with PagePanel, it is also blank.
Any ideas?
thanks man….after hours of searching I finally found your solution.
Thanks for sharing. Really nice work. That s what I need;)
I have difficulties in running the second code. I ll be glad if someone can helpme.
When I print, it is printing in small fonts. Any one know how to change the font size , sucht that it prints in larger fonts.