Skip to content

ES-957518- Add the sample Remove-embedded-MP4-files-from-Word #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;


// Load the Word document from the specified path
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Find all OLE objects in the document where the ObjectType is "Package" (i.e., embedded file)
List<Entity> oleObjects = document.FindAllItemsByProperty(EntityType.OleObject, "ObjectType", "Package");

if (oleObjects != null)
{
// Iterate through each OLE object found in the document
foreach (Entity entity in oleObjects)
{
// Cast the entity to a WOleObject
WOleObject oleObject = entity as WOleObject;

// Get the native (embedded) data from the OLE object
byte[] nativeData = oleObject.NativeData;

// Check if the embedded file is a .mp4 video
if (oleObject.PackageFileName.EndsWith(".mp4"))
{
// Get the paragraph that owns the OLE object
WParagraph ownerPara = oleObject.OwnerParagraph;

// Loop to remove all related field code entities until the FieldEnd is reached
while (true)
{
// Get the next sibling entity in the paragraph
Entity nextEntity = entity.NextSibling as Entity;

// Remove the sibling entity from the paragraph
ownerPara.ChildEntities.Remove(nextEntity);

// Stop when the field end marker is reached
if ((nextEntity is WFieldMark) && (nextEntity as WFieldMark).Type == FieldMarkType.FieldEnd)
break;
}

// Finally, remove the OLE object itself from the paragraph
ownerPara.ChildEntities.Remove(entity);
}
}
}

// Save the modified Word document to the output path
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Remove_embedded_MP4_files_from_Word</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading