Skip to content

Commit

Permalink
-修复PDF编辑器无法打开带密码文档的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Feb 10, 2022
1 parent 8072865 commit f3238d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion App/Functions/Editor/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void LoadPdfDocument() {
if (s != null) {
try {
var d = v.Document;
Model.PdfDocument = v.Document = new MuPdfSharp.MuDocument(s);
Model.PdfDocument = v.Document = PdfHelper.OpenMuDocument(s);
d.TryDispose();
View.AutoBookmark.TryDispose();
v.Enabled = true;
Expand Down
19 changes: 8 additions & 11 deletions App/Processor/Mupdf/MuDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public int AntiAlias {
public bool IsDocumentOpened => _document.IsValid() && _sourceStream.IsValid();
/// <summary>获取文档是否设置了打开密码。</summary>
public bool NeedsPassword => NativeMethods.NeedsPdfPassword(_context, _document);
public bool IsAuthenticated { get; private set; }
public bool IsCancellationPending => _cookie.IsCancellationPending;
object _SyncObj = new object();
public object SyncObj => _SyncObj;
Expand Down Expand Up @@ -151,18 +152,14 @@ public MuPage LoadPage(int pageNumber) {
return new MuPage(_context, _document, pageNumber, ref _cookie);
}

private int InitPdf(string password) {
if (NativeMethods.NeedsPdfPassword(_context, _document)) {
if (String.IsNullOrEmpty(password) == false) {
if (NativeMethods.AuthenticatePassword(_context, _document, password) == false) {
throw new iTextSharp.text.exceptions.BadPasswordException("密码无效。");
}
}
else {
throw new iTextSharp.text.exceptions.BadPasswordException("需要提供打开 PDF 文档的密码。");
}
}
public bool AuthenticatePassword(string password) {
return IsAuthenticated =
NativeMethods.NeedsPdfPassword(_context, _document) == false
|| String.IsNullOrEmpty(password) == false && NativeMethods.AuthenticatePassword(_context, _document, password);
}

int InitPdf(string password) {
AuthenticatePassword(password);
PageCount = NativeMethods.CountPages(_context, _document);
return PageCount;
}
Expand Down
24 changes: 12 additions & 12 deletions App/Processor/PdfHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ internal static bool ConfirmUnethicalMode(this PdfReader pdf) {
}

internal static MuPdfSharp.MuDocument OpenMuDocument(string sourceFile) {
byte[] password;
__PdfPasswordBox.TryGetValue(sourceFile, out password);
while (true) {
var r = new MuPdfSharp.MuDocument(sourceFile, password != null ? Encoding.Default.GetString(password) : String.Empty);
if (password != null && password.Length > 0) {
__PdfPasswordBox[sourceFile] = password;
var d = new MuPdfSharp.MuDocument(sourceFile);
if (d.NeedsPassword) {
if (__PdfPasswordBox.TryGetValue(sourceFile, out byte[] password)) {
d.AuthenticatePassword(password != null ? Encoding.Default.GetString(password) : String.Empty);
}
if (r.NeedsPassword) {
var f = new PasswordEntryForm(sourceFile);
if (f.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) {
throw new iTextSharp.text.exceptions.BadPasswordException("密码错误,没有权限打开 PDF 文件。");
while (d.IsAuthenticated == false) {
using (var f = new PasswordEntryForm(sourceFile)) {
if (f.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) {
throw new iTextSharp.text.exceptions.BadPasswordException("密码错误,没有权限打开 PDF 文件。");
}
__PdfPasswordBox[sourceFile] = password = Encoding.Default.GetBytes(f.Password);
}
password = Encoding.Default.GetBytes(f.Password);
d.AuthenticatePassword(password != null ? Encoding.Default.GetString(password) : String.Empty);
}
return r;
}
return d;
}

static PdfHelper() {
Expand Down

0 comments on commit f3238d4

Please sign in to comment.