aspose file tools*
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java code printing junk characters in PDF