How to convert entire Word document to images and combine it as single image using C#?
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can convert Word document to images and combine it as single image in C#.
Steps to convert Word document pages to single image in C#
Step 1: Create a new .NET console application project.
Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
using SkiaSharp;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
//Open the file as Stream
using (FileStream docStream = new FileStream(@"Template.docx", FileMode.Open, FileAccess.Read))
{
//Loads an existing Word document
using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))
{
//Instantiation of DocIORenderer for Word to image conversion
using (DocIORenderer render = new DocIORenderer())
{
//Convert the first page of the Word document into an image.
Stream[] imageStreams = wordDocument.RenderAsImages();
//Combines multiple images from streams into a single image.
CombineImages(imageStreams, "Output.png");
//Dispose the image streams.
foreach (Stream imageStream in imageStreams)
imageStream.Dispose();
}
}
}
Step 5: Helper method to combine the converted image streams to single image using SkiaSharp.
/// <summary>
/// Combines multiple images from streams into a single image.
/// </summary>
/// <param name="imageStreams">Streams containing the images to be combined.</param>
/// <param name="outputPath">Output path where the combined image will be saved.</param>
public static void CombineImages(Stream[] imageStreams, string outputPath)
{
if (imageStreams == null || imageStreams.Length == 0)
throw new ArgumentException("No images to combine.");
// Load all images and get their dimensions
SKBitmap[] bitmaps = new SKBitmap[imageStreams.Length];
int maxWidth = 0;
int totalHeight = 0;
int margin = 20;
for (int i = 0; i < imageStreams.Length; i++)
{
bitmaps[i] = SKBitmap.Decode(imageStreams[i]);
maxWidth = Math.Max(maxWidth, bitmaps[i].Width);
totalHeight += bitmaps[i].Height + margin;
}
// Add margins to the total width and height
int combinedWidth = maxWidth + 2 * margin;
// Add margin at the bottom
totalHeight += margin;
// Create a new bitmap with the combined dimensions
using (SKBitmap combinedBitmap = new SKBitmap(combinedWidth, totalHeight))
{
using (SKCanvas canvas = new SKCanvas(combinedBitmap))
{
// Set background color to the specified color
canvas.Clear(new SKColor(240, 240, 240));
// Draw each bitmap onto the canvas
int yOffset = margin;
for (int i = 0; i < bitmaps.Length; i++)
{
int xOffset = (combinedWidth - bitmaps[i].Width) / 2; // Center the image horizontally
canvas.DrawBitmap(bitmaps[i], new SKPoint(xOffset, yOffset));
yOffset += bitmaps[i].Height + margin; // Add margin between rows
}
// Save the combined bitmap to the output stream
using (SKImage image = SKImage.FromBitmap(combinedBitmap))
{
using (SKData data = image.Encode(SKEncodedImageFormat.Png, 100))
{
using (FileStream stream = File.OpenWrite(outputPath))
data.SaveTo(stream);
}
}
}
}
}
Conclusion
I hope you enjoyed learning about how to check fonts in ASP.NET Core Word document are available.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!