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

查看:373
本文介绍了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);

查看全文

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