参考下用Spire.Doc.dll做的Demo,这里获取Word目录考虑了目录内容是否是包含在内容控件里面的两种情况,具体代码如下: using Spire.Doc; using Spire.Doc.Fields; using Spire.Doc.Documents; using System.Text.RegularExpressions; namespace TEST { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("example1.docx"); Section section = document.Sections[0]; Document newDoc = new Document(); Section newSection = newDoc.AddSection(); Paragraph p; for (int i = 0; i < section.Body.ChildObjects.Count; i++) { if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTag) { StructureDocumentTag tag = section.Body.ChildObjects[i] as StructureDocumentTag; foreach (DocumentObject obj in tag.ChildObjects) { if (obj is Paragraph) { if (IsTocParagraph(obj as Paragraph)) { newSection.Body.ChildObjects.Add(tag.Clone()); break; } } } } else { if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph) { p = section.Body.ChildObjects[i] as Paragraph; if (IsTocParagraph(p)) { newSection.Body.ChildObjects.Add(p.Clone()); } else { if (IsFieldEnd(p)) { newSection.Body.ChildObjects.Add(p.Clone()); } } } } } string result = "output.docx"; newDoc.SaveToFile(result, FileFormat.Docx); System.Diagnostics.Process.Start(result); } static bool IsTocParagraph(Paragraph paragraph) { Regex regex = new Regex("TOC\\w+", RegexOptions.IgnoreCase); if (regex.IsMatch(paragraph.StyleName)) { return true; } return false; } static bool IsFieldEnd(Paragraph paragraph) { Body body = paragraph.OwnerTextBody; int index = body.ChildObjects.IndexOf(paragraph); for (int i = 0; i < paragraph.ChildObjects.Count; i++) { if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.FieldMark) { if ((paragraph.ChildObjects[i] as FieldMark).Type == FieldMarkType.FieldEnd) { if ((body.ChildObjects[index - 1].DocumentObjectType == DocumentObjectType.Paragraph) && IsTocParagraph(body.ChildObjects[index - 1] as Paragraph)) { return true; } } } } return false; } } }
using Spire.Doc; using Spire.Doc.Fields; using Spire.Doc.Documents; using System.Text.RegularExpressions; namespace TEST { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("example1.docx"); Section section = document.Sections[0]; Document newDoc = new Document(); Section newSection = newDoc.AddSection(); Paragraph p; for (int i = 0; i < section.Body.ChildObjects.Count; i++) { if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTag) { StructureDocumentTag tag = section.Body.ChildObjects[i] as StructureDocumentTag; foreach (DocumentObject obj in tag.ChildObjects) { if (obj is Paragraph) { if (IsTocParagraph(obj as Paragraph)) { newSection.Body.ChildObjects.Add(tag.Clone()); break; } } } } else { if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph) { p = section.Body.ChildObjects[i] as Paragraph; if (IsTocParagraph(p)) { newSection.Body.ChildObjects.Add(p.Clone()); } else { if (IsFieldEnd(p)) { newSection.Body.ChildObjects.Add(p.Clone()); } } } } } string result = "output.docx"; newDoc.SaveToFile(result, FileFormat.Docx); System.Diagnostics.Process.Start(result); } static bool IsTocParagraph(Paragraph paragraph) { Regex regex = new Regex("TOC\\w+", RegexOptions.IgnoreCase); if (regex.IsMatch(paragraph.StyleName)) { return true; } return false; } static bool IsFieldEnd(Paragraph paragraph) { Body body = paragraph.OwnerTextBody; int index = body.ChildObjects.IndexOf(paragraph); for (int i = 0; i < paragraph.ChildObjects.Count; i++) { if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.FieldMark) { if ((paragraph.ChildObjects[i] as FieldMark).Type == FieldMarkType.FieldEnd) { if ((body.ChildObjects[index - 1].DocumentObjectType == DocumentObjectType.Paragraph) && IsTocParagraph(body.ChildObjects[index - 1] as Paragraph)) { return true; } } } } return false; } } }
62,046
社区成员
669,049
社区内容
加载中
.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。
希望和大家一起共同营造一个活跃、友好的社区氛围。
试试用AI创作助手写篇文章吧