【问题标题】:Cumulate RunProperties on Run with OpenXML SDK 2.0在使用 OpenXML SDK 2.0 运行时累积 RunProperties
【发布时间】:2012-08-02 15:02:21
【问题描述】:
我想知道我是否可以在 Run 上累积 RunProperties
Run run = new Run(new Text("test"));
RunProperties runProperties = new RunProperties();
runProperties.AppendChild<Bold>(new Bold());
runProperties.AppendChild<Underline>(new Underline());
run.AppendChild<RunProperties>(runProperties);
在这种情况下,我只得到粗体文本但没有下划线。
请帮忙
【问题讨论】:
标签:
c#
openxml
openxml-sdk
bold
underline
【解决方案1】:
我尝试了您的示例,确实它对我来说只是粗体而不是下划线。我做了一点工作,最终得到了这个:
using (WordprocessingDocument doc = WordprocessingDocument.Open(destFileName, true))
{
Run run = new Run();
RunProperties runProperties = new RunProperties();
runProperties.AppendChild<Underline>(new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single });
runProperties.AppendChild<Bold>(new Bold());
run.AppendChild<RunProperties>(runProperties);
run.AppendChild(new Text("test"));
//Note: I had to create a paragraph element to place the run into.
Paragraph p = new Paragraph();
p.AppendChild(run);
doc.MainDocumentPart.Document.Body.AppendChild(p);
}
基本上,这改变了:
new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }
你必须指定你想要的下划线类型,而不是把它留给 Underline 类的构造函数。我刚刚指定了Single,它对我有用。