diff --git a/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text.sln b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text.sln new file mode 100644 index 00000000..31f99b2f --- /dev/null +++ b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-all-OLE-objects-with-text", "Replace-all-OLE-objects-with-text\Replace-all-OLE-objects-with-text.csproj", "{296FE9C9-0D32-4FBC-A31C-33EECF197775}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {296FE9C9-0D32-4FBC-A31C-33EECF197775}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {296FE9C9-0D32-4FBC-A31C-33EECF197775}.Debug|Any CPU.Build.0 = Debug|Any CPU + {296FE9C9-0D32-4FBC-A31C-33EECF197775}.Release|Any CPU.ActiveCfg = Release|Any CPU + {296FE9C9-0D32-4FBC-A31C-33EECF197775}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {164E035F-E2B0-455C-90C6-4642CC924107} + EndGlobalSection +EndGlobal diff --git a/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Data/Template.docx b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Data/Template.docx new file mode 100644 index 00000000..bd2ae98d Binary files /dev/null and b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Data/Template.docx differ diff --git a/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Program.cs b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Program.cs new file mode 100644 index 00000000..ded253f0 --- /dev/null +++ b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Program.cs @@ -0,0 +1,56 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; + +//Open the file as a Stream. +using (FileStream docStream = new FileStream("../../../Data/Template.docx", FileMode.Open, FileAccess.Read)) +{ + //Load the file stream into a Word document. + using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) + { + //Find all OLE object by EntityType in Word document. + List oleObjects = document.FindAllItemsByProperty(EntityType.OleObject, null, null); + //Remove the OLE objects and endnotes. + for (int i = 0; i < oleObjects.Count; i++) + { + WOleObject ole = oleObjects[i] as WOleObject; + //Replaces the OLE object with a alternate text. + ReplaceOLEObjectsWithPlaceHolder(ole, "Embedded file was here"); + } + //Save a Word document to the MemoryStream. + FileStream outputStream = new FileStream(@"../../../Data/Output.docx", FileMode.OpenOrCreate); + document.Save(outputStream, FormatType.Docx); + //Closes the Word document + document.Close(); + outputStream.Close(); + } +} + +void ReplaceOLEObjectsWithPlaceHolder(WOleObject ole, string replacingText) +{ + WParagraph ownerPara = ole.OwnerParagraph; + int index = ownerPara.ChildEntities.IndexOf(ole); + //Removes the ole object. + RemoveOLEObject(ownerPara, index); + //Insert the alternate text. + InsertTextrange(ownerPara, index, replacingText); +} + +void RemoveOLEObject(WParagraph ownerPara, int index) +{ + //Iterate from FieldEnd to OLE object + for (int i = index + 4; i >= index; i--) + { + //Remove the OLE object based on the structure + ownerPara.ChildEntities.RemoveAt(i); + } +} + +void InsertTextrange(WParagraph ownerPara, int index, string replacingText) +{ + //Create a new textrange + WTextRange textRange = new WTextRange(ownerPara.Document); + //Add the text + textRange.Text = replacingText; + //Insert the textrange in the particular index + ownerPara.ChildEntities.Insert(index, textRange); +} \ No newline at end of file diff --git a/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Replace-all-OLE-objects-with-text.csproj b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Replace-all-OLE-objects-with-text.csproj new file mode 100644 index 00000000..723afe6a --- /dev/null +++ b/Find-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Replace-all-OLE-objects-with-text.csproj @@ -0,0 +1,15 @@ + + + + Exe + net6.0 + Replace_all_OLE_with_text_DocIO + enable + enable + + + + + + +