2

I'm getting the following exception when excecuting this code:

public byte[] watermarking(byte[] orig) throws IOException {
        PdfReader pdfReader = new PdfReader(orig);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfStamper pdfStamper = null;

        try {
            pdfStamper = new PdfStamper(pdfReader, baos); //exc here
            ...
            }
            ...
        } catch (DocumentException var8) {
            ...
        }
    }

Exception:

11:43:11,094 ERROR [de.mlp.xbg.pa.rest.SessionRR] (http-/127.0.0.1:8081-6) PdfReader not opened with owner password: java.lang.IllegalArgumentException: PdfReader not opened with owner password

I checked other threads regarding this topic and it seems that the easiest solution is to add PdfReader.unethicalreading = true;

However, I'm forced to use iText 2.1.7 or older (com.lowagie iText) and not iText 5.0.0 or newer (com.itextpdf iText). PdfReader.unethicalreading does not exist in the old version of the library.

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
0x44656E6E79
  • 1,053
  • 3
  • 14
  • 21
  • 1
    Why are you forced to use an ancient and unmaintained version of iText that doesn't contain the feature you need? – Amedee Van Gasse Mar 23 '16 at 11:31
  • 3
    If you look at the source code (https://github.com/itext/itextpdf/blob/develop/itext/src/main/java/com/itextpdf/text/pdf/PdfReader.java) then you will see that `unethicalreading` was added in iText 5.0.2 _(`com.itextpdf` iText)_. More precisely, in this commit: https://github.com/itext/itextpdf/commit/c27c608dee1495b91dc290653b1f387d7c09adde on Mar 31, 2010. Which means you are using a version of iText that is at least 6 years old. – Amedee Van Gasse Mar 23 '16 at 15:34

1 Answers1

8

Here there seems to be a workaround to make iText to ignore password with a disclaimer:

I leave legal issues up to you by executing the code below.

public static PdfReader unlockPdf(PdfReader reader) {
    if (reader == null) {
        return reader;
    }
    try {
        java.lang.reflect.Field f = reader.getClass().getDeclaredField("encrypted");
        f.setAccessible(true);
        f.set(reader, false);
    } catch (Exception e) { /* ignore */ }
    return reader;
}
Gerrit Griebel
  • 405
  • 3
  • 10
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109