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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;

//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;

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 Debenu Quick PDF Library.