我正在打开一个.docx文件,使用下面的代码遍历所有的内容控件,并将那里的文本设置为“foo文本”。这对于文档正文中的内容控件非常有用。问题是它从不更新页眉和页脚中的内容控件。我遗漏了什么吗?
using (WordprocessingDocument doc = WordprocessingDocument.Open(resultDocumentPath, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
foreach (SdtElement sdt in mainPart.Document.Descendants<SdtElement>())
{
Run formattedRun = new Run();
RunProperties runProperties = new RunProperties();
runProperties.Append(new Text("the foo bar text"));
formattedRun.Append(runProperties);
sdt.Append(new Paragraph(new Run(formattedRun)));
}
}发布于 2017-04-30 04:29:31
标头和页脚存储在与主文档主体分开的部分中。
在原始OpenXML标记中,相应的文件是"header#.xml“和"footer#.xml”。
在OpenXML SDK中,您需要遍历MainDocumentPart.HeaderParts和MainDocumentPart.FooterParts。
https://stackoverflow.com/questions/43664315
复制相似问题