1. Home
  2. Foxit Quick PDF Library
  3. How to render PDF page to device context using Delphi

How to render PDF page to device context using Delphi

In this post I would like to show you how easy it is to render a page from a PDF onto a graphic surface using Delphi and Debenu Quick PDF Library. The graphic surface in the Windows operating system is called a Device Context handle (DC). Debenu Quick PDF Library contains two different functions that can help us accomplish this. We can use the RenderPageToDC function that renders the currently selected document from memory or we can use the DARenderPageToDC function which uses direct access on disk instead of loading the entire file into memory for processing.

The source code that uses the first mentioned function is easier and can looks like this:

  //initialization of the library
  DPL  := TDebenuPDFLibrary.Create();
  //loading of the pdf file the path is up to you
  //I use 'd:/test.pdf'
  DPL.LoadFromFile('d:/test.pdf', '');
  //this will find out pdf page dimensions
  //and create blank bitmap with that dimensions
  width := Round(LPDF.PageWidth());
  height := Round(LPDF.PageHeight());
  Bmp := TBitmap.Create;
    Bmp.PixelFormat := pf24bit;
    Bmp.SetSize(w, h);
   //at last we call RenderPageToDC,
   //note the last parameter,
   //we use the blank bitmaps handle as graphic surface
    DPL.RenderPageToDC(72, 1, Bmp.Canvas.Handle);
  //last thing we need to do is to copy
  //the bitmap with rendered page to scrollbox to show it
  BitBlt(GetDC(ScrollBox1.Handle), 0, 0, w, h, Bmp.Canvas.Handle, 0, 0, SRCCOPY);
  //and don't forget to clean your memory
Updated on March 19, 2019

Was this article helpful?

Related Articles