{"record":{"id":"60543e407aa7a2aa","repo":"wmjordan/PDFPatcher","slug":"error-60543e","errorCode":null,"errorMessage":"无法匹配与指定尺寸及页码范围相符的页面。","messagePattern":"无法匹配与指定尺寸及页码范围相符的页面。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"App/Processor/ContentProcessors/PageDimensionProcessor.cs","lineNumber":250,"sourceCode":"\t\t\t\t\t= new CoordinateTranslationSettings[context.Pdf.NumberOfPages + 1];\r\n\t\t\t}\r\n\t\t\t_adjustMargins = Settings.NeedAdjustMargins;\r\n\t\t\t_pageRanges = String.IsNullOrEmpty(Settings.PageRanges) ? null : PageRangeCollection.Parse(Settings.PageRanges, 1, context.Pdf.NumberOfPages, true);\r\n\t\t\t//todo 为新增加的适应拉伸模式设置参考尺寸\r\n\t\t\tswitch (Settings.PaperSize.SpecialSize) {\r\n\t\t\t\tcase SpecialPaperSize.AsSpecificPage:\r\n\t\t\t\t\tbreak;\r\n\t\t\t\tcase SpecialPaperSize.AsFirstPage:\r\n\t\t\t\t\tvar r = context.Pdf.GetPageSizeWithRotation(1);\r\n\t\t\t\t\t_refPaperSize = new PaperSize(Settings.PaperSize.PaperName, r.Width, r.Height);\r\n\t\t\t\t\tbreak;\r\n\t\t\t\tcase SpecialPaperSize.AsWidestPage:\r\n\t\t\t\tcase SpecialPaperSize.AsNarrowestPage:\r\n\t\t\t\tcase SpecialPaperSize.AsLargestPage:\r\n\t\t\t\tcase SpecialPaperSize.AsSmallestPage:\r\n\t\t\t\t\t_refPaperSize = GetRefPaperSize(context);\r\n\t\t\t\t\tif (_refPaperSize == null) {\r\n\t\t\t\t\t\tthrow new InvalidOperationException(\"无法匹配与指定尺寸及页码范围相符的页面。\");\r\n\t\t\t\t\t}\r\n\t\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tprivate PaperSize GetRefPaperSize(DocProcessorContext context) {\r\n\t\t\tRectangle refRectangle = null;\r\n\t\t\tvar specialSize = Settings.PaperSize.SpecialSize;\r\n\t\t\tforeach (var range in _pageRanges ?? PageRangeCollection.CreateSingle(1, context.Pdf.NumberOfPages)) {\r\n\t\t\t\tforeach (var page in range) {\r\n\t\t\t\t\tvar r = context.Pdf.GetPageSizeWithRotation(page);\r\n\t\t\t\t\tif (refRectangle == null) {\r\n\t\t\t\t\t\trefRectangle = r;\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tswitch (specialSize) {\r\n\t\t\t\t\t\tcase SpecialPaperSize.AsWidestPage:\r\n\t\t\t\t\t\t\tif (r.Width > refRectangle.Width) {\r","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/ContentProcessors/PageDimensionProcessor.cs#L232-L268","documentation":"PageDimensionProcessor.BeginProcess throws InvalidOperationException when GetRefPaperSize returns null for the special sizing modes (AsWidestPage, AsNarrowestPage, AsLargestPage, AsSmallestPage). GetRefPaperSize iterates the configured page ranges (or all pages) and returns null only if no pages were iterated — i.e. the page range collection is empty or all ranges are empty.","triggerScenarios":"Selecting a 'match widest/narrowest/largest/smallest page' special size while supplying a page range filter that excludes every page (empty range, or a range outside 1..NumberOfPages). GetRefPaperSize never assigns refRectangle and returns null, triggering the guard.","commonSituations":"User configures 'resize to widest page' but also sets a page-range string that matches no pages; the PDF has zero pages (NumberOfPages == 0); the page range parser produced an empty collection; a numeric range like '0-0' or a range entirely beyond the document length.","solutions":["Ensure the page range string is non-empty and overlaps with 1..NumberOfPages when using a special relative-size mode.","Validate that context.Pdf.NumberOfPages > 0 and that the parsed _pageRanges contains at least one in-range page before starting the processor.","If the document has no eligible pages, fall back to a fixed paper size or skip the resize operation.","Check the PageRanges setting for typos or inverted bounds."],"exampleFix":"// before\n// Settings.PageRanges = \"999-1000\" on a 10-page doc → throws\n\n// after\nif (_refPaperSize == null) {\n  Tracker.TraceMessage(Tracker.Category.Warning, \"没有匹配的页面，改用默认尺寸。\");\n  _refPaperSize = Settings.PaperSize.Clone();\n}","handlingStrategy":"validation","validationCode":"if (context.Pdf.NumberOfPages == 0) throw new InvalidOperationException(\"PDF has no pages.\");\nvar ranges = PageRangeCollection.Parse(Settings.PageRanges, 1, context.Pdf.NumberOfPages, true);\nif (ranges.Count == 0) throw new InvalidOperationException(\"Page range matches no pages.\");","typeGuard":"static bool HasEligiblePages(PdfReader pdf, string rangeStr) {\n  if (pdf.NumberOfPages == 0) return false;\n  var r = PageRangeCollection.Parse(rangeStr, 1, pdf.NumberOfPages, true);\n  return r.Count > 0;\n}","tryCatchPattern":null,"preventionTips":["Validate the page range overlaps 1..NumberOfPages before using relative sizing modes.","Default to all pages when the range string is empty.","Guard against zero-page documents up front."],"tags":["pdf","page-dimensions","configuration","page-range","validation"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}