File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes To print pdf file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "To print pdf file" Watch "To print pdf file" New topic

To print pdf file

srikanth pidikiti

Joined: Apr 14, 2006
Posts: 12
import javax.print.*;
import javax.print.attribute.*;

public class Printing {
public static void main(String args[]) throws Exception {
String filename = args[0];
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);

The Above program gives the error whenever am printing pdf
for other files like png,gif it will execute sucessfully

plz rectify my error or else send me the code to print the pdf file as soon as possible

advance thanks,
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This does not belong in the SCJD forum...
Moving to Java in General (Intermediate)

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
The Above program gives the error whenever am printing pdf

What is the text of the error? Which line in the code throws it?

Java error/exception messages can be very informative, so when you get one, copy the text so you can examine it at leisure and post it when asking such questions.

Joe Ess

Joined: Oct 29, 2001
Posts: 9189

I'm looking into my crystal ball and seeing an unsupported flavor exception. I believe DocFlavor is intended to indicate the capabilities of the printer, not the content of the document. Try using DocFlavor.INPUT_STREAM.AUTOSENSE.
[ June 14, 2006: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: To print pdf file
It's not a secret anymore!