Skip to content

Commit 4c39356

Browse files
Extract-images-from-Word-document
1 parent 7adba4c commit 4c39356

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-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.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extract-images-from-Word-document", "Extract-images-from-Word-document\Extract-images-from-Word-document.csproj", "{99837ED5-36ED-4095-8A53-9D922F06FF32}"
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+
{99837ED5-36ED-4095-8A53-9D922F06FF32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{99837ED5-36ED-4095-8A53-9D922F06FF32}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{99837ED5-36ED-4095-8A53-9D922F06FF32}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{99837ED5-36ED-4095-8A53-9D922F06FF32}.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 = {04411ABE-09D0-42C8-B555-9D64AA5C1646}
24+
EndGlobalSection
25+
EndGlobal
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>net8.0</TargetFramework>
6+
<RootNamespace>Extract_images_from_Word_document</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>
19.6 KB
Loading
18.5 KB
Loading
20.2 KB
Loading
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Extract_images_from_Word_document
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
//Open the file as Stream.
11+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
12+
{
13+
//Load the file stream into a Word document.
14+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
15+
{
16+
int index = 0;
17+
//Visits all document entities.
18+
foreach (var item in document.Visit())
19+
{
20+
switch (item.EntityType)
21+
{
22+
case EntityType.Picture:
23+
WPicture picture = item as WPicture;
24+
25+
// Use a MemoryStream to handle the image bytes from the picture
26+
using (MemoryStream memoryStream = new MemoryStream(picture.ImageBytes))
27+
{
28+
// Define the path where the image will be saved
29+
string imagePath = Path.GetFullPath(@"../../../Images/Image-" + index + ".jpeg");
30+
31+
// Create a FileStream to write the image to the specified path
32+
using (FileStream image = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
33+
{
34+
// Copy the content of the MemoryStream to the FileStream
35+
memoryStream.CopyTo(image);
36+
}
37+
}
38+
39+
// Increment the index for the next image
40+
index++;
41+
break;
42+
}
43+
44+
}
45+
}
46+
}
47+
}
48+
}
49+
50+
public static class DocIOExtensions
51+
{
52+
public static IEnumerable<IEntity> Visit(this ICompositeEntity entity)
53+
{
54+
var entities = new Stack<IEntity>(new IEntity[] { entity });
55+
while (entities.Count > 0)
56+
{
57+
var e = entities.Pop();
58+
yield return e;
59+
if (e is ICompositeEntity)
60+
{
61+
foreach (IEntity childEntity in ((ICompositeEntity)e).ChildEntities)
62+
{
63+
entities.Push(childEntity);
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)