You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

C#用OpenXML生成Word文档:如何调整图例字体或行间距解决遮挡?

Fixing Truncated Legend Text in OpenXML Pie Charts

It looks like you're missing two key pieces in your current code that are preventing you from adjusting the legend's font size and line spacing: you haven't attached your configured TextProperties to the Legend object, and you haven't explicitly set the font size and line spacing values in the paragraph/run properties. Let's fix that step by step.

Key Issues in Your Current Code

  • The textPros object you created isn't being added to the legend2 element—so none of your text styling is actually being applied to the legend.
  • You haven't defined concrete values for font size or line spacing in your paragraph/run properties.

Modified Code with Fixes

Here's the updated version of your CreateChart method that properly applies smaller font size and tighter line spacing to the legend:

public void CreateChart(List<ChartSubArea> chartList, string variable, string id) {
    dc.Legend legend2 = new dc.Legend();
    dc.LegendPosition legendPosition2 = new dc.LegendPosition() { val = dc.LegendPositionValues.Right };
    dc.Overlay overlay3 = new dc.Overlay() { Val = false };
    legend2.Append(legendPosition2);
    legend2.Append(overlay3);

    // Configure text properties for legend
    dc.TextProperties textPros = new dc.TextProperties();
    textPros.Append(new d.BodyProperties());
    textPros.Append(new d.ListStyle());

    d.Paragraph paragraph = new d.Paragraph();
    d.ParagraphProperties paraPros = new d.ParagraphProperties();
    d.DefaultParagraphProperties defaultParaPros = new d.DefaultParagraphProperties();

    // Set font to Arial, and add font size (8pt = 16 half-points)
    defaultParaPros.Append(new d.LatinFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
    defaultParaPros.Append(new d.ComplexScriptFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
    d.DefaultRunProperties defaultRunProps = new d.DefaultRunProperties() { FontSize = 16 }; // 8pt font
    defaultParaPros.Append(defaultRunProps);

    // Set tight line spacing (10pt line height = 200 twentieths of a point)
    d.Spacing spacing = new d.Spacing() { Line = 200, LineRule = d.LineSpacingRuleValues.Auto };
    defaultParaPros.Append(spacing);

    paraPros.Append(defaultParaPros);
    paragraph.Append(paraPros);
    paragraph.Append(new d.EndParagraphRunProperties() { Language = "en-Us" });

    textPros.Append(paragraph);
    // Critical: Attach the text properties to the legend
    legend2.Append(textPros);
}

What Changed?

  1. Attached TextProperties to Legend: We added legend2.Append(textPros); so your styling is actually applied to the legend element.
  2. Added Font Size: The DefaultRunProperties with FontSize = 16 sets the text to 8pt (OpenXML uses half-point units for font size). Adjust this value (e.g., 14 for 7pt, 12 for 6pt) if you need even smaller text.
  3. Added Tight Line Spacing: The Spacing element with Line = 200 sets the line height to 10pt (units are twentieths of a point). Lower this value (like 180 for 9pt) to tighten spacing further.

Additional Notes

  • If the legend is still truncated, try reducing the FontSize value incrementally (e.g., down to 12 for 6pt) or decreasing the Line value in the Spacing element.
  • Make sure you're using the correct OpenXML namespace aliases (dc for DocumentFormat.OpenXml.Drawing.Charts, d for DocumentFormat.OpenXml.Drawing) to avoid compilation errors.

内容的提问来源于stack exchange,提问作者Faiza Nasir

火山引擎 最新活动