wmjordan/PDFPatcher · error · ArgumentException

转换精度不能小于 0 或大于 6。

Error message

转换精度不能小于 0 或大于 6。

What it means

The UnitConverter.Precision setter clamps precision to the inclusive range 0–6; anything outside throws ArgumentException. Precision controls how many decimal places are kept when converting between PDF points and display units (it is used to compute _PreservedValue = 0.1^precision). The bound exists because the converter formats with ToStringFormat '0.###' (3 digits) by default but allows up to 6.

Source

Thrown at App/Model/UnitConverter.cs:26

	{
		internal const string ToStringFormat = "0.###";
		const string Null = "null";

		/// <summary>
		/// 获取单位转换因数。
		/// </summary>
		[XmlIgnore]
		public float UnitFactor { get; private set; }

		private int _Precision;
		private float _PreservedValue;
		///<summary>获取或指定转换精度的值。</summary>
		[XmlIgnore]
		public int Precision {
			get => _Precision;
			set {
				if (value < 0 || value > 6) {
					throw new ArgumentException("转换精度不能小于 0 或大于 6。");
				}
				_Precision = value;
				_PreservedValue = (float)Math.Pow(0.1, _Precision);
			}
		}

		private string _Unit;
		///<summary>获取或指定转换使用的单位。</summary>
		[XmlAttribute("单位")]
		public string Unit {
			get => _Unit;
			set {
				var f = ValueHelper.MapValue(value, Constants.Units.Names, Constants.Units.Factors, 0);
				if (f == 0) {
					throw new ArgumentException("尺寸单位无效。");
				}
				UnitFactor = f;
				_Unit = value;

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Clamp the precision value to [0, 6] before assigning it to UnitConverter.Precision.
  2. Validate deserialized options files for a precision field in range before applying them.
  3. If a finer granularity is genuinely needed, reconsider the design rather than widening the bound (the formatting and preserved-value math assume ≤6).

Example fix

// before
converter.Precision = 8;

// after
converter.Precision = Math.Clamp(desired, 0, 6);
Defensive patterns

Strategy: validation

Validate before calling

int p = desiredPrecision;
if (p < 0 || p > 6) p = Math.Clamp(p, 0, 6);
converter.Precision = p;

Type guard

static bool IsValidPrecision(int p) => p >= 0 && p <= 6;

Prevention

When it happens

Trigger: Assigning UnitConverter.Precision a value less than 0 or greater than 6, or deserializing a configuration/options object whose Precision property (backed by this setter) is out of range.

Common situations: User sets an unrealistic precision in the options dialog; a serialized options file with a corrupt precision value; copying a precision constant from another library with a different scale.

Related errors


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