|
1 | 1 | using Syncfusion.DocIO; |
2 | 2 | using Syncfusion.DocIO.DLS; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
3 | 6 |
|
4 | 7 | namespace Extract_images_from_Word_document |
5 | 8 | { |
6 | 9 | internal class Program |
7 | 10 | { |
8 | 11 | static void Main(string[] args) |
9 | 12 | { |
10 | | - //Open the file as Stream. |
| 13 | + // Open the file as a stream. |
11 | 14 | using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read)) |
12 | 15 | { |
13 | | - //Load the file stream into a Word document. |
| 16 | + // Load the file stream into a Word document. |
14 | 17 | using (WordDocument document = new WordDocument(docStream, FormatType.Docx)) |
15 | 18 | { |
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); |
24 | 21 |
|
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; |
30 | 26 |
|
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"); |
38 | 32 |
|
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 | + } |
42 | 38 | } |
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 | 39 | } |
65 | 40 | } |
66 | 41 | } |
|
0 commit comments