0

I have a generated document using iText library in java using the next code:

        Document document = new Document();
        document.open();

I manipulate the document with adding tables and data to it. Now i want to print the document after all; I found a way to send the document to the printer using the following code, but with using Input stream:

        InputStream inputStream = new FileInputStream("C://Housing Report(1).pdf");
         Doc doc = new SimpleDoc(inputStream, 
         DocFlavor.INPUT_STREAM.AUTOSENSE,null);

         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();  
         PrintService services = 
         PrintServiceLookup.lookupDefaultPrintService();  


         DocPrintJob job = services.createPrintJob();  
         try {  
         job.print(doc, aset);  

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

}  

My question is how can I print the iText document without having to save it and using InputStream to print it again? Thanks in advance.

  • You cannot print the *"iText `document`"* because it is a mere facade to add content to which then is forwarded to listeners which add soon as possible serialize data away to their respective output streams. What you can do, though, is use a `ByteArrayOutputStream` there to keep the pdf in memory, and feed the underlying byte array to your print service. – mkl May 06 '18 at 17:17
  • @mkl Thank you for clarifying it out. I will try to make it work that way. Thanks again! – Mohammed Amer May 07 '18 at 09:34

0 Answers0