forked from SyncfusionExamples/DocIO-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request SyncfusionExamples#172 from snehasf3509/main
Sample to replace OLE object with text in a Word document
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...-and-Replace/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+39.7 KB
...place-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Data/Template.docx
Binary file not shown.
56 changes: 56 additions & 0 deletions
56
...place/Replace-all-OLE-objects-with-text/.NET/Replace-all-OLE-objects-with-text/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
15 changes: 15 additions & 0 deletions
15
...with-text/.NET/Replace-all-OLE-objects-with-text/Replace-all-OLE-objects-with-text.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |