Skip to content

Commit 0cf5d43

Browse files
committed
Allow XPdfForm to open password protected PDFs
1 parent 278e6f3 commit 0cf5d43

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

PdfSharpCore/Drawing/XPdfForm.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ internal XPdfForm(Stream stream)
9393
_externalDocument = PdfReader.Open(stream);
9494
}
9595

96+
/// <summary>
97+
/// Initializes a new instance of the <see cref="XPdfForm"/> class from a stream and password.
98+
/// </summary>
99+
/// <param name="stream">The stream.</param>
100+
/// <param name="password">The password.</param>
101+
internal XPdfForm(Stream stream, string password) {
102+
// Create a dummy unique path
103+
_path = "*" + Guid.NewGuid().ToString("B");
104+
105+
if (PdfReader.TestPdfFile(stream) == 0)
106+
throw new ArgumentException("The specified stream has no valid PDF file header.", "stream");
107+
108+
_externalDocument = PdfReader.Open(stream, password, PdfDocumentOpenMode.ReadOnly);
109+
}
110+
96111
/// <summary>
97112
/// Creates an XPdfForm from a file.
98113
/// </summary>
@@ -110,6 +125,13 @@ public static XPdfForm FromStream(Stream stream)
110125
return new XPdfForm(stream);
111126
}
112127

128+
/// <summary>
129+
/// Creates an XPdfForm from a stream and a password.
130+
/// </summary>
131+
public static XPdfForm FromStream(Stream stream, string password) {
132+
return new XPdfForm(stream, password);
133+
}
134+
113135
/*
114136
void Initialize()
115137
{

0 commit comments

Comments
 (0)