{"record":{"id":"6c56a7485d4f0d3b","repo":"wmjordan/PDFPatcher","slug":"sourcefile","errorCode":null,"errorMessage":"找不到文件：{sourceFile}","messagePattern":"找不到文件：(.+?)","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"App/Processor/PdfHelper.cs","lineNumber":49,"sourceCode":"\t\t/// </summary>\r\n\t\t/// <param name=\"debugMode\">是否打开容错模式。</param>\r\n\t\tinternal static void ToggleReaderDebugMode(bool debugMode) {\r\n\t\t\tPdfReader.debugmode = debugMode;\r\n\t\t}\r\n\r\n\t\t/// <summary>\r\n\t\t/// 打开 PDF 文件，在有需要时提示输入密码。\r\n\t\t/// </summary>\r\n\t\t/// <param name=\"sourceFile\">需要打开的 PDF 文件。</param>\r\n\t\t/// <param name=\"partial\">是否仅打开文件的部分内容。</param>\r\n\t\t/// <returns><see cref=\"PdfReader\"/> 实例。</returns>\r\n\t\tinternal static PdfReader OpenPdfFile(string sourceFile, bool partial, bool removeUnusedObjects) {\r\n\t\t\tbyte[] password;\r\n\t\t\t__PdfPasswordCache.TryGetValue(sourceFile, out password);\r\n\t\t\twhile (true) {\r\n\t\t\t\ttry {\r\n\t\t\t\t\tif (!File.Exists(sourceFile)) {\r\n\t\t\t\t\t\tthrow new FileNotFoundException($\"找不到文件：{sourceFile}\");\r\n\t\t\t\t\t}\r\n\t\t\t\t\tPdfReader r;\r\n\t\t\t\t\tif (partial) {\r\n\t\t\t\t\t\tr = new PdfReader(new RandomAccessFileOrArray(sourceFile), password);\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse {\r\n\t\t\t\t\t\tr = new PdfReader(sourceFile, password);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif (password != null && password.Length > 0) {\r\n\t\t\t\t\t\t__PdfPasswordCache[sourceFile] = password;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif (removeUnusedObjects) {\r\n\t\t\t\t\t\tr.RemoveUnusedObjects();\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn r;\r\n\t\t\t\t}\r\n\t\t\t\tcatch (iTextSharp.text.exceptions.BadPasswordException) {\r\n\t\t\t\t\tvar f = new PasswordEntryForm(sourceFile);\r","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/PdfHelper.cs#L31-L67","documentation":"Thrown as FileNotFoundException by OpenPdfFile when File.Exists(sourceFile) is false, as an explicit guard before constructing iTextSharp's PdfReader. It runs inside the password-retry while-loop, so it also fires if the file disappears between a failed password attempt and the next iteration. The password cache is consulted before this check, but a missing file aborts before any PdfReader allocation.","triggerScenarios":"OpenPdfFile(sourceFile, partial, removeUnusedObjects) where the OS reports the path does not exist: deleted/renamed file, path is a directory, relative path resolved against an unexpected working directory, unreachable UNC/network path, or the file was removed mid-session (e.g. between adding to the job list and processing).","commonSituations":"File moved or renamed after being added to a batch/processing list; relative paths breaking when CWD changes; network/USB source detached; a path with trailing spaces or quotes copied from a UI; case-sensitivity mismatch on case-sensitive filesystems for a path authored on Windows.","solutions":["Validate File.Exists(sourceFile) and that it is a file (not a directory) before calling OpenPdfFile, and surface a user-friendly message.","Persist and pass absolute, normalized paths (Path.GetFullPath) so CWD changes do not break resolution.","Catch FileNotFoundException at the call site and skip/log the entry instead of crashing a batch run.","Re-resolve or refresh stale paths from the job list before processing."],"exampleFix":"// before\nusing var r = PdfHelper.OpenPdfFile(sourceFile, partial, true);\n\n// after\nif (!File.Exists(sourceFile)) {\n    Tracker.TraceMessage($\"文件不存在，已跳过：{sourceFile}\");\n    continue;\n}\nusing var r = PdfHelper.OpenPdfFile(Path.GetFullPath(sourceFile), partial, true);","handlingStrategy":"validation","validationCode":"if (!File.Exists(sourceFile)) {\n    Tracker.TraceMessage($\"文件不存在，已跳过：{sourceFile}\");\n    continue;\n}\nsourceFile = Path.GetFullPath(sourceFile);","typeGuard":null,"tryCatchPattern":"try {\n    using var r = PdfHelper.OpenPdfFile(sourceFile, partial, true);\n    // ... use r\n}\ncatch (FileNotFoundException ex) {\n    Tracker.TraceMessage($\"找不到文件，已跳过：{ex.FileName ?? sourceFile}\");\n}","preventionTips":["Persist absolute, normalized paths (Path.GetFullPath) in job lists so CWD changes do not break resolution.","Refresh/re-resolve stale paths and remove missing entries before a batch run.","Validate existence and that the path is a file (not a directory) before opening."],"tags":["pdf","file-io","filenotfound","itextsharp","csharp"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}