我将段落宽度用作“添加水平规则”辅助方法的一部分。使用左右缩进效果很好:
public static void AddHorizontalRule(Section section, double percentWidth, Color? color = null)
{
double percent = (percentWidth < 0.0 || percentWidth > 1.0) ? 1.0 : percentWidth;
Color hrColor = color ?? new Color(96, 96, 96); // Lt Grey default
Unit contentWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
Unit indentSize = (contentWidth - (percent * contentWidth)) / 2.0;
Paragraph paragraph = section.AddParagraph();
paragraph.Format.LeftIndent = indentSize;
paragraph.Format.RightIndent = indentSize;
paragraph.Format.Borders.Top.Visible = true;
paragraph.Format.Borders.Left.Visible = false;
paragraph.Format.Borders.Right.Visible = false;
paragraph.Format.Borders.Bottom.Visible = false;
paragraph.Format.Borders.Top.Color = hrColor;
}
请注意,由于节的 PageSetup 值为 0,因此使用默认文档设置,要使用如上所示的客户区宽度,您需要在调用此方法之前在 section.PageSetup 中显式设置这些值。我这样做是为了不必传递文档,也不必依赖 document.LastSection 是我正在处理的部分。我只是传入一个 Section 对象并拥有它。
享受!布赖恩