Skip to content

Commit 09ab8c0

Browse files
author
sneha.biju
committed
Replace-OLE-object-with-text sample
1 parent 6353866 commit 09ab8c0

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34330.188
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-OLE-object-with-text", "Replace-OLE-object-with-text\Replace-OLE-object-with-text.csproj", "{296FE9C9-0D32-4FBC-A31C-33EECF197775}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{296FE9C9-0D32-4FBC-A31C-33EECF197775}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{296FE9C9-0D32-4FBC-A31C-33EECF197775}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{296FE9C9-0D32-4FBC-A31C-33EECF197775}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{296FE9C9-0D32-4FBC-A31C-33EECF197775}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {164E035F-E2B0-455C-90C6-4642CC924107}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
//Open the file as a Stream.
5+
using (FileStream docStream = new FileStream("../../../Data/Template.docx", FileMode.Open, FileAccess.Read))
6+
{
7+
//Load the file stream into a Word document.
8+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
9+
{
10+
//Find all OLE object by EntityType in Word document.
11+
List<Entity> oleObjects = document.FindAllItemsByProperty(EntityType.OleObject, null, null);
12+
//Remove the OLE objects and endnotes.
13+
for (int i = 0; i < oleObjects.Count; i++)
14+
{
15+
WOleObject ole = oleObjects[i] as WOleObject;
16+
//Replaces the OLE object with a alternate text.
17+
ReplaceOLEObjectsWithPlaceHolder(ole, "Embedded file was here");
18+
}
19+
//Save a Word document to the MemoryStream.
20+
FileStream outputStream = new FileStream(@"../../../Data/Output.docx", FileMode.OpenOrCreate);
21+
document.Save(outputStream, FormatType.Docx);
22+
//Closes the Word document
23+
document.Close();
24+
outputStream.Close();
25+
}
26+
}
27+
28+
void ReplaceOLEObjectsWithPlaceHolder(WOleObject ole, string replacingText)
29+
{
30+
WParagraph ownerPara = ole.OwnerParagraph;
31+
int index = ownerPara.ChildEntities.IndexOf(ole);
32+
//Removes the ole object.
33+
RemoveOLEObject(ownerPara, index);
34+
//Insert the alternate text.
35+
InsertTextrange(ownerPara, index, replacingText);
36+
}
37+
38+
void RemoveOLEObject(WParagraph ownerPara, int index)
39+
{
40+
//Iterate from FieldEnd to OLE object
41+
for (int i = index + 4; i >= index; i--)
42+
{
43+
//Remove the OLE object based on the structure
44+
ownerPara.ChildEntities.RemoveAt(i);
45+
}
46+
}
47+
48+
void InsertTextrange(WParagraph ownerPara, int index, string replacingText)
49+
{
50+
//Create a new textrange
51+
WTextRange textRange = new WTextRange(ownerPara.Document);
52+
//Add the text
53+
textRange.Text = replacingText;
54+
//Insert the textrange in the particular index
55+
ownerPara.ChildEntities.Insert(index, textRange);
56+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Replace_OLE_with_text_DocIO</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)