Skip to content

Commit d93807a

Browse files
committed
965467 - Modified samples
1 parent c90373b commit d93807a

File tree

5 files changed

+104
-39
lines changed

5 files changed

+104
-39
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using Syncfusion.DocIO.DLS;
2-
using Syncfusion.DocIO;
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Office;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
@@ -14,34 +15,65 @@ internal class Program
1415
static void Main(string[] args)
1516
{
1617
// Open the file as a stream.
17-
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
18+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Data/Template.docx")))
1819
{
19-
// Load the file stream into a Word document.
20-
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
20+
// Find all pictures by EntityType in the Word document.
21+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
22+
//To save unique images with identifier
23+
int count = 0;
24+
// Iterate through the pictures and save each one as an image file.
25+
for (int i = 0; i < pictures.Count; i++)
2126
{
22-
// Find all pictures by EntityType in the Word document.
23-
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
24-
25-
// Iterate through the pictures and save each one as an image file.
26-
for (int i = 0; i < pictures.Count; i++)
27+
WPicture image = pictures[i] as WPicture;
28+
ExtractImages(image.ImageBytes, count);
29+
count++;
30+
}
31+
// Find all smart arts by EntityType in the Word document.
32+
List<Entity> smartArts = document.FindAllItemsByProperty(EntityType.SmartArt, null, null);
33+
// Iterate through the smart art.
34+
for (int i = 0; i < smartArts.Count; i++)
35+
{
36+
WSmartArt smartArt = smartArts[i] as WSmartArt;
37+
//Extract background image in the smart art
38+
if (smartArt.Background.PictureFill.ImageBytes != null)
2739
{
28-
WPicture image = pictures[i] as WPicture;
29-
30-
// Use a MemoryStream to handle the image bytes from the picture.
31-
using (MemoryStream memoryStream = new MemoryStream(image.ImageBytes))
40+
ExtractImages(smartArt.Background.PictureFill.ImageBytes, count);
41+
count++;
42+
}
43+
//Traverse through all nodes inside the SmartArt.
44+
foreach (IOfficeSmartArtNode node in smartArt.Nodes)
45+
{
46+
foreach (IOfficeSmartArtShape shape in node.Shapes)
3247
{
33-
// Define the path where the image will be saved.
34-
string imagePath = Path.GetFullPath(@"../../Image-" + i + ".jpeg");
35-
36-
// Create a FileStream to write the image to the specified path.
37-
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
48+
//If shape fill type is picture, then extract the image.
49+
if (shape.Fill.FillType == OfficeShapeFillType.Picture)
3850
{
39-
memoryStream.CopyTo(filestream);
51+
ExtractImages(shape.Fill.PictureFill.ImageBytes, count);
52+
count++;
4053
}
4154
}
4255
}
4356
}
4457
}
4558
}
59+
/// <summary>
60+
/// Extracts image data from a byte array and saves it as a JPEG file with a unique identifier.
61+
/// </summary>
62+
/// <param name="imageBytes">The byte array containing image data to be saved</param>
63+
/// <param name="count">A unique identifier used to name the output image file.</param>
64+
private static void ExtractImages(byte[] imageBytes, int count)
65+
{
66+
// Use a MemoryStream to handle the image bytes from the picture.
67+
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
68+
{
69+
// Define the path where the image will be saved.
70+
string imagePath = Path.GetFullPath(@"../../Output/Image-" + count + ".jpeg");
71+
// Create a FileStream to write the image to the specified path.
72+
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
73+
{
74+
memoryStream.CopyTo(filestream);
75+
}
76+
}
77+
}
4678
}
4779
}
Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Syncfusion.DocIO;
22
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Office;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
@@ -10,35 +11,66 @@ internal class Program
1011
{
1112
static void Main(string[] args)
1213
{
13-
// Open the file as a stream.
14-
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
14+
// Open the Word document.
15+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
1516
{
16-
// Load the file stream into a Word document.
17-
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
17+
// Find all pictures by EntityType in the Word document.
18+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
19+
//To save unique images with identifier
20+
int count = 0;
21+
// Iterate through the pictures and save each one as an image file.
22+
for (int i = 0; i < pictures.Count; i++)
1823
{
19-
// Find all pictures by EntityType in the Word document.
20-
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
21-
22-
// Iterate through the pictures and save each one as an image file.
23-
for (int i = 0; i < pictures.Count; i++)
24+
WPicture image = pictures[i] as WPicture;
25+
ExtractImages(image.ImageBytes, count);
26+
count++;
27+
}
28+
// Find all smart arts by EntityType in the Word document.
29+
List<Entity> smartArts = document.FindAllItemsByProperty(EntityType.SmartArt, null, null);
30+
// Iterate through the smart art.
31+
for (int i = 0; i < smartArts.Count; i++)
32+
{
33+
WSmartArt smartArt = smartArts[i] as WSmartArt;
34+
//Extract background image in the smart art
35+
if (smartArt.Background.PictureFill.ImageBytes != null)
2436
{
25-
WPicture image = pictures[i] as WPicture;
26-
27-
// Use a MemoryStream to handle the image bytes from the picture.
28-
using (MemoryStream memoryStream = new MemoryStream(image.ImageBytes))
37+
ExtractImages(smartArt.Background.PictureFill.ImageBytes, count);
38+
count++;
39+
}
40+
//Traverse through all nodes inside the SmartArt.
41+
foreach (IOfficeSmartArtNode node in smartArt.Nodes)
42+
{
43+
foreach (IOfficeSmartArtShape shape in node.Shapes)
2944
{
30-
// Define the path where the image will be saved.
31-
string imagePath = Path.GetFullPath(@"Output/Image-" + i + ".jpeg");
32-
33-
// Create a FileStream to write the image to the specified path.
34-
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
45+
//If shape fill type is picture, then extract the image.
46+
if (shape.Fill.FillType == OfficeShapeFillType.Picture)
3547
{
36-
memoryStream.CopyTo(filestream);
48+
ExtractImages(shape.Fill.PictureFill.ImageBytes, count);
49+
count++;
3750
}
3851
}
3952
}
4053
}
4154
}
4255
}
56+
/// <summary>
57+
/// Extracts image data from a byte array and saves it as a JPEG file with a unique identifier.
58+
/// </summary>
59+
/// <param name="imageBytes">The byte array containing image data to be saved</param>
60+
/// <param name="count">A unique identifier used to name the output image file.</param>
61+
private static void ExtractImages(byte[] imageBytes, int count)
62+
{
63+
// Use a MemoryStream to handle the image bytes from the picture.
64+
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
65+
{
66+
// Define the path where the image will be saved.
67+
string imagePath = Path.GetFullPath(@"../../../Output/Image-" + count + ".jpeg");
68+
// Create a FileStream to write the image to the specified path.
69+
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
70+
{
71+
memoryStream.CopyTo(filestream);
72+
}
73+
}
74+
}
4375
}
4476
}

0 commit comments

Comments
 (0)