无法使用 OpenXml 为单词添加项目符号

发布于 2024-12-18 11:34:55 字数 1519 浏览 5 评论 0原文

我的预期结果是:

  • Hello
  • world!

但是当我使用下面的代码时:

        MainDocumentPart mainDocumentPart =
          package.AddMainDocumentPart();

        DocumentFormat.OpenXml.Wordprocessing.Document elementW =
          new DocumentFormat.OpenXml.Wordprocessing.Document(
            new Body(
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                    new NumberingProperties(
                      new NumberingLevelReference() { Val = 0 },
                      new NumberingId() { Val = 1 })
                    ),
                new Run(
                  new RunProperties(),
                  new Text("Hello, ") { Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" } })),
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                new ParagraphProperties(
                  new NumberingProperties(
                    new NumberingLevelReference() { Val = 0 },
                    new NumberingId() { Val = 1 })),
                new Run(
                  new RunProperties(),
                  new Text("world!")
                  {
                      Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
                  })));

        elementW.Save(mainDocumentPart);

结果是:

  1. Hello
  2. world!

我怎样才能得到我预期的结果?

My expected result is:

  • Hello
  • world!

but when i using below codes:

        MainDocumentPart mainDocumentPart =
          package.AddMainDocumentPart();

        DocumentFormat.OpenXml.Wordprocessing.Document elementW =
          new DocumentFormat.OpenXml.Wordprocessing.Document(
            new Body(
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                    new NumberingProperties(
                      new NumberingLevelReference() { Val = 0 },
                      new NumberingId() { Val = 1 })
                    ),
                new Run(
                  new RunProperties(),
                  new Text("Hello, ") { Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" } })),
              new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
                new ParagraphProperties(
                  new NumberingProperties(
                    new NumberingLevelReference() { Val = 0 },
                    new NumberingId() { Val = 1 })),
                new Run(
                  new RunProperties(),
                  new Text("world!")
                  {
                      Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
                  })));

        elementW.Save(mainDocumentPart);

Result is:

  1. Hello
  2. world!

How can i get my expected result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2024-12-25 11:34:55

我意识到这已经太晚了,但也许它可以帮助其他有同样问题的人。标记的答案(由阿穆拉)实际上并没有达到预期的结果。它只是创建一个以列表为内容的文档,只是比您更完整。您添加到主文档部分的内容很好。

在 XML 格式中,列表项被定义为具有缩进级别和编号 ID 的段落。此 ID 引用文档的 NumberingDefinitionsPart 中定义的编号规则。

在您的例子中,由于您已将编号 ID 设置为 1,因此以下代码将映射该 ID 1 以反映所需的项目符号列表。请注意 Level 对象内的 NumberingFormat 和 LevelText 对象。这些是格式化的关键组成部分。

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

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

numElement.Save(numberingPart);

有关详细信息,请查看 MSDN 上的字处理命名空间< 上所有相关类的文档/a> 或 使用编号标记文章。

I realize this is far too late but maybe it can help others with the same question. The marked answer (by amurra) doesn't actually achieve the desired result. It simply creates a document with the list as content, just more completely than you. What you have added to the main document part is fine.

In the XML format, list items are defined as paragraphs with an indentation level and a numbering ID. This ID references the numbering rules defined in the NumberingDefinitionsPart of the document.

In your case, because you've set the numbering ID to be 1, the following code would map that ID of 1 to reflect a bulleted list as desired. Note the NumberingFormat and LevelText objects inside the Level object. These are the key components for your formatting.

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

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

numElement.Save(numberingPart);

For more information, check out the documentation for all the related classes on the Wordprocessing Namespace on MSDN, or the Working With Numbering markup article.

瀟灑尐姊 2024-12-25 11:34:55

这应该会为您创建一个包含预期输出的空白文档:

        // Creates an Document instance and adds its children.
        public Document GenerateDocument()
        {
            Document document1 = new Document();
            document1.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");

            Body body1 = new Body();

            Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00AF4948", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "ListParagraph" };

            NumberingProperties numberingProperties1 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference(){ Val = 0 };
            NumberingId numberingId1 = new NumberingId(){ Val = 1 };

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

            paragraphProperties1.Append(paragraphStyleId1);
            paragraphProperties1.Append(numberingProperties1);

            Run run1 = new Run();
            Text text1 = new Text();
            text1.Text = "Hello";

            run1.Append(text1);

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(run1);

            Paragraph paragraph2 = new Paragraph(){ RsidParagraphAddition = "00625634", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" };

            ParagraphProperties paragraphProperties2 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId(){ Val = "ListParagraph" };

            NumberingProperties numberingProperties2 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference2 = new NumberingLevelReference(){ Val = 0 };
            NumberingId numberingId2 = new NumberingId(){ Val = 1 };

            numberingProperties2.Append(numberingLevelReference2);
            numberingProperties2.Append(numberingId2);

            paragraphProperties2.Append(paragraphStyleId2);
            paragraphProperties2.Append(numberingProperties2);

            Run run2 = new Run();
            Text text2 = new Text();
            text2.Text = "world!";

            run2.Append(text2);

            paragraph2.Append(paragraphProperties2);
            paragraph2.Append(run2);

            SectionProperties sectionProperties1 = new SectionProperties(){ RsidR = "00625634", RsidSect = "00AF4948" };
            HeaderReference headerReference1 = new HeaderReference(){ Type = HeaderFooterValues.Even, Id = "rId7" };
            HeaderReference headerReference2 = new HeaderReference(){ Type = HeaderFooterValues.Default, Id = "rId8" };
            FooterReference footerReference1 = new FooterReference(){ Type = HeaderFooterValues.Even, Id = "rId9" };
            FooterReference footerReference2 = new FooterReference(){ Type = HeaderFooterValues.Default, Id = "rId10" };
            HeaderReference headerReference3 = new HeaderReference(){ Type = HeaderFooterValues.First, Id = "rId11" };
            FooterReference footerReference3 = new FooterReference(){ Type = HeaderFooterValues.First, Id = "rId12" };
            PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
            PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
            Columns columns1 = new Columns(){ Space = "720" };
            DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };

            sectionProperties1.Append(headerReference1);
            sectionProperties1.Append(headerReference2);
            sectionProperties1.Append(footerReference1);
            sectionProperties1.Append(footerReference2);
            sectionProperties1.Append(headerReference3);
            sectionProperties1.Append(footerReference3);
            sectionProperties1.Append(pageSize1);
            sectionProperties1.Append(pageMargin1);
            sectionProperties1.Append(columns1);
            sectionProperties1.Append(docGrid1);

            body1.Append(paragraph1);
            body1.Append(paragraph2);
            body1.Append(sectionProperties1);

            document1.Append(body1);
            return document1;
        }

This should create you a blank document with your expected output:

        // Creates an Document instance and adds its children.
        public Document GenerateDocument()
        {
            Document document1 = new Document();
            document1.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");

            Body body1 = new Body();

            Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00AF4948", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "ListParagraph" };

            NumberingProperties numberingProperties1 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference(){ Val = 0 };
            NumberingId numberingId1 = new NumberingId(){ Val = 1 };

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

            paragraphProperties1.Append(paragraphStyleId1);
            paragraphProperties1.Append(numberingProperties1);

            Run run1 = new Run();
            Text text1 = new Text();
            text1.Text = "Hello";

            run1.Append(text1);

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(run1);

            Paragraph paragraph2 = new Paragraph(){ RsidParagraphAddition = "00625634", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" };

            ParagraphProperties paragraphProperties2 = new ParagraphProperties();
            ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId(){ Val = "ListParagraph" };

            NumberingProperties numberingProperties2 = new NumberingProperties();
            NumberingLevelReference numberingLevelReference2 = new NumberingLevelReference(){ Val = 0 };
            NumberingId numberingId2 = new NumberingId(){ Val = 1 };

            numberingProperties2.Append(numberingLevelReference2);
            numberingProperties2.Append(numberingId2);

            paragraphProperties2.Append(paragraphStyleId2);
            paragraphProperties2.Append(numberingProperties2);

            Run run2 = new Run();
            Text text2 = new Text();
            text2.Text = "world!";

            run2.Append(text2);

            paragraph2.Append(paragraphProperties2);
            paragraph2.Append(run2);

            SectionProperties sectionProperties1 = new SectionProperties(){ RsidR = "00625634", RsidSect = "00AF4948" };
            HeaderReference headerReference1 = new HeaderReference(){ Type = HeaderFooterValues.Even, Id = "rId7" };
            HeaderReference headerReference2 = new HeaderReference(){ Type = HeaderFooterValues.Default, Id = "rId8" };
            FooterReference footerReference1 = new FooterReference(){ Type = HeaderFooterValues.Even, Id = "rId9" };
            FooterReference footerReference2 = new FooterReference(){ Type = HeaderFooterValues.Default, Id = "rId10" };
            HeaderReference headerReference3 = new HeaderReference(){ Type = HeaderFooterValues.First, Id = "rId11" };
            FooterReference footerReference3 = new FooterReference(){ Type = HeaderFooterValues.First, Id = "rId12" };
            PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
            PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
            Columns columns1 = new Columns(){ Space = "720" };
            DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };

            sectionProperties1.Append(headerReference1);
            sectionProperties1.Append(headerReference2);
            sectionProperties1.Append(footerReference1);
            sectionProperties1.Append(footerReference2);
            sectionProperties1.Append(headerReference3);
            sectionProperties1.Append(footerReference3);
            sectionProperties1.Append(pageSize1);
            sectionProperties1.Append(pageMargin1);
            sectionProperties1.Append(columns1);
            sectionProperties1.Append(docGrid1);

            body1.Append(paragraph1);
            body1.Append(paragraph2);
            body1.Append(sectionProperties1);

            document1.Append(body1);
            return document1;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文