Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inline dictionaries for Encrypt tag #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,19 @@ 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;
xrefEncrypt.Value = encrypt;

_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.

Expand Down
10 changes: 10 additions & 0 deletions src/foundation/src/PDFsharp/tests/PdfSharp.Tests/IO/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}