Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,29 @@ static void Main()
// Find all merge fields with the same name
List<Entity> mergeGroups = document.FindAllItemsByProperty(EntityType.MergeField, "FieldName", groupName);
// Start from second occurrence to remove duplicates
for (int i = 1; i < mergeGroups.Count; i++)
for (int i = 2; i < mergeGroups.Count; i++)
{
WMergeField mergeField = mergeGroups[i] as WMergeField;
// Check if it's a group start field
if (mergeField.FieldCode.Contains("TableStart") || mergeField.FieldCode.Contains("BeginGroup"))
if (mergeField.FieldCode.Contains("TableStart:") || mergeField.FieldCode.Contains("BeginGroup:"))
{
// Generate unique Bookmark name
string bkmkGroupName = groupName + Guid.NewGuid().ToString();
// Add bookmark start before the group
BookmarkStart bkmkStart = new BookmarkStart(document, groupName);
BookmarkStart bkmkStart = new BookmarkStart(document, bkmkGroupName);
WParagraph startPara = mergeField.OwnerParagraph;
int mergeFieldIndex = startPara.ChildEntities.IndexOf(mergeField);
startPara.ChildEntities.Insert(mergeFieldIndex, bkmkStart);
// Add bookmark end after the group
WMergeField endField = mergeGroups[i + 1] as WMergeField;
BookmarkEnd bkmkEnd = new BookmarkEnd(document, groupName);
BookmarkEnd bkmkEnd = new BookmarkEnd(document, bkmkGroupName);
WParagraph endPara = endField.OwnerParagraph;
int endFieldIndex = endPara.ChildEntities.IndexOf(endField);
endPara.ChildEntities.Insert(endFieldIndex + 1, bkmkEnd);
i++;
// Delete content inside the bookmark
BookmarksNavigator navigator = new BookmarksNavigator(document);
navigator.MoveToBookmark(groupName);
navigator.MoveToBookmark(bkmkGroupName);
navigator.DeleteBookmarkContent(false);
document.Bookmarks.Remove(navigator.CurrentBookmark);
// Remove owner table if applicable
Expand Down
2 changes: 1 addition & 1 deletion Videos/IF-field/Create IF Field.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="30.2.7" />
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Office;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -13,35 +14,66 @@ internal class Program
{
static void Main(string[] args)
{
// Open the file as a stream.
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
// Open the Word document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../Data/Template.docx")))
{
// Load the file stream into a Word document.
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
// Find all pictures by EntityType in the Word document.
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
//To save unique images with identifier
int count = 0;
// Iterate through the pictures and save each one as an image file.
for (int i = 0; i < pictures.Count; i++)
{
// Find all pictures by EntityType in the Word document.
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);

// Iterate through the pictures and save each one as an image file.
for (int i = 0; i < pictures.Count; i++)
WPicture image = pictures[i] as WPicture;
ExtractImages(image.ImageBytes, count);
count++;
}
// Find all smart arts by EntityType in the Word document.
List<Entity> smartArts = document.FindAllItemsByProperty(EntityType.SmartArt, null, null);
// Iterate through the smart art.
for (int i = 0; i < smartArts.Count; i++)
{
WSmartArt smartArt = smartArts[i] as WSmartArt;
//Extract background image in the smart art
if (smartArt.Background.PictureFill.ImageBytes != null)
{
WPicture image = pictures[i] as WPicture;

// Use a MemoryStream to handle the image bytes from the picture.
using (MemoryStream memoryStream = new MemoryStream(image.ImageBytes))
ExtractImages(smartArt.Background.PictureFill.ImageBytes, count);
count++;
}
//Traverse through all nodes inside the SmartArt.
foreach (IOfficeSmartArtNode node in smartArt.Nodes)
{
foreach (IOfficeSmartArtShape shape in node.Shapes)
{
// Define the path where the image will be saved.
string imagePath = Path.GetFullPath(@"../../Image-" + i + ".jpeg");

// Create a FileStream to write the image to the specified path.
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
//If shape fill type is picture, then extract the image.
if (shape.Fill.FillType == OfficeShapeFillType.Picture)
{
memoryStream.CopyTo(filestream);
ExtractImages(shape.Fill.PictureFill.ImageBytes, count);
count++;
}
}
}
}
}
}
/// <summary>
/// Extracts image data from a byte array and saves it as a JPEG file with a unique identifier.
/// </summary>
/// <param name="imageBytes">The byte array containing image data to be saved</param>
/// <param name="count">A unique identifier used to name the output image file.</param>
private static void ExtractImages(byte[] imageBytes, int count)
{
// Use a MemoryStream to handle the image bytes from the picture.
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{
// Define the path where the image will be saved.
string imagePath = Path.GetFullPath(@"../../Output/Image-" + count + ".jpeg");
// Create a FileStream to write the image to the specified path.
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
{
memoryStream.CopyTo(filestream);
}
}
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Office;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -10,35 +11,66 @@ internal class Program
{
static void Main(string[] args)
{
// Open the file as a stream.
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
// Open the Word document.
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Load the file stream into a Word document.
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
// Find all pictures by EntityType in the Word document.
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
//To save unique images with identifier
int count = 0;
// Iterate through the pictures and save each one as an image file.
for (int i = 0; i < pictures.Count; i++)
{
// Find all pictures by EntityType in the Word document.
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);

// Iterate through the pictures and save each one as an image file.
for (int i = 0; i < pictures.Count; i++)
WPicture image = pictures[i] as WPicture;
ExtractImages(image.ImageBytes, count);
count++;
}
// Find all smart arts by EntityType in the Word document.
List<Entity> smartArts = document.FindAllItemsByProperty(EntityType.SmartArt, null, null);
// Iterate through the smart art.
for (int i = 0; i < smartArts.Count; i++)
{
WSmartArt smartArt = smartArts[i] as WSmartArt;
//Extract background image in the smart art
if (smartArt.Background.PictureFill.ImageBytes != null)
{
WPicture image = pictures[i] as WPicture;

// Use a MemoryStream to handle the image bytes from the picture.
using (MemoryStream memoryStream = new MemoryStream(image.ImageBytes))
ExtractImages(smartArt.Background.PictureFill.ImageBytes, count);
count++;
}
//Traverse through all nodes inside the SmartArt.
foreach (IOfficeSmartArtNode node in smartArt.Nodes)
{
foreach (IOfficeSmartArtShape shape in node.Shapes)
{
// Define the path where the image will be saved.
string imagePath = Path.GetFullPath(@"Output/Image-" + i + ".jpeg");

// Create a FileStream to write the image to the specified path.
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
//If shape fill type is picture, then extract the image.
if (shape.Fill.FillType == OfficeShapeFillType.Picture)
{
memoryStream.CopyTo(filestream);
ExtractImages(shape.Fill.PictureFill.ImageBytes, count);
count++;
}
}
}
}
}
}
/// <summary>
/// Extracts image data from a byte array and saves it as a JPEG file with a unique identifier.
/// </summary>
/// <param name="imageBytes">The byte array containing image data to be saved</param>
/// <param name="count">A unique identifier used to name the output image file.</param>
private static void ExtractImages(byte[] imageBytes, int count)
{
// Use a MemoryStream to handle the image bytes from the picture.
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{
// Define the path where the image will be saved.
string imagePath = Path.GetFullPath(@"../../../Output/Image-" + count + ".jpeg");
// Create a FileStream to write the image to the specified path.
using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
{
memoryStream.CopyTo(filestream);
}
}
}
}
}
Loading