|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | + |
| 3 | +// Load the Word document using Syncfusion.DocIO |
| 4 | +using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"))) |
| 5 | +{ |
| 6 | + // Define an array of texts to find within the document |
| 7 | + string[] textsToFind = new string[2] { "GIANT START", "GIANT END" }; |
| 8 | + |
| 9 | + // Iterate through each text in the array and call FindStartEndAndIterate to process them |
| 10 | + foreach (string textToFind in textsToFind) |
| 11 | + { |
| 12 | + FindStartEndAndIterate(document, textToFind); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Finds the start and end paragraphs based on the search text, then iterates through the paragraphs in the specified range. |
| 18 | +/// </summary> |
| 19 | +/// <param name="document">The Word document to search through.</param> |
| 20 | +/// <param name="textToSearch">The text to search for that marks the start of the range.</param> |
| 21 | +void FindStartEndAndIterate(WordDocument document, string textToSearch) |
| 22 | +{ |
| 23 | + WParagraph startPara = null; |
| 24 | + WParagraph endPara = null; |
| 25 | + WSection startSection = null; |
| 26 | + WSection endSection = null; |
| 27 | + |
| 28 | + string endText = "GIANT"; // Text to mark the end of the range |
| 29 | + |
| 30 | + // Step 1: Find the start and end paragraphs by iterating through each section of the document |
| 31 | + foreach (WSection section in document.Sections) |
| 32 | + { |
| 33 | + foreach (Entity entity in section.Body.ChildEntities) |
| 34 | + { |
| 35 | + // Check if the entity is a paragraph |
| 36 | + if (entity is WParagraph paragraph) |
| 37 | + { |
| 38 | + string paraText = paragraph.Text; |
| 39 | + |
| 40 | + // If the paragraph starts with the 'textToSearch', set it as the start paragraph |
| 41 | + if (paraText.StartsWith(textToSearch)) |
| 42 | + { |
| 43 | + startPara = paragraph; |
| 44 | + startSection = section; |
| 45 | + } |
| 46 | + // If the paragraph contains the 'endText', set it as the end paragraph |
| 47 | + else if (paraText.Contains(endText)) |
| 48 | + { |
| 49 | + endPara = paragraph; |
| 50 | + endSection = section; |
| 51 | + break; // Stop once the end paragraph is found |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // If both start and end paragraphs have been found, break out of the loop |
| 57 | + if (startPara != null && endPara != null) |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + // If no start or end paragraphs were found, exit the method |
| 62 | + if (startPara == null || endPara == null) |
| 63 | + return; |
| 64 | + |
| 65 | + // Get the index of the start paragraph within its section's body and the section itself |
| 66 | + int startBodyIndex = startSection.Body.ChildEntities.IndexOf(startPara); |
| 67 | + int startSectionIndex = document.Sections.IndexOf(startSection); |
| 68 | + |
| 69 | + // Get the index of the end paragraph within its section's body and the section itself |
| 70 | + int endBodyIndex = endSection.Body.ChildEntities.IndexOf(endPara); |
| 71 | + int endSectionIndex = document.Sections.IndexOf(endSection); |
| 72 | + |
| 73 | + // Step 2: Loop through the sections from the start section to the end section |
| 74 | + for (int sectionIndex = startSectionIndex; sectionIndex <= endSectionIndex; sectionIndex++) |
| 75 | + { |
| 76 | + // Get the current section from the document |
| 77 | + WSection currentSection = document.Sections[sectionIndex]; |
| 78 | + |
| 79 | + // Determine the starting index of body entities in this section |
| 80 | + // If it's the first section, start from startBodyIndex; otherwise, start from the beginning |
| 81 | + int start = (sectionIndex == startSectionIndex) ? startBodyIndex : 0; |
| 82 | + |
| 83 | + // Determine the ending index of body entities in this section |
| 84 | + // If it's the last section, end at endBodyIndex; otherwise, end at the last entity |
| 85 | + int end = (sectionIndex == endSectionIndex) ? endBodyIndex : currentSection.Body.ChildEntities.Count - 1; |
| 86 | + |
| 87 | + // Step 3: Loop through the paragraphs from start to end within the current section |
| 88 | + for (int paraIndex = start; paraIndex <= end; paraIndex++) |
| 89 | + { |
| 90 | + // Get the current entity in the section's body |
| 91 | + Entity currEntity = currentSection.Body.ChildEntities[paraIndex]; |
| 92 | + |
| 93 | + // Check if the current entity is a paragraph |
| 94 | + if (currEntity is WParagraph currentPara) |
| 95 | + { |
| 96 | + // Print the paragraph text to the console |
| 97 | + Console.WriteLine(currentPara.Text); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments