I am trying to use iText 2.1.7 in my application. I nailed down by problem to the following piece of code:
InputStream inputFile = ...
OutputStream outputStream = ...
PdfReader pdfReader = new PdfReader(fis);
PdfStamper pdfStamper = new PdfStamper(pdfReader, os, '0', true);
pdfStamper.close();
pdfReader.close();
outputStream .close();
This code yields broken pdf (error 118) when as input I use this pdf (this is polish taxation form). Do you know if it there is some easy work-around for this problem? I know that corresponding code in c#/iTextSharp works.
Edit: This is not a problem with encryption. I have already solved this one.
As mentioned as a comment, the document in question is encrypted to restrict permissions. The code from the official iText 2.1.7 jar will, therefore, throw a BadPasswordException when instantiating the PdfStamper because the correct password was not supplied. Thus, it does not produce a broken PDF and so the issue here is not a bug. 😉
That being said it of course is possible to coerce iText 2.1.7 into ignoring the fact that the password was not supplied. In that case the program (having corrected the variable names where necessary) indeed produces a PDF which Adobe Acrobat refuses to open showing an error code 118.
Doing the same with the current iText 5.x, on the other hand, produces a document which Adobe Acrobat opens without any objection.
If one compares those two outputs, the problem becomes clear. While in case of iText 2.1.7 the updated info dictionary looks like this:
it looks like this with the current iText 5.x:
Thus, the iText 2.1.7 did not encrypt the entries of the updated info dictionary, and Adobe Acrobat hickuped while trying to decrypt it.
EDIT
Unfortunately, even after spacing away the unencrypted info dictionary, there is the 118 when trying to load the file. Thus looking at the files again there is another difference: the version created by iText 2.1.7 contains a tiny object stream while the other version does not. iText itself stumbles when trying to read that object stream…