Skip to content

Commit

Permalink
Merge pull request SyncfusionExamples#172 from snehasf3509/main
Browse files Browse the repository at this point in the history
Sample to replace OLE object with text in a Word document
  • Loading branch information
MohanaselvamJothi authored Apr 8, 2024
2 parents 753e781 + 57695c1 commit bfea0f8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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<Entity> 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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Replace_all_OLE_with_text_DocIO</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

</Project>

0 comments on commit bfea0f8

Please sign in to comment.