Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Printing PDF in Background

 
KR Gol
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to write a program that prints PDF files in a directory without asking user for Print Dialog, Background printing.

I used, DocFlavor URL.PDF. But it is simply printing gibberish and repeating the printing forever...
However, a text document is working fine.
I don't see any MIME types application/pdf fromt he list of flavors supported from my printer. However, if I print from ACrobat Reader it works as we all know.

Any help???!!!

Here is the code:
private void doPrint(String pdfFile) {
FileInputStream psStream = null;
try {
psStream = new FileInputStream(pdfFile);
} catch (FileNotFoundException ffne) {
}
if (psStream == null) {
return;
}
URL fileURL = null;
try {
fileURL = new File(pdfFile).toURL();
System.out.println(fileURL);
}catch(MalformedURLException mue) {
mue.printStackTrace();
}

DocFlavor psInFormat = DocFlavor.INPUT_STREAM.PDF;
DocFlavor urlFileFlavor = DocFlavor.URL.PCL;

Doc myDoc = new SimpleDoc(fileURL, urlFileFlavor, null);

PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();

aset.add(new Copies(1));
aset.add(MediaSizeName.EXECUTIVE);

//aset.add(Sides.DUPLEX);
//PrintService[] services =
//PrintServiceLookup.lookupPrintServices(psInFormat, aset);
PrintService services =
PrintServiceLookup.lookupDefaultPrintService();
if (services != null) {
System.out.println("Service :"+services.getName());
System.out.println("Is this supported:"+services.isDocFlavorSupported(psInFormat));
getSupportedPrintFlavors(services);
this.getSupportedPrintAttributes(services);
DocPrintJob job = services.createPrintJob();
try {
job.print(myDoc, aset);
//throw new PrintException("IGNORE THIS!");

} catch (PrintException pe) {
pe.printStackTrace();
}

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic