Reputation: 1097
I want to print test3.doc file. I have tried this with java.awt.print.PrinterJob; Here is the code I have tried:
PrinterJob printerjob= PrinterJob.getPrinterJob();
PageFormat pageformat=new PageFormat();
Paper paper=new Paper();
paper.setSize(8.27, 11.69);
pageformat.setPaper(paper);
printerjob.defaultPage(pageformat);
text.setText(printerjob.getUserName());
printerjob.pageDialog(pageformat);
printerjob.printDialog();
String file="C:/test3.doc";
printerjob.setJobName(file);
try{
printerjob.print();
text.setText("success");
}
catch (PrinterException e){text.setText("error");}
But it does not print any file.Does anyone have any idea why does not it work? Or how can I change code in order to print test3.doc file.
Upvotes: 1
Views: 926
Reputation: 2319
As far as I understand the Java API docs, setJobName(String)
sets the name of the document to be printed, but this does not reference the document but instead it's just a name for the job, which can be anything like "MyPrintJobName".
When printing a document, this document needs to be rendered and the resulting "graphics" is then sent to the printer as described here: http://docs.oracle.com/javase/tutorial/2d/printing/printable.html
So, to print a .doc file from pure Java code, you need an engine which is able to render the document accordingly. Having a look at similar questions, there does not seem to be such an engine provided by Oracle with Java so you need to use a third-party component for handling .doc files.
Upvotes: 1