wmjordan/PDFPatcher · error · MuException
密码错误,没有权限打开 PDF 文件。
Error message
密码错误,没有权限打开 PDF 文件。
What it means
MuPdfHelper.OpenMuDocument throws MuPDF.MuException('密码错误,没有权限打开 PDF 文件') when the user cancels the password-entry dialog for an encrypted PDF, or when authentication against a password-protected document ultimately fails. The loop prompts via PasswordEntryForm; if the dialog returns Cancel, the code gives up and throws.
Source
Thrown at App/Processor/MuPdfHelper.cs:19
using System;
using System.Text;
namespace PDFPatcher.Processor;
internal static class MuPdfHelper
{
internal static MuPDF.Document OpenMuDocument(string sourceFile) {
var d = MuPDF.Document.Open(sourceFile);
if (d.NeedsPassword) {
var authenticated = false;
if (PdfHelper.PasswordCache.TryGetValue(sourceFile, out byte[] password)) {
authenticated = d.CheckPassword(password != null ? Encoding.Default.GetString(password) : String.Empty);
}
while (!authenticated) {
using (var f = new PasswordEntryForm(sourceFile)) {
if (f.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) {
throw new MuPDF.MuException("密码错误,没有权限打开 PDF 文件。");
}
PdfHelper.PasswordCache[sourceFile] = password = Encoding.Default.GetBytes(f.Password);
}
authenticated = d.CheckPassword(password != null ? Encoding.Default.GetString(password) : String.Empty);
}
}
return d;
}
}View on GitHub (pinned to 4782bbd9ad)
Solutions
- Provide the correct password via PdfHelper.PasswordCache[sourceFile] before calling OpenMuDocument, to bypass the interactive prompt.
- For batch processing, pre-populate the password cache from configuration to avoid the UI prompt.
- Catch MuPDF.MuException at the call site and skip/log encrypted documents the user cannot unlock.
- If running headless, detect d.NeedsPassword beforehand and handle it without invoking the dialog.
Example fix
// before
var doc = MuPdfHelper.OpenMuDocument(file); // throws on cancel
// after
PdfHelper.PasswordCache[file] = Encoding.Default.GetBytes(knownPassword);
var doc = MuPdfHelper.OpenMuDocument(file);
// or:
try { doc = MuPdfHelper.OpenMuDocument(file); }
catch (MuPDF.MuException) { Tracker.TraceMessage(Tracker.Category.Warning, "无法打开加密文档:" + file); continue; } Defensive patterns
Strategy: validation
Validate before calling
PdfHelper.PasswordCache[file] = Encoding.Default.GetBytes(knownPassword); // then OpenMuDocument will authenticate from cache without prompting
Type guard
static bool HasCachedPassword(string file) =>
PdfHelper.PasswordCache.ContainsKey(file); Try / catch
try { var doc = MuPdfHelper.OpenMuDocument(file); }
catch (MuPDF.MuException) { /* skip encrypted/unopenable file */ } Prevention
- Pre-populate PdfHelper.PasswordCache for known-encrypted files.
- Detect NeedsPassword before calling OpenMuDocument in headless flows.
- Catch MuPDF.MuException at the call site and skip/log.
- Provide a non-interactive password source for batch processing.
When it happens
Trigger: Calling OpenMuDocument on a PDF whose NeedsPassword is true, and either (a) the PasswordEntryForm dialog is cancelled by the user, or (b) no cached password exists and the user clicks Cancel on the prompt. The throw occurs at the Cancel branch; a wrong password re-prompts rather than throwing immediately.
Common situations: Batch/headless processing of an encrypted PDF where no UI dialog can be shown or the user dismisses it; a document requiring a password not present in PdfHelper.PasswordCache; a GUI run where the user cancels because they don't know the password.
Related errors
- 页面缺少 MediaBox。
- 无法匹配与指定尺寸及页码范围相符的页面。
- 无法加载字体:{n}
- 在简易书签第 {lineNum} 行的缩进格式不正确。 说明:下级书签最多只能比上级书签多一个缩进标记。
- 简易书签文件内容不足。
AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13).
Data as JSON: /api/errors/039d970c07f1efb7.
Report an issue: GitHub.