0

I am trying to create a PDF on Android but only to show some information when I press a button, not to store it on the mobile phone. I am getting this error:

Unhandled exception: com.itextpdf.text.DocumentException

but I do not understand why it happens. I have the following code:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfDocument pdf = new PdfDocument();

PdfWriter pdfWriter = PdfWriter.getInstance(pdf, baos); //Error here
pdf.open();
pdf.add(new Paragraph("Hello world")); //Error here
pdf.close();

byte[] pdfByteArray = baos.toByteArray();

Why I am getting this error? Am I using the itextg library incorrectly? I could not find any information about this error.

P.S.: I could see that the error is related with itext instead of itextg so I do not know if the error can be produced with this fact.

Thanks in advance!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

1 Answers1

1

This is wrong:

PdfDocument pdf = new PdfDocument();

In iText 5, PdfDocument is a class to be used internally by iText only. You are supposed to use the Document class instead.

Adapt your code like this:

try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Document document = new Document();
    PdfWriter pdfWriter = PdfWriter.getInstance(document, baos); //Error here
    document.open();
    document.add(new Paragraph("Hello world")); //Error here
    document.close();
    byte[] pdfByteArray = baos.toByteArray();
}
catch (DocumentException de) {
    // handle the exception when something goes wrong on the iText level.
    // for instance: you add one element to another element that is incompatible
}
catch (IOException) {
    // handle the exception when something goes wrong on the IO level.
    // for instance: you try to write to a file in a folder that doesn't exist
}

Please read the documentation carefully before starting to experiment on your own. You can find a Hello World example in the Getting Started section of the Q&As.

The actual problem is caused by the fact that you don't have a try/catch (or a throws) that deals with an IOException or a DocumentException.

Your error is totally unrelated to the difference between iText (Java) and iTextG (Android). You are using methods that throw exceptions. You need to handle those exceptions, regardless of whether you're working in Java or Android.

There is very little difference between iText and iTextG. There is no reason whatsoever to have separate iText and iTextG documentation.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you very much! I went to the documentation but in the wrong place I think. Here is where I saw the [example](http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/examples/chapter-1) I was following to create the PDF. – Francisco Romero Aug 08 '16 at 07:51
  • 1
    You were looking at documentation for iText 7 (as seen in the comment at the top of the code), which has a different API from iText(G) 5. – Amedee Van Gasse Aug 08 '16 at 08:09
  • @AmedeeVanGasse Thank you for the clarification. I thought that it was better to use the last version, it is why I got confused. Again, thanks! – Francisco Romero Aug 08 '16 at 09:08
  • 2
    iText 7 is a complete rewrite, it is essentially an entirely different product. It's not a backwards compatible upgrade. – Amedee Van Gasse Aug 08 '16 at 09:10
  • @AmedeeVanGasse Thank you very much again for your clarification :) – Francisco Romero Aug 08 '16 at 09:49