Skip to content

Commit 732bbe3

Browse files
Merge pull request #208 from DharanitharanA/main
Added sample for converting Word and combine to single image
2 parents 8ddcbff + 8dc6f0e commit 732bbe3

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34205.153
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Word-and-combine-to-single-image", "Convert-Word-and-combine-to-single-image\Convert-Word-and-combine-to-single-image.csproj", "{47CD95E3-76EA-4F5C-AFC7-E5EF73097DE9}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{47CD95E3-76EA-4F5C-AFC7-E5EF73097DE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{47CD95E3-76EA-4F5C-AFC7-E5EF73097DE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{47CD95E3-76EA-4F5C-AFC7-E5EF73097DE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{47CD95E3-76EA-4F5C-AFC7-E5EF73097DE9}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3A56C486-C2E5-4062-8439-5FAA0BDBCACC}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Convert_Word_and_combine_to_single_image</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using SkiaSharp;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.DocIORenderer;
5+
6+
namespace Convert_Word_and_combine_to_single_image
7+
{
8+
internal class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Open the file as Stream
13+
using (FileStream docStream = new FileStream(@"../../../Data/Template.docx", FileMode.Open, FileAccess.Read))
14+
{
15+
//Loads an existing Word document
16+
using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
17+
{
18+
//Instantiation of DocIORenderer for Word to image conversion
19+
using (DocIORenderer render = new DocIORenderer())
20+
{
21+
//Convert the first page of the Word document into an image.
22+
Stream[] imageStreams = wordDocument.RenderAsImages();
23+
24+
//Combines multiple images from streams into a single image.
25+
CombineImages(imageStreams, "Output.png");
26+
27+
//Dispose the image streams.
28+
foreach (Stream imageStream in imageStreams)
29+
imageStream.Dispose();
30+
}
31+
}
32+
}
33+
}
34+
35+
/// <summary>
36+
/// Combines multiple images from streams into a single image.
37+
/// </summary>
38+
/// <param name="imageStreams">Streams containing the images to be combined.</param>
39+
/// <param name="outputPath">Output path where the combined image will be saved.</param>
40+
public static void CombineImages(Stream[] imageStreams, string outputPath)
41+
{
42+
if (imageStreams == null || imageStreams.Length == 0)
43+
throw new ArgumentException("No images to combine.");
44+
45+
// Load all images and get their dimensions
46+
SKBitmap[] bitmaps = new SKBitmap[imageStreams.Length];
47+
int maxWidth = 0;
48+
int totalHeight = 0;
49+
int margin = 20;
50+
51+
for (int i = 0; i < imageStreams.Length; i++)
52+
{
53+
bitmaps[i] = SKBitmap.Decode(imageStreams[i]);
54+
maxWidth = Math.Max(maxWidth, bitmaps[i].Width);
55+
totalHeight += bitmaps[i].Height + margin;
56+
}
57+
58+
// Add margins to the total width and height
59+
int combinedWidth = maxWidth + 2 * margin;
60+
// Add margin at the bottom
61+
totalHeight += margin;
62+
63+
// Create a new bitmap with the combined dimensions
64+
using (SKBitmap combinedBitmap = new SKBitmap(combinedWidth, totalHeight))
65+
{
66+
using (SKCanvas canvas = new SKCanvas(combinedBitmap))
67+
{
68+
// Set background color to the specified color
69+
canvas.Clear(new SKColor(240, 240, 240));
70+
71+
// Draw each bitmap onto the canvas
72+
int yOffset = margin;
73+
for (int i = 0; i < bitmaps.Length; i++)
74+
{
75+
int xOffset = (combinedWidth - bitmaps[i].Width) / 2; // Center the image horizontally
76+
canvas.DrawBitmap(bitmaps[i], new SKPoint(xOffset, yOffset));
77+
yOffset += bitmaps[i].Height + margin; // Add margin between rows
78+
}
79+
80+
// Save the combined bitmap to the output stream
81+
using (SKImage image = SKImage.FromBitmap(combinedBitmap))
82+
{
83+
using (SKData data = image.Encode(SKEncodedImageFormat.Png, 100))
84+
{
85+
using (FileStream stream = File.OpenWrite(outputPath))
86+
data.SaveTo(stream);
87+
}
88+
}
89+
}
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)