wmjordan/PDFPatcher · error · ArgumentException

页面宽度不可小于 0。

Error message

页面宽度不可小于 0。

What it means

The PaperSize.Width setter rejects any negative value with ArgumentException. Width is serialized as XML attribute '宽度' (Width) and round-trips through the same setter. Zero is permitted (used by special auto-width modes), negatives are not.

Source

Thrown at App/Model/PaperSize.cs:60

		[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;
			}
		}

		public PaperSize() { }

		public PaperSize(float width, float height) : this(null, width, height) {
		}

		public PaperSize(string paperName, float width, float height) {
			PaperName = paperName;
			Width = width;
			Height = height;
		}

		internal PaperSize Scale(float xFactor, float yFactor) {
			return new PaperSize(PaperName, Width * xFactor, Height * yFactor);

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Validate the width value is non-negative before assigning it to PaperSize.Width.
  2. Sanitize the '宽度' attribute in XML before loading if the source is untrusted.
  3. Audit Scale() callers and rotation code paths that construct new PaperSize instances to ensure positive inputs.
  4. Pass 0 rather than a negative sentinel for auto-width special sizes.

Example fix

// before
size = new PaperSize(name, size.Height, size.Width); // size.Width may be negative

// after
size = new PaperSize(name, size.Height, Math.Max(0f, size.Width));
Defensive patterns

Strategy: validation

Validate before calling

float w = ComputeWidth();
if (w < 0f) throw new ArgumentOutOfRangeException(nameof(w));
var p = new PaperSize(name, w, h);

Type guard

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

Prevention

When it happens

Trigger: Assigning a negative number to PaperSize.Width, constructing a PaperSize(width, height) with a negative width, or deserializing an XML info document whose '宽度' attribute is negative. Also reached via PaperSize.Scale or RotatePage-derived computations that feed a negative width back through the constructor.

Common situations: Hand-edited XML info file with a malformed width value; a unit-conversion or scaling factor that flips the sign; rotation logic in PageDimensionProcessor swapping width/height when one was already negative.

Related errors


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