无法使用 OpenXml 为单词添加项目符号
我的预期结果是:
- 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);
结果是:
- Hello
- 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:
- Hello
- world!
How can i get my expected result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到这已经太晚了,但也许它可以帮助其他有同样问题的人。标记的答案(由阿穆拉)实际上并没有达到预期的结果。它只是创建一个以列表为内容的文档,只是比您更完整。您添加到主文档部分的内容很好。
在 XML 格式中,列表项被定义为具有缩进级别和编号 ID 的段落。此 ID 引用文档的 NumberingDefinitionsPart 中定义的编号规则。
在您的例子中,由于您已将编号 ID 设置为 1,因此以下代码将映射该 ID 1 以反映所需的项目符号列表。请注意 Level 对象内的 NumberingFormat 和 LevelText 对象。这些是格式化的关键组成部分。
有关详细信息,请查看 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.
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.
这应该会为您创建一个包含预期输出的空白文档:
This should create you a blank document with your expected output: