How to convert PDF page images to one image in ASP.NET Core PDF?
The Syncfusion PDFtoImageConverter library can be used to convert PDF pages into images in ASP.NET Core PDF. While there isn’t a direct API to convert an entire PDF document into a single image file, this can be accomplished by first converting each page into an individual image stream using the Convert API. Afterward, SkiaSharp can be used to merge these separate image streams into a single image.
Steps to convert multiple pdf page images to a single image using PdfToImageConverter
Step 1 : Create a new instance of the PdfToImageConverter. Open the source PDF file as a FileStream and load it into the converter using the Load method.
//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("../../../Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
Step 2: Use the Convert API to process the document. This method returns an array of Stream objects, where each stream contains the image data for a single page.
//Convert PDF to Image.
Stream[] imageStreams = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
Step 3: Create a array of SKBitmap objects to hold the Image stream collection.
// Load all images and get their dimensions
SKBitmap[] bitmaps = new SKBitmap[imageStreams.Length];
Step 4 : To prepare the final layout, the system first measures each of your individual page images to determine the necessary dimensions. It identifies the widest page to set the final image’s width, ensuring no content is cut off. It then calculates the total height by adding up the height of each page, plus a margin for clean spacing between them. Finally, a border is added around the entire composite image to give it a polished and professional finish.
for (int i = 0; i < imageStreams.Length; i++)
{
imageStreams[i].Position = 0;
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;
Step 5: To create the final output, a new, larger blank image is first prepared to serve as the main canvas. This canvas is then filled with a light gray background color to provide a clean, uniform look. The process takes each of the individual page images and draws them one by one onto this canvas. Each image is automatically centered horizontally for a balanced appearance and is placed vertically below the previous one, with a consistent margin creating clear spacing between each page. After all the page images have been neatly arranged on the canvas, the final composite image is saved as a single PNG file.
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);
}
}
}
A complete working sample for Convert PDF pages to single image can be downloaded from GitHub.
The execution of the above code results can convert the PDF all pages to single image, appearing as follows.
Conclusion
I hope you enjoyed learning about how to convert PDF page images to one image in ASP.NET Core PDF.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example 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!