{"record":{"id":"039d970c07f1efb7","repo":"wmjordan/PDFPatcher","slug":"pdf","errorCode":null,"errorMessage":"密码错误，没有权限打开 PDF 文件。","messagePattern":"密码错误，没有权限打开 PDF 文件。","errorType":"exception","errorClass":"MuException","httpStatus":null,"severity":"error","filePath":"App/Processor/MuPdfHelper.cs","lineNumber":19,"sourceCode":"﻿using System;\r\nusing System.Text;\r\n\r\nnamespace PDFPatcher.Processor;\r\n\r\ninternal static class MuPdfHelper\r\n{\r\n\r\n\tinternal static MuPDF.Document OpenMuDocument(string sourceFile) {\r\n\t\tvar d = MuPDF.Document.Open(sourceFile);\r\n\t\tif (d.NeedsPassword) {\r\n\t\t\tvar authenticated = false;\r\n\t\t\tif (PdfHelper.PasswordCache.TryGetValue(sourceFile, out byte[] password)) {\r\n\t\t\t\tauthenticated = d.CheckPassword(password != null ? Encoding.Default.GetString(password) : String.Empty);\r\n\t\t\t}\r\n\t\t\twhile (!authenticated) {\r\n\t\t\t\tusing (var f = new PasswordEntryForm(sourceFile)) {\r\n\t\t\t\t\tif (f.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) {\r\n\t\t\t\t\t\tthrow new MuPDF.MuException(\"密码错误，没有权限打开 PDF 文件。\");\r\n\t\t\t\t\t}\r\n\t\t\t\t\tPdfHelper.PasswordCache[sourceFile] = password = Encoding.Default.GetBytes(f.Password);\r\n\t\t\t\t}\r\n\t\t\t\tauthenticated = d.CheckPassword(password != null ? Encoding.Default.GetString(password) : String.Empty);\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn d;\r\n\t}\r\n}","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/MuPdfHelper.cs#L1-L28","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar doc = MuPdfHelper.OpenMuDocument(file); // throws on cancel\n\n// after\nPdfHelper.PasswordCache[file] = Encoding.Default.GetBytes(knownPassword);\nvar doc = MuPdfHelper.OpenMuDocument(file);\n// or:\ntry { doc = MuPdfHelper.OpenMuDocument(file); }\ncatch (MuPDF.MuException) { Tracker.TraceMessage(Tracker.Category.Warning, \"无法打开加密文档：\" + file); continue; }","handlingStrategy":"validation","validationCode":"PdfHelper.PasswordCache[file] = Encoding.Default.GetBytes(knownPassword);\n// then OpenMuDocument will authenticate from cache without prompting","typeGuard":"static bool HasCachedPassword(string file) =>\n    PdfHelper.PasswordCache.ContainsKey(file);","tryCatchPattern":"try { var doc = MuPdfHelper.OpenMuDocument(file); }\ncatch (MuPDF.MuException) { /* skip encrypted/unopenable file */ }","preventionTips":["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."],"tags":["pdf","encryption","password","mu-pdf","user-cancel"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}