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 – including ours! 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.
Latest posts by Mark Stephens (see all)
- Saving your settings in our online PDF to HTML5 and SVG converter - May 20, 2013
- PDF teasers – how would you handle this stack problem? - May 15, 2013
- Where do your PDF objects start in a PDF file? - May 8, 2013
- Version 5 release – Swing and javaFX - April 26, 2013
- Which languages should have examples when documenting a web service? - April 24, 2013

Dear Jpedal supports:
Nice to know you and your product, as i read an article from your pdfblog about
there are 3 ways to print pdf files
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();
fist question:
we have an oce brand cutsheet printer , i think it can directly support pdf printing because this printer has PostScript License and PCL license, and i can use windows lpr command to send a postScript to this printer to print so is this mean this printer directly support pdf printing? if no how i can make sure my printer directly support pdf print.
second question:
if my printer directly support pdf printing is that mean i can use this script to print pdf?
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();
if yes, i write a small script to send pdf to my printer as fellows:
File file=new File(“D:\\t22.pdf”);
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
InputStream in= new FileInputStream(file);
PrintService printService1[]= PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,printService1, defaultService, flavor, pras);
pras.add(new Copies(3));
pras.add(PrintQuality.HIGH);
pras.add(Sides.DUPLEX);
pras.add(new JobName(“E2″,null));
Doc doc1=new SimpleDoc(in,flavor,null);
pj.print(doc1, pras);
in.close();
i can send the pdf to the printer and print out , but the PrintRequestAttribute seems not work, i set 3 copies but just print 1 copy and i set duplex but always simplex but the printer can get the JobName “E2″, so i don’t know why, what’s maybe the reason?
third question:
as said from your blog “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” say “all work” , what work is done by the printer?
————————————————
4 question:
i installed the oce printer driver to send files, but in oce printer driver all print job are need give a mediaName for example “t3″ , and in the printer there are 4 trays,and for example the first tray are assigned mediaName “t1″ and second tray are assigned mediaName “t2″
the thrid tray are assigned mediaName “t3″ the fourth tray is assigned mediaName “t4″ , so if i send a file with printer driver and give mediaName “t3″, the job will print with tray 3, as i found “javax.print.attribute.standard.MediaName” can this class
meet this requirement?
hope you can give me some help. it would be better if you can reply to my email address
Best Regards
Eric
You will need to check with the manufacturer what support they actually have.
Dear mark Stephens:
First thanks your reply.
from your reply said i need check with printer manufacturer right?
And is it means that even thougth i do the DocAttribute , printJobAttribute, and printRequestAttribute settings like the copies , mediaName , mediaSize , all these methods maybe not valid for some brand printer drivers right?
Thanks
Eric