0

My Java code is,

    Document doc=new Document();
    PdfWriter.getInstance(doc, response.getOutputStream());
    doc.open();
    doc.add( new Paragraph( "Hello, World!" ) );
    doc.close();
    file.close();

With this code, I can able to generate the PDF file with the content "Hello, World!" on the fly.

Now, I have one external JAR to return me an document (com.lowagie.text.Document). I need to generate this document on the fly. I have tried the following,

    Document doc=new Document();
    PdfWriter.getInstance(doc, response.getOutputStream());
    doc.open();
    doc=method(); // returns me an pdf document
    doc.close();
    file.close();

It doesn't generated a document on the fly. This is because I have opened a document but not added the content.

My question is, how can I generate that document on the fly?

Also, I would like to add another try of mine. Code of method() is,

    private Document method()throws DocumentException
    {
    Document doc1=new Document();
    doc1.open();
    doc1.add(new Paragraph("Success"));
    doc1.close();
    return doc1;        
    }

If I am passing document object in to that method it is working. Code is,

    method(doc);
    private void method(Document doc1)throws DocumentException
    {
    doc1.add(new Paragraph("Success")); 
    }

Is there anything that I am missing in my non-working code?

  • 1
    Can you share the code of `method()`? – fvu May 08 '16 at 09:11
  • Sorry, I have no access to that Jar. But, it is returning an document in the type com.lowagie.text.Document. – Kalyan Sundharam May 08 '16 at 09:14
  • 1
    Please take a look at my name. I am the Lowagie you mention when you say `com.lowagie.text.Document`. You should read this warning: [Can iText 2.1.7 or earlier be used commercially?](http://developers.itextpdf.com/question/can-itext-217-or-earlier-be-used-commercially) The answer is **No, you can't.** I was in Bangalore two weeks ago, explaining companies like your employer that their developers should stop using `com.lowagie` classes, and start using `com.itextpdf` classes. – Bruno Lowagie May 08 '16 at 10:01
  • As for your question, you should read [How can I serve a PDF to a browser without storing a file on the server side?](http://developers.itextpdf.com/question/how-can-i-serve-pdf-browser-without-storing-file-server-side) It is custom that you read the official documentation when using a software library. The official documentation for iText can be found here: [developers' site](http://developers.itextpdf.com/), Just look for the keyword [Servlet](http://developers.itextpdf.com/search/node/Servlet) – Bruno Lowagie May 08 '16 at 10:02
  • By the way: `doc=method(); // returns me an pdf document` is wrong. You've already created a `Document` instance when you write this line: `Document doc=new Document();` That `doc` instance listens to an unnamed `PdfWriter` instance. You can't just *replace* `doc` and hope that it will magically work. It's very clear that you never looked at the [documentation](http://developers.itextpdf.com/). – Bruno Lowagie May 08 '16 at 10:06
  • That doc instance listens to an unnamed PdfWriter instance. You can't just replace doc and hope that it will magically work. Can you please briefly explain your statement? I can't get it – Kalyan Sundharam May 11 '16 at 08:51

0 Answers0