我编写了以下代码来在文档页脚的内容控件中插入一些文本。
oItem.File.CheckOut();
byte[] byteArray = oItem.File.OpenBinary();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument wp = WordprocessingDocument.Open(mem, true))
{
Boolean foundInFooter = false;
MainDocumentPart mainPart = wp.MainDocumentPart;
foreach (FooterPart footerPart in mainPart.FooterParts)
{
Word.Footer footer = footerPart.Footer;
foreach (Word.SdtElement sdt in footer.Descendants<Word.SdtElement>().ToList())
{
Word.SdtAlias alias = sdt.Descendants<Word.SdtAlias>().FirstOrDefault();
if (alias.Val.Value == "Revisionsnummer")
{
foundInFooter = true;
if (sdt.Descendants<Word.Text>().FirstOrDefault() != null)
{
sdt.Descendants<Word.Text>().FirstOrDefault().Text = (string)oItem["Version"];
}
}
}
}
}
}
出于某种原因,有时sdt.Descendants<Word.Text>().FirstOrDefault()
返回 null 所以我无法插入文本。无论如何在这些情况下添加 Word.Text ?