|
| 1 | +// See https://aka.ms/new-console-template for more information |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.DocIO; |
| 4 | + |
| 5 | +using (WordDocument document = new WordDocument()) |
| 6 | +{ |
| 7 | + //Opens the Word template document. |
| 8 | + Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Data/TOC.docx")); |
| 9 | + document.Open(docStream, FormatType.Docx); |
| 10 | + docStream.Dispose(); |
| 11 | + //Removes the TOC field. |
| 12 | + TableOfContent toc = document.Sections[0].Body.Paragraphs[2].Items[0] as TableOfContent; |
| 13 | + RemoveTableOfContents(toc); |
| 14 | + //Saves the file in the given path |
| 15 | + docStream = File.Create(Path.GetFullPath(@"../../../Sample.docx")); |
| 16 | + document.Save(docStream, FormatType.Docx); |
| 17 | + docStream.Dispose(); |
| 18 | +} |
| 19 | + |
| 20 | +#region Helper methods |
| 21 | +/// <summary> |
| 22 | +/// Removes the table of contents from Word document. |
| 23 | +/// </summary> |
| 24 | +/// <param name="toc"></param> |
| 25 | +static void RemoveTableOfContents(TableOfContent toc) |
| 26 | +{ |
| 27 | + //Finds the last TOC item. |
| 28 | + Entity lastItem = FindLastTOCItem(toc); |
| 29 | + |
| 30 | + //TOC field end mark wasn't exist. |
| 31 | + if (lastItem == null) |
| 32 | + return; |
| 33 | + |
| 34 | + //Inserts the bookmark start before the TOC instance. |
| 35 | + BookmarkStart bkmkStart = new BookmarkStart(toc.Document, "tableOfContent"); |
| 36 | + toc.OwnerParagraph.Items.Insert(toc.OwnerParagraph.Items.IndexOf(toc), bkmkStart); |
| 37 | + |
| 38 | + //Inserts the bookmark end to next of TOC last item. |
| 39 | + BookmarkEnd bkmkEnd = new BookmarkEnd(toc.Document, "tableOfContent"); |
| 40 | + WParagraph paragraph = lastItem.Owner as WParagraph; |
| 41 | + paragraph.Items.Insert(paragraph.Items.IndexOf(lastItem) + 1, bkmkEnd); |
| 42 | + |
| 43 | + //Delete all the items from bookmark start to end (TOC items) using Bookmark Navigator. |
| 44 | + DeleteBookmarkContents(bkmkEnd.Name, toc.Document); |
| 45 | +} |
| 46 | +/// <summary> |
| 47 | +/// Delete the bookmark items. |
| 48 | +/// </summary> |
| 49 | +/// <param name="bkmkName"></param> |
| 50 | +/// <param name="document"></param> |
| 51 | +static void DeleteBookmarkContents(string bkmkName, WordDocument document) |
| 52 | +{ |
| 53 | + //Creates the bookmark navigator instance to access the bookmark |
| 54 | + BookmarksNavigator navigator = new BookmarksNavigator(document); |
| 55 | + //Moves the virtual cursor to the location before the end of the bookmark "tableOfContent". |
| 56 | + navigator.MoveToBookmark(bkmkName); |
| 57 | + //Deletes the bookmark content. |
| 58 | + navigator.DeleteBookmarkContent(false); |
| 59 | + //Gets the bookmark instance by using FindByName method of BookmarkCollection with bookmark name. |
| 60 | + Bookmark bookmark = document.Bookmarks.FindByName(bkmkName); |
| 61 | + //Removes the bookmark named "tableOfContent" from Word document. |
| 62 | + document.Bookmarks.Remove(bookmark); |
| 63 | +} |
| 64 | +/// <summary> |
| 65 | +/// Finds the last TOC item. |
| 66 | +/// </summary> |
| 67 | +/// <param name="toc"></param> |
| 68 | +/// <returns></returns> |
| 69 | +static Entity FindLastTOCItem(TableOfContent toc) |
| 70 | +{ |
| 71 | + int tocIndex = toc.OwnerParagraph.Items.IndexOf(toc); |
| 72 | + //TOC may contains nested fields and each fields has its owner field end mark |
| 73 | + //so to identify the TOC Field end mark (WFieldMark instance) used the stack. |
| 74 | + Stack<Entity> fieldStack = new Stack<Entity>(); |
| 75 | + fieldStack.Push(toc); |
| 76 | + |
| 77 | + //Finds whether TOC end item is exist in same paragraph. |
| 78 | + for (int i = tocIndex + 1; i < toc.OwnerParagraph.Items.Count; i++) |
| 79 | + { |
| 80 | + Entity item = toc.OwnerParagraph.Items[i]; |
| 81 | + |
| 82 | + if (item is WField) |
| 83 | + fieldStack.Push(item); |
| 84 | + else if (item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd) |
| 85 | + { |
| 86 | + if (fieldStack.Count == 1) |
| 87 | + { |
| 88 | + fieldStack.Clear(); |
| 89 | + return item; |
| 90 | + } |
| 91 | + else |
| 92 | + fieldStack.Pop(); |
| 93 | + } |
| 94 | + } |
| 95 | + return FindLastItemInTextBody(toc, fieldStack); |
| 96 | +} |
| 97 | +/// <summary> |
| 98 | +/// Finds the last TOC item from consequence text body items. |
| 99 | +/// </summary> |
| 100 | +/// <param name="toc"></param> |
| 101 | +/// <param name="fieldStack"></param> |
| 102 | +/// <returns></returns> |
| 103 | +static Entity FindLastItemInTextBody(TableOfContent toc, Stack<Entity> fieldStack) |
| 104 | +{ |
| 105 | + WTextBody tBody = toc.OwnerParagraph.OwnerTextBody; |
| 106 | + |
| 107 | + //Finds whether TOC end item is exist in text body items. |
| 108 | + for (int i = tBody.ChildEntities.IndexOf(toc.OwnerParagraph) + 1; i < tBody.ChildEntities.Count; i++) |
| 109 | + { |
| 110 | + WParagraph paragraph = null; |
| 111 | + if (tBody.ChildEntities[i] is WParagraph) |
| 112 | + paragraph = tBody.ChildEntities[i] as WParagraph; |
| 113 | + else |
| 114 | + continue; |
| 115 | + |
| 116 | + foreach (Entity item in paragraph.Items) |
| 117 | + { |
| 118 | + if (item is WField) |
| 119 | + fieldStack.Push(item); |
| 120 | + else if (item is WFieldMark && (item as WFieldMark).Type == FieldMarkType.FieldEnd) |
| 121 | + { |
| 122 | + if (fieldStack.Count == 1) |
| 123 | + { |
| 124 | + fieldStack.Clear(); |
| 125 | + return item; |
| 126 | + } |
| 127 | + else |
| 128 | + fieldStack.Pop(); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + return null; |
| 133 | +} |
| 134 | +#endregion |
0 commit comments