diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReader.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReader.cs index 45ab116b..1898c1a5 100644 --- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReader.cs +++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReader.cs @@ -308,7 +308,8 @@ PdfDocument OpenFromStream(Stream stream, string? password, PdfDocumentOpenMode // Reference.Values available by now: All trailers and cross-reference streams (which are not encrypted by definition). // 2. Read the encryption dictionary, if existing. - if (_document.Trailer!.Elements[PdfTrailer.Keys.Encrypt] is PdfReference xrefEncrypt) + var encryptPdfItem = _document.Trailer!.Elements[PdfTrailer.Keys.Encrypt]; + if (encryptPdfItem is PdfReference xrefEncrypt) { var encrypt = parser.ReadIndirectObject(xrefEncrypt, null, true); encrypt.Reference = xrefEncrypt; @@ -316,6 +317,10 @@ PdfDocument OpenFromStream(Stream stream, string? password, PdfDocumentOpenMode _document.SecurityHandler.PrepareForReading(); } + else if (encryptPdfItem is PdfDictionary) + { + _document.SecurityHandler.PrepareForReading(); + } // References available by now: All references to file-level objects. // Reference.Values available by now: All trailers and cross-reference streams and the encryption dictionary. diff --git a/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/IO/ReaderTests.cs b/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/IO/ReaderTests.cs index 7e4e3fd6..7893a232 100644 --- a/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/IO/ReaderTests.cs +++ b/src/foundation/src/PDFsharp/tests/PdfSharp.Tests/IO/ReaderTests.cs @@ -298,5 +298,15 @@ static void DrawPageFrame(PdfPage page, XColor color, Int32 width, string? label } } } + + [Fact] + public void Encrypt_can_be_an_inline_dictionary() + { + const string path = @"C:\Users\ArnaudTAMAILLON\Downloads\dictionary_encrypt.pdf"; + var act = () => PdfReader.Open(path, PdfDocumentOpenMode.Import); + act.Should().NotThrow(); + act = () => PdfReader.Open(path, "asdfzxcv", PdfDocumentOpenMode.Modify); + act.Should().NotThrow(); + } } }