PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder

Convert PDF To High-Resolution Images Using Delphi

Use PDFtoolkit to export PDF pages to images.
By V. Subhash
Important

With the release of Version 4 of PDFtoolkit, PDF-to-image conversion is much more simpler. It takes a single method call - TgtPDFDocument.SaveAsImage(). A special article titled "PDF-to-Image Export Using PDFtoolkit V4" has been written for users of Version 4. This article has been left here for users of older versions of PDFtoolkit.

PDFtoolkit offers two methods to export content from PDF pages. Both methods have DPI parameters that allows you to zoom in or out the content if required.

public procedure RenderToDC(
    aDC: Cardinal;
    aWidth: Cardinal;
    aHeight: Cardinal;
    aPageNum: Integer;
    axDPI: Double = 72;
    ayDPI: Double = 72;
    aRotationAngle: Integer = 0;
    aAntiAliasing: Boolean = True;
    aForPrinting: Boolean = False
);

public procedure RenderToStream(
    aEMFPlusStream: TStream; 
    aPageNum: Integer; 
    aWidth: double; 
    aHeight: double; 
    axDPI: Double = 72; 
    ayDPI: Double = 72; 
    aRotationAngle: Integer = 0; 
    aAntiAliasing: Boolean = True; 
    aForPrinting: Boolean = False
); overload;

In an earlier article, we saw how to use RenderToStream() method to convert PDF to a GDI image. In this article, we will see how to use the RenderToDC() method to convert a PDF page to a simple image.

Delphi Code For PDF-To-Image Conversion

Following is a code example for converting a PDF to a high-resolution image. The RenderToDC() method requires a Windows handle to which page contents can be exported. For that, we supply the canvas handle of a bitmap image. The dimensions of the bitmap is set to that of the page at the required output DPI. Calculation of dimensions is a bit awkward because we need to downcast the values to an integer. Unless we use insane values, the code will not blow up. After calling RenderToDC(), we can save the bitmap to file.

{

 This program converts page 2 of a PDF document (sample_doc.pdf) to
 a high-resolution image (page2.bmp).
}

program PDF_to_HiRes_Image;

{$APPTYPE CONSOLE}

uses
  SysUtils, Graphics,
  gtPDFClasses, gtCstPDFDoc, gtExPDFDoc, gtExProPDFDoc, gtPDFDoc;

var
  gtPDFDocument1: TgtPDFDocument;
  Bitmap1: TBitmap;
  Output_DPI, Desktop_DPI: Integer;
begin
  Output_DPI := 120;   // Required DPI of hi-res image
  Desktop_DPI := 96;   // Current DPI of screen or in PDF viewer

  try
    // Load a PDF document
    gtPDFDocument1 := TgtPDFDocument.Create(nil);
    gtPDFDocument1.LoadFromFile('sample_doc.pdf');

    // Create a bitmap with size equal to that of
    // second page of the document at the required DPI
    Bitmap1 := TBitmap.Create;
    Bitmap1.Width := Round(gtPDFDocument1.GetPageSize(2, muPixels).Width *
                           (Output_DPI/Desktop_DPI));   // Warning
    Bitmap1.Height := Round(gtPDFDocument1.GetPageSize(2, muPixels).Height *
                            (Output_DPI/Desktop_DPI));  // Warning

    // Render contents of page 2 to bitmap
    gtPDFDocument1.RenderToDC(
                      Bitmap1.Canvas.Handle,  // Handle of the bitmap canvas
                      Bitmap1.Width,          // Width of page at required DPI
                      Bitmap1.Height,         // Height of page at required DPI
                              2,              // Number of the page
                              Output_DPI,     // Horizontal DPI
                              Output_DPI,     // Vertical DPI
                              0,              // Angle of rotation
                              True,           // Enable anti-aliasing
                              False);         // Not for printing
    // Save bitmap to file
    Bitmap1.SaveToFile('page2.bmp');

    // Free image
    Bitmap1.FreeImage;
    // Unload document
    gtPDFDocument1.Reset;
  except on Err:Exception do
    begin
      Writeln('Sorry, an exception was raised. ');
      Writeln(Err.Classname + ':' + Err.Message);
      Readln;
    end;
  end;

end.

In Pictures

PDF Page #2 at 96 DPI
PDF Page #2 at 120 DPI
PDF Page #2 exported at 120 DPI to a bimap image

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.