3

Hi everyone this is my first question at the site .

i use : NetBeans IDE 7.0.1 and i want to print a PDF with java , but i dont know where is the mistake in my code , please help me .

there is a good code which one print a GIF used the 5-th printer.


PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);


if (pss.length == 0)  throw new    RuntimeException("No printer services available.");
    PrintService ps = pss[5];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("a.gif");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

job.print(doc, pras);
fin.close();  

But i dont know whats wrong the following code which try to print a PDF at the same printer(5th)


PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PDF, pras);


if (pss.length == 0)  throw new    RuntimeException("No printer services available.");
    PrintService ps = pss[5];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("test.PDF");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.PDF, null);

job.print(doc, pras);
fin.close(); 

this is the program answer : " Exception in thread "main" java.lang.RuntimeException: No printer services available. "

If you have any idea please write it Thanks

Karoly Nagy
  • 33
  • 1
  • 3
  • It seems to be saying that you don't have a default printer on your system to print in PDF. Have you had a look at this: http://stackoverflow.com/questions/7355025/create-pdf-with-java – assylias Feb 27 '12 at 15:04
  • unfortunately , i have a default printer ,so the problem is not that – Karoly Nagy Feb 27 '12 at 15:27
  • Does your default printer support a PDF stream as direct input? because it seems to me that this is what you are trying to do... – yms Feb 27 '12 at 15:37
  • Thanks all answers the solution is that my printer cant print PDF – Karoly Nagy Feb 28 '12 at 08:27

1 Answers1

0

You can't generally print a PDF directly from Java. Iwrote a blog article about ways to do it at http://www.jpedal.org/PDFblog/2010/01/printing-pdf-files-from-java/

mark stephens
  • 3,205
  • 16
  • 19