jmanm / PDFsharp-netstandard2.0

PDFSharp for .NET Standard 2.0
MIT License
17 stars 4 forks source link

Cannot open secure PDF's created by Adobe Acrobat #6

Open jmanm opened 5 years ago

jmanm commented 5 years ago

Tested using Acrobat Pro DC version 2019.010.20069.

Steps to reproduce:

var doc = PdfReader.Open("<path to secure pdf>", "<open password>");
//throws PdfSharp.SharpZipLib.SharpZipBaseException:  Header checksum illegal
itsunbelievable commented 5 years ago

Hi, by any chance have you found some solution for this issue? Thanks

jmanm commented 5 years ago

No I have not. I haven't looked at it in a few months, but so far I've tried:

But no luck.

sisve commented 2 years ago

I have managed to track this down. This comes from PdfDictionary.TryUnfilter() which tries to call Filtering.Decode(...) where FlateDecode tries to use the embedded SharpZipLib to decompress the stream. However, the value is still encrypted, and needs to be decrypted. Catching the exception and decrypting and retrying works for me.

Old code: https://github.com/jmanm/PDFsharp-netstandard2.0/blob/51e048bef7e8e0489cbd6a172ae9b59c2412c51b/src/PdfSharp/Pdf/PdfDictionary.cs#L1684

New code:

byte[] bytes;

try {
    bytes = Filtering.Decode(_value, filter);
} catch (SharpZipBaseException) {
    _ownerDictionary._document.SecurityHandler.DecryptObject(_ownerDictionary);
    bytes = Filtering.Decode(_value, filter);
}

I have not submitted a pull request because this fix is a bit weird.

My changes can be seen at https://github.com/sisve/PDFsharp-netstandard2.0/commit/6227b9f163f1c53add12faaf95fb55ed39c88b15