|
<< Click to Display Table of Contents >> Navigation: Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Processing > Draw page to a raster image |
In this section we present example code to draw a PDF page on to a raster image . Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.
Given below is the sample code.
string inputFile = @"input.pdf";
float renderingDpi = 96;
using (var pdfDoc = new PDF())
{
pdfDoc.LoadDocument(inputFile);
var page = pdfDoc.GetPage(pageNum: 1);
// Get page width and height in inches
float pageWidth = page.GetWidth(MeasurementUnit.Inches);
float pageHeight = page.GetHeight(MeasurementUnit.Inches);
// Create a bitmap for the page and a surface from the bitmap
var graphics = Framework.Graphics;
// Calculate scale factor based on rendering DPI
var systemDpi = graphics.SystemDPI;
var scaleFactor = renderingDpi / systemDpi;
// Convert the length unit from inches to pixels
pageWidth *= systemDpi;
pageHeight *= systemDpi;
// Scale the page width and height according to the rendering DPI
pageWidth *= scaleFactor;
pageHeight *= scaleFactor;
// Create a bitmap image
using (var image = graphics.CreateImage(new Size(pageWidth, pageHeight)))
{
using (var surface = image.GetGraphicsSurface())
{
// Scale the graphics surface
surface.CurrentGraphicsState.Scale(scaleFactor, scaleFactor);
// Draw the page onto the graphics surface
page.Draw(surface, renderFormFields: false);
}
// Set image resolution
image.SetResolution(renderingDpi, renderingDpi);
// Save the image to file as PNG
image.Save(@"page_image.png", new PNGEncoderSettings());
}
}