This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Java code printing junk characters in PDF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java code printing junk characters in PDF" Watch "Java code printing junk characters in PDF" New topic
Author

Java code printing junk characters in PDF

Suhel Sayyed
Greenhorn

Joined: Nov 03, 2011
Posts: 8
Hi,

I have a JAVA code that reads a PDF file from remote server. It then merges it with an existing PDF file and print the resultant. However, sometimes I observe that the PDF printed contains gibbersih data. It doesn't happen always. Is it some issue with the fonts used in PDF? Or some inherent issue of Java, while printing PDF files.

I use a print job object to print PDF file. Logic for printing the PDF file is as following

private boolean printDocument(String fileName){
System.out.println("printComplianceDocument : Begin");
boolean result = false;
PDDocument document = null;
PDPage page = new PDPage();

try {
document = PDDocument.load(fileName);
PrinterJob pjob = PrinterJob.getPrinterJob();

PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(Sides.DUPLEX);
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attr_set);
for(PrintService service : services){
if (service.getName().equalsIgnoreCase(SystemProp.getInstance().getWorkStation().getDfltRptPrinter().getAddress().trim())){
System.out.println("Printing Document to printer: " + service.getName());
pjob.setPrintService(service);
pjob.setPageable(document);
pjob.print(attr_set);
result = true;
break;
}
}

//Try printing to Default Printer if appropriate Print Service is not found
if(!result){
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("Printing Document to Default Printer: " + defaultPrintService.getName());
pjob.setPrintService(defaultPrintService);
pjob.setPageable(document);
pjob.print(attr_set);
result = true;
}

} catch (IOException e) {
// Exception
} catch (PrinterException e) {
// Exception
}
System.out.println("printDocument : End");
return result;
}

Please share your ideas and suggest solutions if you could. Thanks
Suhel Sayyed
Greenhorn

Joined: Nov 03, 2011
Posts: 8
I have also verified the PDF kept on remote server, they are good.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java code printing junk characters in PDF
 
Similar Threads
Java Print Service API!!!(File not printed)
Java Print
how to print a word document in java?
Java Print Dialog
Event Listeners in Java Print Service API