Skip to content

Commit 1c06952

Browse files
ES-957518- Add the sample Remove-embedded-MP4-files-from-Word
1 parent 4a924b7 commit 1c06952

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-embedded-MP4-files-from-Word", "Remove-embedded-MP4-files-from-Word\Remove-embedded-MP4-files-from-Word.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
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+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
5+
// Load the Word document from the specified path
6+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
7+
{
8+
// Find all OLE objects in the document where the ObjectType is "Package" (i.e., embedded file)
9+
List<Entity> oleObjects = document.FindAllItemsByProperty(EntityType.OleObject, "ObjectType", "Package");
10+
11+
if (oleObjects != null)
12+
{
13+
// Iterate through each OLE object found in the document
14+
foreach (Entity entity in oleObjects)
15+
{
16+
// Cast the entity to a WOleObject
17+
WOleObject oleObject = entity as WOleObject;
18+
19+
// Get the native (embedded) data from the OLE object
20+
byte[] nativeData = oleObject.NativeData;
21+
22+
// Check if the embedded file is a .mp4 video
23+
if (oleObject.PackageFileName.EndsWith(".mp4"))
24+
{
25+
// Get the paragraph that owns the OLE object
26+
WParagraph ownerPara = oleObject.OwnerParagraph;
27+
28+
// Loop to remove all related field code entities until the FieldEnd is reached
29+
while (true)
30+
{
31+
// Get the next sibling entity in the paragraph
32+
Entity nextEntity = entity.NextSibling as Entity;
33+
34+
// Remove the sibling entity from the paragraph
35+
ownerPara.ChildEntities.Remove(nextEntity);
36+
37+
// Stop when the field end marker is reached
38+
if ((nextEntity is WFieldMark) && (nextEntity as WFieldMark).Type == FieldMarkType.FieldEnd)
39+
break;
40+
}
41+
42+
// Finally, remove the OLE object itself from the paragraph
43+
ownerPara.ChildEntities.Remove(entity);
44+
}
45+
}
46+
}
47+
48+
// Save the modified Word document to the output path
49+
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Remove_embedded_MP4_files_from_Word</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+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)