wmjordan/PDFPatcher · error · InvalidOperationException

无法匹配与指定尺寸及页码范围相符的页面。

Error message

无法匹配与指定尺寸及页码范围相符的页面。

What it means

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.

Source

Thrown at App/Processor/ContentProcessors/PageDimensionProcessor.cs:250

					= new CoordinateTranslationSettings[context.Pdf.NumberOfPages + 1];
			}
			_adjustMargins = Settings.NeedAdjustMargins;
			_pageRanges = String.IsNullOrEmpty(Settings.PageRanges) ? null : PageRangeCollection.Parse(Settings.PageRanges, 1, context.Pdf.NumberOfPages, true);
			//todo 为新增加的适应拉伸模式设置参考尺寸
			switch (Settings.PaperSize.SpecialSize) {
				case SpecialPaperSize.AsSpecificPage:
					break;
				case SpecialPaperSize.AsFirstPage:
					var r = context.Pdf.GetPageSizeWithRotation(1);
					_refPaperSize = new PaperSize(Settings.PaperSize.PaperName, r.Width, r.Height);
					break;
				case SpecialPaperSize.AsWidestPage:
				case SpecialPaperSize.AsNarrowestPage:
				case SpecialPaperSize.AsLargestPage:
				case SpecialPaperSize.AsSmallestPage:
					_refPaperSize = GetRefPaperSize(context);
					if (_refPaperSize == null) {
						throw new InvalidOperationException("无法匹配与指定尺寸及页码范围相符的页面。");
					}
					break;
			}
		}

		private PaperSize GetRefPaperSize(DocProcessorContext context) {
			Rectangle refRectangle = null;
			var specialSize = Settings.PaperSize.SpecialSize;
			foreach (var range in _pageRanges ?? PageRangeCollection.CreateSingle(1, context.Pdf.NumberOfPages)) {
				foreach (var page in range) {
					var r = context.Pdf.GetPageSizeWithRotation(page);
					if (refRectangle == null) {
						refRectangle = r;
						continue;
					}
					switch (specialSize) {
						case SpecialPaperSize.AsWidestPage:
							if (r.Width > refRectangle.Width) {

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Ensure the page range string is non-empty and overlaps with 1..NumberOfPages when using a special relative-size mode.
  2. Validate that context.Pdf.NumberOfPages > 0 and that the parsed _pageRanges contains at least one in-range page before starting the processor.
  3. If the document has no eligible pages, fall back to a fixed paper size or skip the resize operation.
  4. Check the PageRanges setting for typos or inverted bounds.

Example fix

// before
// Settings.PageRanges = "999-1000" on a 10-page doc → throws

// after
if (_refPaperSize == null) {
  Tracker.TraceMessage(Tracker.Category.Warning, "没有匹配的页面,改用默认尺寸。");
  _refPaperSize = Settings.PaperSize.Clone();
}
Defensive patterns

Strategy: validation

Validate before calling

if (context.Pdf.NumberOfPages == 0) throw new InvalidOperationException("PDF has no pages.");
var ranges = PageRangeCollection.Parse(Settings.PageRanges, 1, context.Pdf.NumberOfPages, true);
if (ranges.Count == 0) throw new InvalidOperationException("Page range matches no pages.");

Type guard

static bool HasEligiblePages(PdfReader pdf, string rangeStr) {
  if (pdf.NumberOfPages == 0) return false;
  var r = PageRangeCollection.Parse(rangeStr, 1, pdf.NumberOfPages, true);
  return r.Count > 0;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13). Data as JSON: /api/errors/60543e407aa7a2aa. Report an issue: GitHub.