diff --git a/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder.slnx b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder.slnx new file mode 100644 index 000000000..5045dd9b2 --- /dev/null +++ b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Data/Template.docx b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Data/Template.docx new file mode 100644 index 000000000..a7b48ea76 Binary files /dev/null and b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Data/Template.docx differ diff --git a/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Output/.gitkeep b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Output/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Program.cs b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Program.cs new file mode 100644 index 000000000..b3ef095a4 --- /dev/null +++ b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Program.cs @@ -0,0 +1,64 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; +using System; +using System.IO; +using System.Collections.Generic; +namespace Remove_previous_Paragraph_of_PlaceHolder +{ + internal class Program + { + static void Main(string[] args) + { + string documentPath = Path.GetFullPath(@"Data/Template.docx"); + // Define an array of phrases to remove + string[] phrases = { + "{Themes and styles also help keep your document coordinated}", + "{Reading is easier, too, in the new Reading view}" + }; + + RemovePhrasesFromWord(documentPath, phrases); + } + public static void RemovePhrasesFromWord(string documentPath, string[] searchPhrases) + { + // Load the Word document + using (WordDocument document = new WordDocument(documentPath, FormatType.Docx)) + { + foreach (string searchPhrase in searchPhrases) // Iterate through all phrases + { + Console.WriteLine($"Processing phrase: {searchPhrase}"); + TextSelection[] selections = document.FindAll(searchPhrase, false, false); // Find all occurrences + + if (selections.Length > 0) + { + List paragraphsToRemove = new List(); + + foreach (var selection in selections) + { + WParagraph paragraphContainingPhrase = selection.GetAsOneRange().OwnerParagraph; + //Previous sibling gets previous entity (above paragraphContainingPhrase) + if (paragraphContainingPhrase.PreviousSibling is WParagraph) + { + // Mark the paragraph above for removal + paragraphsToRemove.Add(paragraphContainingPhrase.PreviousSibling as WParagraph); + } + // Mark the paragraph containing the phrase for removal + paragraphsToRemove.Add(paragraphContainingPhrase); + } + + // Reverse loop to safely remove paragraphs without index shifting issues + for (int i = paragraphsToRemove.Count - 1; i >= 0; i--) + { + WParagraph para = paragraphsToRemove[i]; + if (para != null && para.OwnerTextBody != null) + { + para.OwnerTextBody.ChildEntities.Remove(para); + } + } + } + } + // Save the modified document + document.Save(Path.GetFullPath(@"../../../Output/output.docx"), FormatType.Docx); + } + } + } +} diff --git a/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Remove_previous_Paragraph_of_PlaceHolder.csproj b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Remove_previous_Paragraph_of_PlaceHolder.csproj new file mode 100644 index 000000000..a5f3dbe25 --- /dev/null +++ b/Find-and-Replace/Remove_previous_Paragraph_of_PlaceHolder/NET/Remove_previous_Paragraph_of_PlaceHolder/Remove_previous_Paragraph_of_PlaceHolder.csproj @@ -0,0 +1,21 @@ + + + + Exe + net10.0 + Remove_previous_Paragraph_of_PlaceHolder + + + + + + + + + Always + + + Always + + +