Draw page to a vector image

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Processing >

Draw page to a vector image

In this section we present example code to draw a PDF page on to a vector 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 vector image
   using (var image = graphics.CreateVectorImage(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);
      }
 
      // Save the image to file
      var nativeImage = image.GetNativeImage();
      if (nativeImage is System.Drawing.Imaging.Metafile metafile)
      {
         metafile.Save("page_image.png");
      }
   }
}