1- using Syncfusion . DocIO . DLS ;
2- using Syncfusion . DocIO ;
1+ using Syncfusion . DocIO ;
2+ using Syncfusion . DocIO . DLS ;
3+ using Syncfusion . Office ;
34using System ;
45using System . Collections . Generic ;
56using 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}
0 commit comments