1

我正在使用 C# 和 OpenXml DLL 来修改现有的 MS Word 文档。我成功地替换了文档中的一些标签,然后保存了修改,但我还不能删除部分文本。

例如,我的文档有很多标题(Heading1 文本样式),然后是正文,我想以编程方式删除给定的标题和所有后续文本,直到下一个标题。

原始文件示例:

Heading 1 Body text 1 ... ...

Heading 2 Body text 2 ... ...

Heading 3 Body text 3 ... ...

如果用户想要删除标题 2,输出文档应导致:

Heading 1 Body text 1 ... ...

Heading 3 Body text 3 ... ...

我会以正确的方式做到这一点吗?有谁知道怎么做?

4

2 回答 2

2

这取决于数据(段落)的组织方式。

如果标题和段落彼此相邻,只需循环浏览段落,找到带有标题的段落并删除下一个段落。

bool remove = false;

foreach(Paragraph p in body.Descendants<Paragraph>()) {

    if (remove)
    {
        p.Remove();
        remove = !remove;
        continue;
    }

    if(p.InnerText.Contains("Heading 2")) {

        p.Remove();
        remove = !remove;

    }

}
于 2012-11-26T14:56:02.390 回答
1

我包括我用来解决问题的代码:

        List<OpenXmlElement> ElementsToDeleteList = new List<OpenXmlElement>();
        bool IsParagraphsToDelete = false;
        ...
        // Execute headings removal
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(sOutputFileName, true))
        {
            foreach (OpenXmlElement element in wordDoc.MainDocumentPart.RootElement.Descendants())
            {
                if (element.GetType() == typeof(Paragraph))
                {
                    Paragraph paragraph = (Paragraph)element;
                    if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.ParagraphStyleId != null &&
                        paragraph.ParagraphProperties.ParagraphStyleId.Val != null && paragraph.ParagraphProperties.ParagraphStyleId.Val.Value != null)
                    {
                        if (paragraph.ParagraphProperties.ParagraphStyleId.Val.Value.ToLower().Contains(MainHeaderStyleName.ToLower()) ||
                            paragraph.ParagraphProperties.ParagraphStyleId.Val.Value.ToLower().Contains(SecondaryHeaderStyleName.ToLower()))
                        {
                            StringBuilder sb = new StringBuilder();
                            foreach (var run in paragraph.Elements<Run>())
                                sb.Append(run.InnerText);

                            string ChapterTitle = sb.ToString().Trim().ToUpper();
                            IsParagraphsToDelete = ListOfDocumentTests.Where(x => x.Title.ToUpper().Trim() == ChapterTitle && x.IsIncluded == false).FirstOrDefault() != null;

                            if (string.IsNullOrEmpty(ChapterTitle) && !IsParagraphsToDelete)
                                ElementsToDeleteList.Add(paragraph);
                        }
                    }
                }

                if (IsParagraphsToDelete && (element.GetType() == typeof(Paragraph) || element.GetType() == typeof(Table)))
                {
                    ElementsToDeleteList.Add(element);
                }

            }

            foreach (OpenXmlElement elemToDelete in ElementsToDeleteList)
            {
                elemToDelete.RemoveAllChildren();
                elemToDelete.Remove();
            }


            wordDoc.MainDocumentPart.Document.Save();

        }
于 2012-12-05T08:03:01.817 回答