在openxml中插入段落的连续编号列表

时间:2018-07-16 13:38:48

标签: c# c#-4.0 openxml openxml-sdk

我有一个带编号列表段落的Word文档。 例如

  1. 列表项
  2. Google新闻是由Google开发的新闻汇总器和应用。它提出了一个 持续,可定制的,由成千上万条整理的文章流 发布者。
  3. 第3段。

我的要求是继续插入另外两个编号的列表4.,5等段落(带文本)。并且还应保留前一段的格式。

使用当前代码,编号列表从1开始,而不是4。没有弄错我在哪里。

NumberingDefinitionsPart numberingPart = Document.MainDocumentPart.NumberingDefinitionsPart;
var abstractNumId = numberingPart.Numbering.Elements<AbstractNum>().Count() + 1;
AbstractNum abstractNum1 = new AbstractNum() { AbstractNumberId = abstractNumId };
abstractNum1.SetAttribute(new OpenXmlAttribute("w15", "restartNumberingAfterBreak", "http://schemas.microsoft.com/office/word/2012/wordml", "0"));

Paragraph paragraph15 = new Paragraph() { RsidParagraphMarkRevision = "005B5111", RsidParagraphAddition = "0012034F", RsidParagraphProperties = "005B5111", RsidRunAdditionDefault = "004A6864", ParagraphId = "555A4D6E", TextId = "49553577" };

ParagraphProperties paragraphProperties11 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId11 = new ParagraphStyleId() { Val = "ListParagraph" };
Level level2 = new Level() { LevelIndex = 6, TemplateCode = "04090019", Tentative = true };
StartNumberingValue startNumberingValue2 = new StartNumberingValue() { Val = 1 };
NumberingFormat numberingFormat2 = new NumberingFormat() { Val = NumberFormatValues.LowerLetter };

LevelJustification levelJustification2 = new LevelJustification() { Val = LevelJustificationValues.Left };

PreviousParagraphProperties previousParagraphProperties2 = new PreviousParagraphProperties();

NumberingProperties numberingProperties1 = new NumberingProperties();
NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference() { Val = 0 };
NumberingId numberingId1 = new NumberingId() { Val = 4 };
LevelText levelText2 = new LevelText() { Val = "%7." };
level2.Append(startNumberingValue2);
level2.Append(numberingFormat2);
level2.Append(levelText2);
level2.Append(levelJustification2);
level2.Append(previousParagraphProperties2);

abstractNum1.Append(level2);

numberingProperties1.Append(numberingLevelReference1);
numberingProperties1.Append(numberingId1);

ParagraphMarkRunProperties paragraphMarkRunProperties8 = new ParagraphMarkRunProperties();
RunFonts runFonts54 = new RunFonts() { Ascii = "Arial", HighAnsi = "Arial", ComplexScript = "Arial" };
Color color1 = new Color() { Val = "626262" };
FontSize fontSize56 = new FontSize() { Val = "20" };
FontSizeComplexScript fontSizeComplexScript6 = new FontSizeComplexScript() { Val = "20" };
Shading shading1 = new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "FFFFFF" };

paragraphMarkRunProperties8.Append(runFonts54);
paragraphMarkRunProperties8.Append(color1);
paragraphMarkRunProperties8.Append(fontSize56);
paragraphMarkRunProperties8.Append(fontSizeComplexScript6);
paragraphMarkRunProperties8.Append(shading1);

paragraphProperties11.Append(paragraphStyleId11);
paragraphProperties11.Append(numberingProperties1);
paragraphProperties11.Append(paragraphMarkRunProperties8);
BookmarkStart bookmarkStart1 = new BookmarkStart() { Name = "_GoBack", Id = "0" };
BookmarkEnd bookmarkEnd1 = new BookmarkEnd() { Id = "6" };

Run run45 = new Run() { RsidRunProperties = "005B5111" };

RunProperties runProperties41 = new RunProperties();
RunFonts runFonts55 = new RunFonts() { Ascii = "Arial", HighAnsi = "Arial", ComplexScript = "Arial" };
Color color2 = new Color() { Val = "626262" };
FontSize fontSize57 = new FontSize() { Val = "20" };
FontSizeComplexScript fontSizeComplexScript7 = new FontSizeComplexScript() { Val = "20" };
Shading shading2 = new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "FFFFFF" };

runProperties41.Append(runFonts55);
runProperties41.Append(color2);
runProperties41.Append(fontSize57);
runProperties41.Append(fontSizeComplexScript7);
runProperties41.Append(shading2);
Text text21 = new Text();
text21.Text = "agency or by the Public Records";
run45.Append(runProperties41);
run45.Append(text21);

Run run46 = new Run();

RunProperties runProperties42 = new RunProperties();
RunFonts runFonts56 = new RunFonts() { Ascii = "Arial", HighAnsi = "Arial", ComplexScript = "Arial" };
Color color3 = new Color() { Val = "626262" };
FontSize fontSize58 = new FontSize() { Val = "20" };
FontSizeComplexScript fontSizeComplexScript8 = new FontSizeComplexScript() { Val = "20" };
Shading shading3 = new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "FFFFFF" };

runProperties42.Append(runFonts56);
runProperties42.Append(color3);
runProperties42.Append(fontSize58);
runProperties42.Append(fontSizeComplexScript8);
runProperties42.Append(shading3);
Text text22 = new Text();
text22.Text = ".";

run46.Append(runProperties42);
run46.Append(text22);

paragraph15.Append(paragraphProperties11);
paragraph15.Append(bookmarkStart1);
paragraph15.Append(bookmarkEnd1);
paragraph15.Append(run45);
paragraph15.Append(run46);
Document.MainDocumentPart.Document.Body.Append(paragraph15);

1 个答案:

答案 0 :(得分:0)

问题是使用new NumberingId() { Val = 4 };,如果您想继续现有列表,请从该列表中获取NumberingId(任何段落,但最简单/逻辑上您可能会使用最后一个)并使用它。

如果使用“生产力工具”查看底层的Open XML,则会在document.xml中看到类似的内容。请注意,这是Word Open XML,它定义了两个段落-两者都是相同编号序列(列表)的成员。

<w:p><w:pPr><w:pStyle w:val="ListParagraph"/>
      <w:numPr><w:ilvl w:val="0"/><w:numId w:val="5"/></w:numPr>
      <w:tabs><w:tab w:val="right" w:pos="8789"/></w:tabs></w:pPr>
         <w:r><w:t xml:space="preserve">This is a numbered line</w:t></w:r>
</w:p>
<w:p><w:pPr><w:pStyle w:val="ListParagraph"/>
      <w:numPr><w:ilvl w:val="0"/><w:numId w:val="5"/></w:numPr>
      <w:tabs><w:tab w:val="right" w:pos="8789"/></w:tabs></w:pPr>
         <w:r><w:t xml:space="preserve">Another numbered line</w:t></w:r>
</w:p>

请注意两件事:<w:pStyle w:val="ListParagraph"/><w:numId w:val="5"/>

其中第一个定义编号的外观-在这种情况下,它是一种内置样式。您无需为此做任何事情,但这很“高兴”。

第二个告诉Word在哪里查看numbering.xml中的编号顺序。这就是使列表“在一起”的原因。此元素对应于SDK中的NumberingId

您可以看到上面两个段落使用相同的ID。如果它们被编号为1和2,并且用户进行了切换(例如通过拖动),则编号将进行调整。 1等于2,反之亦然。这仅是因为Word以这种方式处理编号。如果要创建一个段落,将其放在具有相同编号ID的这两者之间,则段落编号将再次自动调整为1、2、3。