OpenXML 2 SDK - Word 文档 - 以编程方式创建项目符号列表 [英] OpenXML 2 SDK - Word document - Create bulleted list programmatically

查看:32
本文介绍了OpenXML 2 SDK - Word 文档 - 以编程方式创建项目符号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 OpenXML SDK、2.0 CTP,我尝试以编程方式创建 Word 文档.在我的文档中,我必须插入一个项目符号列表,列表中的某些元素必须加下划线.我该怎么做?

Using the OpenXML SDK, 2.0 CTP, I am trying to programmatically create a Word document. In my document I have to insert a bulleted list, an some of the elements of the list must be underlined. How can I do this?

推荐答案

OpenXML 中的列表有点混乱.

Lists in OpenXML are a little confusing.

有一个 NumberingDefinitionsPart 描述文档中的所有列表.它包含有关列表应如何显示(项目符号、编号等)的信息,并为每个列表分配 ID.

There is a NumberingDefinitionsPart that describes all of the lists in the document. It contains information on how the lists should appear (bulleted, numbered, etc.) and also assigns and ID to each one.

然后在 MainDocumentPart 中,对于您要创建的列表中的每个项目,您添加一个新段落并为该段落分配您想要的列表 ID.

Then in the MainDocumentPart, for every item in the list you want to create, you add a new paragraph and assign the ID of the list you want to that paragraph.

所以要创建一个项目符号列表,例如:

So to create a bullet list such as:

  • 你好
  • 世界!

您首先必须创建一个 NumberingDefinitionsPart:

You would first have to create a NumberingDefinitionsPart:

    NumberingDefinitionsPart numberingPart =
      mainDocumentPart.AddNewPart<NumberingDefinitionsPart>("someUniqueIdHere");

    Numbering element = 
      new Numbering(
        new AbstractNum(
          new Level(
            new NumberingFormat() { Val = NumberFormatValues.Bullet },
            new LevelText() { Val = "·" }
          ) { LevelIndex = 0 }
        ) { AbstractNumberId = 1 },
        new NumberingInstance(
          new AbstractNumId() { Val = 1 }
        ) { NumberID = 1 });

    element.Save(numberingPart);

然后像往常一样创建 MainDocumentPart,除了在段落属性中,分配编号 ID:

Then you create the MainDocumentPart as you normally would, except in the paragraph properties, assign the numbering ID:

    MainDocumentPart mainDocumentPart =
      package.AddMainDocumentPart();

    Document element = 
      new Document(
        new Body(
          new Paragraph(
            new ParagraphProperties(
              new NumberingProperties(
                new NumberingLevelReference() { Val = 0 },
                new NumberingId() { Val = 1 })),
            new Run(
              new RunProperties(),
              new Text("Hello, ") { Space = "preserve" })),
          new Paragraph(
            new ParagraphProperties(
              new NumberingProperties(
                new NumberingLevelReference() { Val = 0 },
                new NumberingId() { Val = 1 })),
            new Run(
              new RunProperties(),
              new Text("world!") { Space = "preserve" }))));

    element.Save(mainDocumentPart);

OpenXML 参考指南 2.9 节.

这篇关于OpenXML 2 SDK - Word 文档 - 以编程方式创建项目符号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆