标题说明了我得到的错误。我正在尝试使用OpenXml 隐藏单词doc中的所有文本。目前,当我尝试追加段落属性时,我收到上述错误。我在网上找不到这个错误。
返回错误的代码
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(mDoc_copy, true))
{
// Manage namespaces to perform XPath queries.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);
// Get the document part from the package.
// Load the XML in the document part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(wdDoc.MainDocumentPart.GetStream());
MainDocumentPart main = wdDoc.MainDocumentPart;
IEnumerable<OpenXmlElement> elem = main.Document.Body.Descendants().ToList();
Paragraph p;
ParagraphProperties pp = new ParagraphProperties();
ParagraphMarkRunProperties prmp = new ParagraphMarkRunProperties();
Vanish v = new Vanish();
apprmp.AppendChild<Vanish>(v);
pp.AppendChild<ParagraphMarkRunProperties>(apprmp);
foreach (Paragraph para in main.Document.Body.Descendants<Paragraph>().ToList())
{
para.ParagraphProperties = pp;
}
}
答案 0 :(得分:33)
通常,可以通过克隆导致异常的任何节点然后插入该克隆值来修复此错误。像这样:
LeftBorder leftBorder = new LeftBorder() { Style = BorderStyleValues.Thin };
TopBorder topBorder = new TopBorder() { Style = BorderStyleValues.Thin };
RightBorder rightBorder = new RightBorder() { Style = BorderStyleValues.Thin };
BottomBorder bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin };
Color color = new Color() { Auto = true, Rgb = rgbHexValue == string.Empty ? new HexBinaryValue("00000000") : new HexBinaryValue(rgbHexValue) };
leftBorder.Color = color;
topBorder.Color = (Color)color.CloneNode(true);
rightBorder.Color = (Color)color.CloneNode(true);
bottomBorder.Color = (Color)color.CloneNode(true);
这将创建一个Color
实例,然后通过克隆原始实例然后插入它来对所有边框使用相同的实例。