pdf - Is there a way to get this "send to printer" Java method to work? -


public static void sendpdftoprinter(string epsilon) {     fileinputstream psstream = null;     try {         psstream = new fileinputstream(epsilon);         } catch (filenotfoundexception ffne) {           ffne.printstacktrace();         }         if (psstream == null) {             return;         }     docflavor psinformat = docflavor.input_stream.autosense;     doc mydoc = new simpledoc(psstream, psinformat, null);       printrequestattributeset aset = new hashprintrequestattributeset();     printservice[] services = printservicelookup.lookupprintservices(psinformat, aset);      // step necessary because have several printers configured     printservice myprinter = null;     (int = 0; < services.length; i++)     {         string svcname = services[i].tostring();                   system.out.println("service found: "+ svcname);          if (svcname.contains("series"))         {             myprinter = services[i];             system.out.println("my printer found: "+svcname);             break;         }     }      if (myprinter != null) {                     docprintjob job = myprinter.createprintjob();         try {         job.print(mydoc, aset);          } catch (exception pe) {pe.printstacktrace();}     } else {         system.out.println("no printer services found");     } }  public static void main(string[] args) throws ioexception {     //@suppresswarnings("unused")     //testing t = new testing();     string dest = ("c:/users/brian/desktop/so046201r-17/testingalpha/6fs-2m.pdf");     sendpdftoprinter(dest); } 

i'm writing program that'll write text series of pdf files , send them printer. i've got writing pdf part done, whenever try passing file "sendpdftoprinter" method encounter problems. far i've tested hp deskjet printer , canon inkjet printer no success (former gave "java document error" message , latter wouldn't add file queue). suppose question boils down this:

is problem code or printers i'm using? there workaround?

using mark's edit:

public static void sendpdftoprinter(string epsilon) throws  invalidpasswordexception, ioexception {     fileinputstream psstream = null;     try {         psstream = new fileinputstream(epsilon);         } catch (filenotfoundexception ffne) {           ffne.printstacktrace();         }         if (psstream == null) {             return;         }     docflavor psinformat = docflavor.input_stream.autosense;     pddocument mydoc = pddocument.load(new file(epsilon));       printrequestattributeset aset = new hashprintrequestattributeset();     printservice[] services =      printservicelookup.lookupprintservices(psinformat, aset);      // step necessary because have several printers configured     printservice myprinter = null;     (int = 0; < services.length; i++)     {         string svcname = services[i].tostring();                   system.out.println("service found: "+ svcname);          if (svcname.contains("series"))         {             myprinter = services[i];             system.out.println("my printer found: "+svcname);             break;         }     }      if (myprinter != null) {                     printerjob job = printerjob.getprinterjob();          try {             job.setprintservice(myprinter);             job.setpageable(new pdfpageable(mydoc));             job.print();         } catch (printerexception e) {             // handle exception.         }     } else {         system.out.println("no printer services found");     }  } 

this may because printers don't natively support pdfs. managed print pdf code, printed if pdf file opened text file.

one solution use apache's pdf library java: pdfbox. file can loaded so:

pddocument mydoc = pddocument.load(new file(epsilon)); 

to print file, use printerjob instead of docprintjob:

printerjob job = printerjob.getprinterjob();  try {     job.setprintservice(myprinter);     job.setpageable(new pdfpageable(mydoc));     job.print(); } catch (printerexception e) {     // handle exception. } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -