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 Foxit Quick PDF Library. The graphic surface in the Windows operating system is called a Device Context handle (DC). Foxit 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;
Now we will modify the code for use with the direct access functions:
begin
//initialization of the library
DPL := TDebenuPDFLibrary.Create();
DPL.UnlockKey('UNLOCKCODE');
try
//we need to get the FileHandle ID using DAOpenFile function
FileHandle := DPL.DAOpenFile('d:/test.pdf', '');
if FileHandle>0
then
//Now that we have the FileHandle
//we need to find the Page Reference ID for the requested page
PageRef := DPL.DAFindPage(FileHandle, 2);
if PageRef>0
then
begin
//this is similar as the first sample
//we get the dimensions but now
//we need to use direct access functions for dimensions
//then we again create blank bitmap
width := Round(DPL.DAGetPageWidth(FileHandle, PageRef));
height := Round(DPL.DAGetPageHeight(FileHandle, PageRef));
Bmp := TBitmap.Create;
try
Bmp.PixelFormat := pf24bit;
Bmp.SetSize(w, h);
finally
end;
//now we can call DARenderPageToDC,
//again we use the blank bitmaps handle as graphic surface
//and the the FileHandle ID and Page Reference ID
DPL.DARenderPageToDC(FileHandle, PageRef, 72, Bmp.Canvas.Handle);
end;
finally
end;
//again we need 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 finally clean memory
Bmp.Free;
DPL.Destroy();
end;
And that’s it. As you can see, it’s not rocket science to render a page to a device context using Foxit Quick PDF Library.
This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.
Updated on May 16, 2022