How to print PDF files using java print API

Hi,I was goign throw lot of discusion and reading lot of forums related to print pdf files using java api. but nothing seems to be working for me. Can any one tell me how to print pdf files using java api.Thanks in advance
[250 byte] By [ jayakumarsin] at [2007-9-30 9:04:58]
# 1
You can use PDF Writer printer, to print to PDFs. There are many free ones available, to name a few; CutePDF, PDF Factory, PDF995 etc..If you use any of the above, all you to have to do is, prepare your document to print in Java and then simply print to this printer.
dasanshuman at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 2

I think, the question is "How do I *print* PDF files using Java (e.g. java.print)" - not "How do I *create* PDF files" or "How do I print *to a PDF* file",

So - what's the answer ? I have the same problem - can create PDF files with Java, but just can print graphic or text files .... please help!

Thanx!

EyeQ at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 3
Are you all from English-speaking countries?You sentense is very interesting,is tongue or writing language?
liuxianguo at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 4

Just quote the above answer:

prepare your document to print in Java and then simply print to this printer.

I think what that means is that you print the thing to the printer, and the output would be paper. The assumption is that the printer will take whatever pdf stream, understand them and just print them.

vy_ho at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 5
Use Smart JPrint that allows you to (i) Print, (ii) PDF generate, (iii) TIFF, (iv) JPEG, (v) PNG generation from both Java client and server programs.Try the online demo at: http://www.activetree.com
jatima at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 6

Hello,

Take a look at http://www.cs.wisc.edu/~ghost/gsview.

The GSview can view/print PostScript and PDF file to your windows printer. For non-Windows environment, like Linux, HP-UX, Solaris, pre-compiled binaries is available too.

Note: The GhostScript is pre-requisite to GSView.

I hope this can help you.

RubensUmeda at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 7
I've gotten PDFs to print using cups. Just set the DocFlavor to be DocFlavor.BYTE_ARRAY.AUTOSENSEHowever, it is ignoring the attributes related to the paper size (MediaSizeName and MediaTray). Has anyone gotten PDFs to print on different paper sizes yet?Thanks,Tom
tel at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 8

I tried the AUTOSENSE DovFlavor and it seems to work. But, the fonts in my doc doesn't match. I guess the printer tries to match the fonts in the document to what fonts it has installed.

PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();

DocPrintJob printerJob = defaultPrintService.createPrintJob();

File pdfFile = new File("myPdfFile.pdf");

SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(), DocFlavor.URL.AUTOSENSE, null);

printerJob.print(simpleDoc, null);

/ Jrgen

jorgen at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 9
if you are sure that u r printing pdf then instead of DocFlavor.URL.AUTOSENSE you can use DocFlavor.INPUT_STREAM.PDF Which will print the PDF
ManjunathaReddy at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 10

I use the code posted for this topic to print pdf:

PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();

DocPrintJob printerJob = defaultPrintService.createPrintJob();

File pdfFile = new File("myPdfFile.pdf");

SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(), DocFlavor.URL.AUTOSENSE, null);

printerJob.print(simpleDoc, null);

Pdf is not printed, the following sentence is printed on the paper:

Pdf Print failed, in order to enable the print job, 128 M memory is needed.

Actually, my computer memory is very big. Any idea?

Jennifer_Guo at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 11

> Pdf is not printed, the following sentence is printed

> on the paper:

> Pdf Print failed, in order to enable the print job,

> 128 M memory is needed.

>

> Actually, my computer memory is very big. Any idea?

It's probably the printer that needs 128M.

/ Jrgen

jorgen at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 12

If printer need 128M memory, this means I need to find specific printer to do the test. I want to know whether you really use java print api to print pdf successfully. If yes, what kind of printer did you use? The thing is that I research a lot related to this topic, lots of people mentioned java print api still not support pdf printing. So I'm surprised that you can print pdf successfully.

Jennifer_Guo at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 13

hi when i am trying to use DocFlavor.BYTE_ARRAY.AUTOSENSE;

i am getting following exception

java.lang.IllegalArgumentException: data is not of declared type

at javax.print.SimpleDoc.<init>(SimpleDoc.java:82)

at com.print.PrintDocument.main(PrintDocument.java:41)

can you please help me out

Thanks in advance

Regards

Ramu

P.Ramu at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 14

Hello

i try the same code that u got by someone for printing

code is

PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();

DocPrintJob printerJob = defaultPrintService.createPrintJob();

File pdfFile = new File("c:\\temp1.pdf");

SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(), DocFlavor.URL.AUTOSENSE, null);

printerJob.print(simpleDoc, null);

code will print notprintable character

that means pdf printting is not supported by this code

so did u get any other solution reply me soon

jignesh.tandel at 2007-7-2 21:26:33 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 15

The DocFlavor is just for detecting if a printer supports a flavor. So if you have a printer that can directly print PDF code (that I not know of), you could directly print PDF files with the Java print API. But most printers just understand their own code, PCL, many also PostScript and perhaps some other bitmap protocol.

We use GhostScript to print our PDF files through Java (not GSView, but directly GhostScript). GhostScript transforms the PDF to PostScript as PDF is very similar to PostScript (but not the same).

MartinHilpert at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 16
HP laserjet 9050 does have PDF Direct support and works very well when you send PDF files from Java Print Service API. There are also bunch of "PDF Direct" DIMMS available for current and older HP Models.
...ilA at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 17

Hi there

We develop a (commercial) PDF library which enables printing PDFs from Java (amongst other things). You can get more information and a free trial at http://big.faceless.org/products/pdf - there's an example included called "PrintPDF.java" which should get you started.

Cheers... Mike

MikeBremford at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 18

Mike,

Can't seem to get hold of the example described in your reply below. If you could let us have the URL to get then it would be great.

My GUI application creates a pdf document which I need to print. I want to achieve this using the standard Java class PrinterJob (no 3rd party APIs I'm afraid, commercial restraints etc ..). I had a stab at it using the following code. When executed I get the pretty printer dialog then when I click ok to print, nothing happens!

boolean showPrintDialog=true;

PrinterJob printJob = PrinterJob.getPrinterJob ();

printJob.setJobName ("Contract.pdf");

try {

if (showPrintDialog) {

if (printJob.printDialog()) {

printJob.print();

}

}

else

printJob.print ();

} catch (Exception PrintException) {

PrintException.printStackTrace();

}

Thank you and a happy new year.

Cheers,

Chris

Zarbster at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 19
@ Jrgen (jorgen99), Thank you - your sample code works like a charm with simple PDF files! Exactly what I was looking for :)Cheers,Eric
munyul at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 20
Eric,I'm glad my sample code worked for you BUT I still can't get it to work. Have you modified/corrected it? If so could you post us your new version please?Ta,Chris
Zarbster at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...
# 21
hi there.i don't know why, but mine program tells me thatPrintService defaultPrintService = PrintService.lookupDefaultPrintService(); funciont lookupdefaultprintservince is not found... what can be possible reasons for that?
isolated at 2007-7-2 21:26:35 > top of Java-index,Other Topics,Java Community Process (JCP) Program...