Cannot insert the OpenXmlElement “newChild” because it is part of a tree

前端 未结 1 1449
-上瘾入骨i
-上瘾入骨i 2020-12-17 14:26

The Title states the error I am getting. I\'m trying to hide all the text in a word doc using OpenXml. Currently when I try and append the Paragraph propert

相关标签:
1条回答
  • 2020-12-17 15:05

    Normally this error can be fixed by Cloning whatever node is causing the exception and then inserting that cloned value. Something like this:

    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);
    

    This will create one Color instance and then use the same instance for all the borders by cloning the original instance then inserting it.

    0 讨论(0)
提交回复
热议问题