iOfficeAI/OfficeCLI · error · ArgumentException

Invalid font size: '{value}'. Font size must be greater than

Error message

Invalid font size: '{value}'. Font size must be greater than 0.

What it means

Thrown by ParseFontSize when the parsed number is <= 0. A non-positive font size is invalid, so it is rejected before any OOXML write. This guard sits below the finite-number check and above the half-point minimum check.

Source

Thrown at src/officecli/Core/ParseHelpers.cs:369

        value != null && TrimInvisible(value).ToLowerInvariant() is "true" or "1" or "yes" or "on"
                                                                 or "false" or "0" or "no" or "off";

    /// <summary>
    /// Parse a font size string, stripping optional "pt" suffix.
    /// Supports integers and fractional values (e.g. "24", "10.5", "24pt").
    /// Returns double to preserve fractional sizes for correct unit conversion.
    /// </summary>
    public static double ParseFontSize(string value)
    {
        var trimmed = value.Trim();
        if (trimmed.EndsWith("pt", StringComparison.OrdinalIgnoreCase))
            trimmed = trimmed[..^2].Trim();
        if (trimmed.Contains(','))
            throw new ArgumentException($"Invalid font size: '{value}'. Comma is not allowed — use '.' as decimal separator (e.g., '10.5').");
        if (!double.TryParse(trimmed, CultureInfo.InvariantCulture, out var result) || double.IsNaN(result) || double.IsInfinity(result))
            throw new ArgumentException($"Invalid font size: '{value}'. Expected a finite number (e.g., '12', '10.5', '14pt').");
        if (result <= 0)
            throw new ArgumentException($"Invalid font size: '{value}'. Font size must be greater than 0.");
        // OOXML w:sz/w:szCs/w:fontSize are half-points and must be >= 1.
        // Anything below 0.5pt would round to val=0 on write, producing
        // schema-invalid OOXML. Reject up front with the same shape as
        // the "<= 0" guard above.
        if (result < 0.5)
            throw new ArgumentException($"Invalid font size: '{value}'. Minimum font size is 0.5pt (one half-point).");
        // OOXML caps user-entered font size at 1638pt (Word) and Office
        // renderers stop honoring values past ~4000pt anyway. Anything
        // larger silently overflows the int32 the writers cast to (PPTX
        // writes pt × 100, Word writes pt × 2 as half-points), producing
        // negative w:sz / a:rPr@sz values Word rejects on open. Reject
        // up front with the same shape as the lower-bound guards.
        if (result > 4000)
            throw new ArgumentException($"Invalid font size: '{value}'. Maximum font size is 4000pt (Office cap).");
        return result;
    }

    /// <summary>

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Ensure the size is strictly positive (>0).
  2. Clamp computed sizes to a small positive floor before parsing.
  3. Treat 0 as 'unset/inherit' upstream rather than passing it to ParseFontSize.

Example fix

// before
var size = (baseSize * scale).ToString(CultureInfo.InvariantCulture); // scale 0 -> 0
ParseFontSize(size); // throws 294

// after
var pts = Math.Max(baseSize * scale, 1.0);
ParseFontSize(pts.ToString(CultureInfo.InvariantCulture));
Defensive patterns

Strategy: validation

Validate before calling

if (pts <= 0) throw new InvalidOperationException("font size must be > 0");
// or clamp: pts = Math.Max(pts, 1.0);

Type guard

static bool IsPositiveSize(double v) => v > 0 && !double.IsNaN(v) && !double.IsInfinity(v);

Try / catch

try { pts = ParseFontSize(s); }
catch (ArgumentException ex) when (ex.Message.Contains("greater than 0"))
{ pts = 1; }

Prevention

When it happens

Trigger: Passing "0", "-5", or a size computed as zero (e.g. baseSize - offset where offset >= baseSize).

Common situations: Font-size scaling math that bottoms out at zero; negative sizes from an inverted scale factor; default-unset sentinel of 0 leaking into the size field.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/b5b04ef71fa953dd. Report an issue: GitHub.