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
textProsobject you created isn't being added to thelegend2element—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?
- Attached
TextPropertiesto Legend: We addedlegend2.Append(textPros);so your styling is actually applied to the legend element. - Added Font Size: The
DefaultRunPropertieswithFontSize = 16sets 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. - Added Tight Line Spacing: The
Spacingelement withLine = 200sets 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
FontSizevalue incrementally (e.g., down to 12 for 6pt) or decreasing theLinevalue in theSpacingelement. - Make sure you're using the correct OpenXML namespace aliases (
dcforDocumentFormat.OpenXml.Drawing.Charts,dforDocumentFormat.OpenXml.Drawing) to avoid compilation errors.
内容的提问来源于stack exchange,提问作者Faiza Nasir




