Skip to content

Paragraph

XML常用部分结构

w:p
|
|-- w:pPr
|   |-- w:jc
|   |-- w:spacing
|   |-- w:ind
|   |-- w:outlineLvl 
|-- w:r

XML层级结构例子

xml
<w:p w:rsidRPr="00274798" w:rsidR="00274798" w:rsidP="002C414E" w:rsidRDefault="00274798" w14:paraId="157ED312" w14:textId="77777777" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:spacing w:before="312" w:beforeLines="100" w:after="200" w:line="360" w:lineRule="auto" />
    <w:ind w:left="440" w:leftChars="200" w:right="220" w:rightChars="100" w:firstLine="440" w:firstLineChars="200" />
    <w:jc w:val="center" />
    <w:rPr>
      <w:rFonts w:ascii="仿宋_GB2312" w:eastAsia="仿宋_GB2312" />
      <w:szCs w:val="22" />
    </w:rPr>
  </w:pPr>
</w:p>

如何创建一个简单的Paragraph

c#
  DocumentFormat.OpenXml.Wordprocessing.Paragraph para = new Paragraph(
      new DocumentFormat.OpenXml.Wordprocessing.Justification(){Val = new EnumValue<EnumValue<JustificationValues>>(EnumValue<JustificationValues>.Center)},
      new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text("Hello World!"))
      );
xml
<w:p xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:pPr>
     <w:jc w:val="center" />
    </w:pPr>
    <w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:t>Hello World!</w:t>
    </w:r>
</w:p>

如何在Office软件中的段落里新增一个Paragraph

通过使用 enter 可以在Office软件中的段落里新增一个Paragraph

w:pPr

关于段落的各种设置

w:jc

横向布局相关,Justification

属性 类型 说明
center EnumValue<JustificationValues?> 居中
left EnumValue<JustificationValues?> 左对齐
right EnumValue<JustificationValues?> 右对齐
both EnumValue<JustificationValues?> 两端对齐
start EnumValue<JustificationValues?> 左对齐,仅支持2010及其以后版本
end EnumValue<JustificationValues?> 右对齐,仅支持2010及其以后版本
distribute EnumValue<JustificationValues?> 分散对齐

w:spacing

段落间距相关,Spacing

属性 类型 说明
before UInt32Value 段前间距,单位为twip
after UInt32Value 段后间距,单位为twip
line UInt32Value 行间距,单位为twip
lineRule EnumValue<LineSpacingRuleValues?> 行间距规则
beforeLines UInt32Value 段前段间距,单位为行
afterLines UInt32Value 段后段间距,单位为行

示例:1.5倍行距的段落

c#
  DocumentFormat.OpenXml.Wordprocessing.Paragraph para = new Paragraph(
     new DocumentFormat.OpenXml.Wordprocessing.SpacingBetweenLines() { Line = "360",LineRule = new EnumValue<LineSpacingRuleValues>(LineSpacingRuleValues.Auto)}
     );
xml
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:spacing w:line="360" w:lineRule="auto" />
  </w:pPr>
</w:p>

IMPORTANT

ECMA 17.3.1.33 (行与段落上方/下方的间距)

磅(Point):1 磅 = 1/72 英寸。

缇(Twip):1 缇 = 1/20 磅 = 1/1440 英寸。

12 磅 = 240 缇

w:beforeAutospacing 和 w:before 同时存在时,w:before 优先级更高

lineRule="auto" 时,w:line 的值是行距倍数(如 240=1倍,480=2倍)。

lineRule="exact" 时,w:line 是精确磅值的缇数(如 240=12磅)。

如果未指定 <w:spacing>,Word 会使用默认行距和段前段后间距为0

w:ind

段落缩进相关,Indentation

属性 类型 说明
left UInt32Value 左缩进,单位为twip
leftChars UInt32Value 左缩进字符数,单位为字符
right UInt32Value 右缩进,单位为twip
rightChars UInt32Value 右缩进字符数,单位为字符
firstLine UInt32Value 首行缩进,单位为twip
firstLineChars UInt32Value 首行缩进字符数,单位为字符
hanging UInt32Value 悬挂缩进,单位为twip

w:outlineLevel

大纲级别,OutlineLevel

属性 类型 说明
val UInt32Value 大纲级别

更多详情