Java PDF Blog

Java and PDF development - our personal experiences and discoveries

Download JPedal

Download JPedal PDF viewers

PDF to Image service

Try our PDF to image conversion service now.

Java PDF Ebook Solution

Try our Ebook solution now.

Subscribe

Your email:

Java PDF blog

Current Articles | RSS Feed RSS Feed

Printing PDF files from Java

Posted by Mark Stephens on Sat, Jan 30, 2010 @ 04:10 AM
Submit to Digg digg it | Submit to Reddit reddit | Add to delicious delicious | Submit to StumbleUpon StumbleUpon 

Printing PDF files from Java is something that raises a lot of general questions, so this short article is a general guide to the options available. Java itself contains a built-in print system (JPS). JPS itself does not internally support the PDF file format.

There are 3 ways to print PDF files in Java:-

1. Use a printer which directly supports PDF files and use JPS to send the data directly to it.

All the work is done by the printer, often in hardware so this is a brilliant solution if you can precisely define the printers used but does not provide a generic solution. If you want to try this, here is some generic code

FileInputStream fis = new FileInputStream("C:/mypdf.pdf");

Doc pdfDoc = new SimpleDoc(fis, null, null);

DocPrintJob printJob = printService.createPrintJob();

printJob.print(pdfDoc, new HashPrintRequestAttributeSet());

fis.close();

2. Print from Java using a non-java application

Java allows you to access non-java code so that you can access Acrobat, Ghostscript, CUPS or any other solution. You can do this with the Java command

Runtime.getRuntime().exec("commands");

Again, this works if you have control of the exact platforms and software available but does not provide a generic solution.

3. Print using JPS

JPS does not include PDF support, but it does have hooks to allow any Java program to print content to any printer. A number of Java PDF libraries offer printing - they essentially convert the PDF into a rendered page which JPS then prints. This provides a generic solution but the files tend to be larger and it relies on the capabilities of the PDF library which vary.

All three methods have their pros and cons so try them to find out which one offers the best fit for your requirements. Try and see what meets your needs best. If you would like to see a more detailled article please let us know or post your comments here.

Tags: 

COMMENTS

Currently, there are no comments. Be the first to post one!
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics

Receive email when someone replies.