Back to Blog

    Developer Blog

    How to render PDF page to device context using Delphi

    Foxit Quick PDF Library

    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:

    begin
      //initialization of the library
      DPL  := TDebenuPDFLibrary.Create();
      DPL.UnlockKey('UNLOCKCODE');
     
      //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;
      try
        Bmp.PixelFormat := pf24bit;
        Bmp.SetSize(w, h);
      finally
      end;
     
       //at last we call RenderPageToDC,
       //note the last parameter,
       //we use the blank bitmaps handle as graphic surface
       try
        DPL.RenderPageToDC(72, 1, Bmp.Canvas.Handle);
      finally
      end;
     
      //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
      Bmp.Free;
     
      DPL.Destroy();
    end;
    

    This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.