【问题标题】:Remove permStart and permEnd tags from document docx从文档 docx 中删除 permStart 和 permEnd 标签
【发布时间】:2012-07-21 07:09:25
【问题描述】:

我有 2 个 docx 文档,这些文档在文档级别是只读的。但是这些文档中少数段落的编辑选项是通过使用 permStart 和 permEnd 标签启用的。

我必须合并这 2 个文档并使新文档可编辑。我正在使用 PowerTools DocumentBuilder 来合并这两个 docx。最终的 docx 是可编辑的,但由于存在 permStart 和 permEnd 标签,所有段落都以灰色背景突出显示。

我想知道如何删除这些 permStart 和 permEnd 标签。我尝试了以下代码但无法正常工作。

wordD.MainDocumentPart.Document.Body.RemoveAllChildren(); wordD.MainDocumentPart.Document.Body.RemoveAllChildren();

我正在使用 OpenXML SDK2.0、VS2010、.NET 4.0 和 Powertools 文档生成器。任何帮助都会很棒。

谢谢!

【问题讨论】:

    标签: c# .net openxml


    【解决方案1】:

    您需要删除文档保护。除此之外,您还需要删除 PermStart 和 PermEnd,因为这些标签仅在文档受到保护时才相关。代码将是

    1. 删除文档保护。

      DocumentSettingsPart documentSettingsPart = wordprocessingDocument.MainDocumentPart.GetPartsOfType().FirstOrDefault();

          if (documentSettingsPart != null)
          {
              documentSettingsPart.Settings.RemoveAllChildren<DocumentProtection>();
          }
      
    2. 删除 PermStart 和 PermEnd 标签,就像你已经在做的那样

      wordD.MainDocumentPart.Document.Body.RemoveAllChildren(); wordD.MainDocumentPart.Document.Body.RemoveAllChildren(); wordD.MainDocumentPart.Document.Save();

    【讨论】:

      【解决方案2】:

      这是我删除 permStart 和 permEnd 标记的方法。欢迎对以下代码进行任何改进。

                      foreach (PermStart p1 in wordD.MainDocumentPart.Document.Body.Descendants<PermStart>())
                      {
                          p1.Parent.RemoveChild<PermStart>(p1);
                      }
      
                      foreach (PermEnd p2 in wordD.MainDocumentPart.Document.Body.Descendants<PermEnd>())
                      {
                          p2.Parent.RemoveChild<PermEnd>(p2);
                      }
                      wordD.MainDocumentPart.Document.Save();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多