wmjordan/PDFPatcher · error · ArgumentException

页面高度不可小于 0。

Error message

页面高度不可小于 0。

What it means

The PaperSize.Height setter rejects any negative value with ArgumentException. Height is serialized as the XML attribute '高度' (Height) and deserialized back via the same setter, so the guard fires both at runtime and when loading an XML info file. The check is a hard lower bound: zero is allowed (auto-height modes), negatives are not.

Source

Thrown at App/Model/PaperSize.cs:47

					AsLargestPage => SpecialPaperSize.AsLargestPage,
					AsSmallestPage => SpecialPaperSize.AsSmallestPage,
					AsFirstPage => SpecialPaperSize.AsFirstPage,
					_ => SpecialPaperSize.None,
				};
			}
		}

		[XmlIgnore]
		public SpecialPaperSize SpecialSize { get; private set; }

		private float _Height;
		///<summary>获取或指定页面高度的值。</summary>
		[XmlAttribute("高度")]
		public float Height {
			get => _Height;
			set {
				if (value < 0) {
					throw new ArgumentException("页面高度不可小于 0。");
				}
				_Height = value;
			}
		}

		private float _Width;
		///<summary>获取或指定页面宽度的值。</summary>
		[XmlAttribute("宽度")]
		public float Width {
			get => _Width;
			set {
				if (value < 0) {
					throw new ArgumentException("页面宽度不可小于 0。");
				}
				_Width = value;
			}
		}

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Validate the height value is non-negative before assigning it to PaperSize.Height.
  2. If loading from XML, sanitize or reject the '高度' attribute before deserialization.
  3. Trace callers of the PaperSize(string, float, float) constructor to ensure no scaling/rotation path produces a negative height.
  4. If auto-height semantics are intended, pass 0 instead of a negative sentinel.

Example fix

// before
var p = new PaperSize("A4", 595f, -842f);

// after
var h = Math.Max(0f, computedHeight);
var p = new PaperSize("A4", 595f, h);
Defensive patterns

Strategy: validation

Validate before calling

float h = ComputeHeight();
if (h < 0f) throw new ArgumentOutOfRangeException(nameof(h));
var p = new PaperSize(name, w, h); // Height setter is now safe

Type guard

static bool IsValidPaperDimension(float v) => v >= 0f;

Prevention

When it happens

Trigger: Assigning a negative number to PaperSize.Height, or constructing a PaperSize(width, height) with a negative height argument, or loading an XML info document whose '高度' attribute parses to a negative float. Also triggered indirectly by scaling/rotation code that computes a new height and passes it through the constructor.

Common situations: A user-supplied or XML-stored page dimension contains a minus sign (e.g. a malformed info file edited by hand, or a locale/parse issue turning a decimal into a negative). Scaling logic in PageDimensionProcessor that multiplies dimensions and accidentally yields a negative result due to an inverted factor.

Related errors


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