Skip to content

Commit f803ddd

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

File tree

4 files changed

+21
-46
lines changed

4 files changed

+21
-46
lines changed

Word-document/Extract-images-from-Word-document/.NET/Extract-images-from-Word-document/Program.cs

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,41 @@
11
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
36

47
namespace Extract_images_from_Word_document
58
{
69
internal class Program
710
{
811
static void Main(string[] args)
912
{
10-
//Open the file as Stream.
13+
// Open the file as a stream.
1114
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
1215
{
13-
//Load the file stream into a Word document.
16+
// Load the file stream into a Word document.
1417
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
1518
{
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;
19+
// Find all pictures by EntityType in the Word document.
20+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
2421

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");
22+
// Iterate through the pictures and save each one as an image file.
23+
for (int i = 0; i < pictures.Count; i++)
24+
{
25+
WPicture image = pictures[i] as WPicture;
3026

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-
}
27+
// Use a MemoryStream to handle the image bytes from the picture.
28+
using (MemoryStream memoryStream = new MemoryStream(image.ImageBytes))
29+
{
30+
// Define the path where the image will be saved.
31+
string imagePath = Path.GetFullPath(@"../../../Image-" + i + ".jpeg");
3832

39-
// Increment the index for the next image
40-
index++;
41-
break;
33+
// Create a FileStream to write the image to the specified path.
34+
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
35+
{
36+
memoryStream.CopyTo(filestream);
37+
}
4238
}
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);
6439
}
6540
}
6641
}

0 commit comments

Comments
 (0)