|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | + |
| 3 | +namespace Delete_bracketed_content_up_to_specific_word |
| 4 | +{ |
| 5 | + class Program |
| 6 | + { |
| 7 | + public static void Main(string[] args) |
| 8 | + { |
| 9 | + // Load the existing word document |
| 10 | + using(WordDocument document = new WordDocument(@"Data\Input.docx")) |
| 11 | + { |
| 12 | + // Find the phrase using Find API (Specific Word, case-insensitive, whole-word match) |
| 13 | + TextSelection selection = document.Find("Importadores", true, true); |
| 14 | + // Proceed only if the phrase is found |
| 15 | + if (selection != null) |
| 16 | + { |
| 17 | + // Get the paragraph that contains the selected word |
| 18 | + WParagraph paragraph = selection.GetAsOneRange().OwnerParagraph; |
| 19 | + // Get a owner section |
| 20 | + WSection ownerSection = (WSection)paragraph.OwnerTextBody.Owner; |
| 21 | + // Find the position (index) of the paragraph in the section |
| 22 | + int phraseParaIndex = ownerSection.Body.ChildEntities.IndexOf(paragraph); |
| 23 | + // Find the position of the word inside the paragraph |
| 24 | + int phraseEntityIndex = paragraph.ChildEntities.IndexOf(selection.GetAsOneRange()); |
| 25 | + // Set how many paragraphs before the word we want to check |
| 26 | + int maxPreviousParagraphs = 6; |
| 27 | + // Call a method to remove content inside brackets before the word |
| 28 | + RemoveBlock(document, paragraph, phraseParaIndex, phraseEntityIndex, ownerSection, maxPreviousParagraphs); |
| 29 | + // Save the updated document to a file |
| 30 | + document.Save(@"../../../Output/Output.docx"); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + /// <summary> |
| 35 | + /// Removes content enclosed in brackets from previous paragraphs up to a specific word in the document. |
| 36 | + /// </summary> |
| 37 | + /// <param name="document">The Word document to modify.</param> |
| 38 | + /// <param name="paragraph">The paragraph that contains the specific word or phrase.</param> |
| 39 | + /// <param name="phraseParaIndex">The index of the paragraph containing the specific word.</param> |
| 40 | + /// <param name="phraseEntityIndex">The index of the word within the paragraph.</param> |
| 41 | + /// <param name="ownerSection">The section that contains the paragraph.</param> |
| 42 | + /// <param name="maxPreviousParagraphs">The number of previous paragraphs to check for bracketed content.</param> |
| 43 | + private static void RemoveBlock(WordDocument document, WParagraph paragraph, int phraseParaIndex, int phraseEntityIndex, WSection ownerSection, int maxPreviousParagraphs) |
| 44 | + { |
| 45 | + // Initialize state |
| 46 | + int bracketCount = 0; |
| 47 | + bool isBracketFound = false; |
| 48 | + // Create a unique name for the temporary bookmark |
| 49 | + string bookmarkName = "Remove" + Guid.NewGuid().ToString(); |
| 50 | + // Store the paragraph where the opening bracket '[' is found |
| 51 | + WParagraph openingBracketParagraph = null; |
| 52 | + // Store the character position of the opening bracket '[' |
| 53 | + int openingBracketCharIndex = 0; |
| 54 | + // Loop backwards through previous paragraphs |
| 55 | + for (int i = phraseParaIndex; i >= 0 && i > phraseParaIndex - maxPreviousParagraphs; i--) |
| 56 | + { |
| 57 | + WParagraph currentParagraph = ownerSection.Paragraphs[i]; |
| 58 | + // If it's the phrase paragraph, start from the word's position; otherwise, start from the end |
| 59 | + int start = i == phraseParaIndex ? phraseEntityIndex : currentParagraph.ChildEntities.Count - 1; |
| 60 | + // Loop through entities in reverse |
| 61 | + for (int j = start; j >= 0; j--) |
| 62 | + { |
| 63 | + // Check if the entity is a text range |
| 64 | + if (currentParagraph.ChildEntities[j] is WTextRange textRange) |
| 65 | + { |
| 66 | + string text = textRange.Text; |
| 67 | + // Loop through characters in reverse |
| 68 | + for (int k = text.Length - 1; k >= 0; k--) |
| 69 | + { |
| 70 | + char ch = text[k]; |
| 71 | + // If we find a closing bracket |
| 72 | + if (ch == ']') |
| 73 | + { |
| 74 | + isBracketFound = true; |
| 75 | + bracketCount++; |
| 76 | + } |
| 77 | + // If we find an opening bracket |
| 78 | + else if (ch == '[') |
| 79 | + { |
| 80 | + // If there's no matching closing bracket, stop processing |
| 81 | + if (bracketCount == 0) |
| 82 | + return; |
| 83 | + |
| 84 | + // Reduce bracket count |
| 85 | + bracketCount--; |
| 86 | + // Save the position of the opening bracket |
| 87 | + openingBracketCharIndex = k; |
| 88 | + // Store the paragraph that contains this opening bracket, |
| 89 | + openingBracketParagraph = currentParagraph; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + // When a bracket is found and bracket count is 0, it means a complete and valid bracket pair is detected. |
| 96 | + if (isBracketFound && bracketCount == 0) |
| 97 | + { |
| 98 | + // Create and insert a bookmark start at the opening bracket position |
| 99 | + BookmarkStart bookmarkStart = new BookmarkStart(document, bookmarkName); |
| 100 | + openingBracketParagraph .ChildEntities.Insert(openingBracketCharIndex, bookmarkStart); |
| 101 | + // Create and insert a bookmark end after the specific word |
| 102 | + BookmarkEnd bookmarkEnd = new BookmarkEnd(document, bookmarkName); |
| 103 | + paragraph.ChildEntities.Insert(phraseEntityIndex + 1, bookmarkEnd); |
| 104 | + // Use Bookmarknavigator to delete content between the bookmarks |
| 105 | + BookmarksNavigator navigator = new BookmarksNavigator(document); |
| 106 | + navigator.MoveToBookmark(bookmarkName); |
| 107 | + navigator.DeleteBookmarkContent(true); |
| 108 | + // Remove the temporary bookmark from the document |
| 109 | + document.Bookmarks.Remove(navigator.CurrentBookmark); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments