0

I have an InputStream. I am trying to read the InputStream and write it to a pdf file using iText.

I tried the following :

FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fileout);

InputStream is=null;
is = myInfo.getInputStream();


String result = IOUtils.toString(is, "UTF-8");
Paragraph paragraph=new Paragraph();
paragraph.add(result);
document.add(paragraph);

is.close();
document.close();

Here the output file contains so many unwanted characters, some XML tags etc. The output pdf file is dumped with a lot of things which are non-readable.

And I am able to write it to pdf file using the following code:

OutputStream ostream = new FileOutputStream("c:\\test\\newfile1.pdf");
byte[] data = new byte[4096];
            int r = 0;
            while((r = is.read(data, 0, data.length)) != -1)
            {
                ostream.write(data, 0, r);
             }
ostream.flush();
ostream.close();

The above code helps me to write the inputstream to the pdf file. But I want to do the same thing with itext.

I am using iText for the first time and confused how to use it properly. Could someone please help me with this? Thanks.


Solution :

I changed the inputstream to bytearraay.

PdfReader pdfreader;
pdfreader = new PdfReader(myInfo.getByteArray());
PdfStamper pdfStamper = new PdfStamper(pdfreader, fileout);
pdfStamper.close();
pdfreader.close();

In this way, I am able to read the inputsteam and write to pdf using itext. Thanks everyone.

Mukund S
  • 31
  • 2
  • 9
  • What the code is doing is reading a UTF-8 text, and adding it to a PDF document object as paragraph. Save the text first as `.txt` without itext, and look what you got. (The calls to close were not listed in your question, but I assume they are in the original code.) – Joop Eggen Aug 20 '18 at 16:29
  • Yes I have is.close() and document.close() in my code and have also updated here. I want the code to generate pdf file. so why to save it as .txt ? – Mukund S Aug 20 '18 at 16:41
  • Garbage in, garbage out. If your original file is full of unwanted characters, xml tags, etc., and you write these unwanted characters and xml tags to the PDF file, why would they disappear? What is the ofiginal file? What does it contain? – JB Nizet Aug 20 '18 at 16:47
  • The original file is just a letter which contains details like name,address, content of the letter. I am able to write it to pdf if I use FileOutputStream but I am not able to do the same thing using itext. – Mukund S Aug 20 '18 at 16:51
  • So, the original file is already a PDF file? Not a text file? Then why are you reading it as text? And what's the point in using iText to generate a PDF file if what you have is already a PDF file? What are you, precisely, trying to achieve? – JB Nizet Aug 20 '18 at 16:53
  • Thanks everyone. What I did was I changed the inputstream to bytearray and read using pdfReader. Now it got fixed. – Mukund S Aug 20 '18 at 17:41
  • You haven't answered the questions you were asked. What you did was basically the equivalent of taking an already assembled ikea funiture, dismantle it, and assemble it again. If you already have the bytes of the PDF you want in the InputStream, just write those bytes, as is, to a file. No need for iText. And that will of course be much faster. – JB Nizet Aug 20 '18 at 17:51
  • I am using itext because I need to add extra things to the pdf like bookmarks,additional page etc. – Mukund S Aug 20 '18 at 18:20
  • And could you please look into the following question and provide me suggestions? https://stackoverflow.com/questions/51936626/itext-read-pdfs-in-a-loop-and-merge-one-by-one – Mukund S Aug 20 '18 at 18:32

0 Answers0