0

I have tried to unlock pdf reader but still got bad user password

Exception in thread "main" com.lowagie.text.exceptions.BadPasswordException: Bad user password
    at com.lowagie.text.pdf.PdfReader.readPdf(PdfReader.java:611)
    at com.lowagie.text.pdf.PdfReader.<init>(PdfReader.java:214)
    at com.lowagie.text.pdf.PdfReader.<init>(PdfReader.java:198)

Here the code

public class Test {
    public static void main(String[] args) throws IOException {
        List<String> test = new ArrayList<>();
        File path = new File("encryptPdf.pdf");
      
        byte[] byteArr = fileToByteArray(path);
        PdfReader pdfReader = new PdfReader(byteArr);
        pdfReader = unlockPdf(pdfReader1);
    }

    public static byte[] fileToByteArray(File file) throws IOException {
        FileInputStream fl = new FileInputStream(file);
        byte[] arr = new byte[(int)file.length()];
        fl.read(arr);
        fl.close();
        return arr;
    }

    public static PdfReader unlockPdf(PdfReader reader) {
        if (reader == null) {
            return reader;
        }
        try {
            Field f = reader.getClass().getDeclaredField("encrypted");
            f.setAccessible(true);
            f.set(reader, false);
        } catch (Exception e) {
            System.out.println("exception");
        }
        return reader;
    }
}

I am using opendpdf library version 1.0.5 (https://github.com/LibrePDF/OpenPDF)

Environment : Java 11

I tried above code for unencrypted pdf its working as expected but for encrypted pdf keep showing above error even thought i have set accessible to true

Anyone is facing similar problem ? any suggestion to fix this issue ?

Fairuz Yassar
  • 129
  • 3
  • 11
  • Don't read the file into a byte array first: **a.** it's quite unnecessary - there are plenty of ctors which will read a file on construction and (more importantly) **b.** the way you load the byte array does *not* guarantee that it will be filled fully. btw, that stack trace was *not* produced by the above code so I'm wondering if that's your *actual* code..? – g00se Sep 27 '22 at 12:01
  • i am debuging on legacy merge pdf code (has parameter byte[]) because there is usecase to merge encrypted pdf but pdf reader keep showing above error when tried to read btye array from encrypted pdf – Fairuz Yassar Sep 27 '22 at 13:18
  • Yes but you're not merging anything. You're reading from a file. You should probably post a link to a sample source file – g00se Sep 27 '22 at 13:20
  • yes i am not showing merging file because the actual problem is not merging itself but when read byte array from encrypted pdf – Fairuz Yassar Sep 27 '22 at 13:22
  • if you wondering how merging code look like you can check this question https://stackoverflow.com/questions/72723546/pdf-file-generated-by-pdfwriter-is-showing-blank-page – Fairuz Yassar Sep 27 '22 at 13:23
  • That's OK but the point stands - your *are* reading into a byte array but are not doing it correctly. Now that might *not* be the reason for your problem, but should be eliminated by doing it correctly – g00se Sep 27 '22 at 13:28
  • I have tried to merge two non encrypted pdf, code working as expected – Fairuz Yassar Sep 27 '22 at 13:32
  • 1
    you really need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) No merging, please. No byte arrays. Just direct from file. You've already stated yourself: *the actual problem is not merging itself...* – g00se Sep 27 '22 at 13:35
  • the problem is when above code is reading unencrypted document its working but not for encrypted document – Fairuz Yassar Sep 27 '22 at 13:41

0 Answers0