Using PDFium in Delphi: PDF Rendering, Text Extraction, and Image Conversion with Sample Code
PDFium is a high‑performance PDF rendering engine also used in Google Chrome.
This page explains how to use PDFium in Delphi to display PDF files, extract text, and convert pages into images.
It covers everything you need: (1) where to obtain the required DLL files, (2) where to get the libraries such as PdfiumCore.pas, implementation steps for VCL forms, event setup, and complete sample code.
① Downloading the DLL Files
Download the DLL files from the following URL:
https://github.com/bblanchon/pdfium-binaries
In the middle of the page, download:
Windows ⇒ x86 ⇒ pdfium-win-x86.tgz
and
Windows ⇒ x64 ⇒ pdfium-win-x64.tgz
then extract both archives.
From the extracted pdfium-win-x86.tgz archive, take the pdfium.dll file located inside the bin directory and place it in:
Delphi project folder\win32\debug and Delphi project folder\win32\release.
From the extracted pdfium-win-x64.tgz archive, take the pdfium.dll file located inside the bin directory and place it in:
Delphi project folder\win64\debug and Delphi project folder\win64\release.
② Downloading the Library Files
This guide uses the PDFiumLib library.
Download it from the following URL:
https://github.com/ahausladen/PdfiumLib
Click "Code" => "Download ZIP" to download PdfiumLib-master.zip.
Extract the downloaded ZIP file.
Inside the Source directory, copy the following files into your Delphi project folder:
PdfiumCore.pas, PdfiumCtrl.pas, PdfiumLib.pas.
Creating the Project
Start Delphi and select [File] ⇒ [New] ⇒ [VCL Forms Application – Delphi].
Place two TButton components, one TPanel, one TMemo, and one TOpenDialog onto the form.
Click [File] ⇒ [Save All], then create a folder named VCL_PDFium under
C:\Users\...\Embarcadero\Studio\Projects.
Save the unit as Unit1.pas and the project as Project1.dproj.
Copy the files PdfiumCore.pas, PdfiumCtrl.pas, and PdfiumLib.pas into the VCL_PDFium folder.
Then copy the pdfium.dll file from the extracted pdfium-win-x86.tgz archive (inside the bin directory) into:
VCL_PDFium\win32\debug.
Writing the Source Code
Switch the IDE to the "Code" view (press F12) and paste the source code below.
Replace the part marked ‘Specify the PDF file you want to open’ with the actual PDF file name you want to load.
Switch back to the "Design" view (press F12) and assign the following event handlers:
• Set Form1.OnCreate to FormCreate
• Set Form1.OnDestroy to FormDestroy
• Set Button1.OnClick to Button1Click
• Set Button2.OnClick to Button2Click
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, pdfiumcore, pdfiumCtrl; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Panel1: TPanel; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } PdfControl:TPdfControl; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var i:Integer; st:string; begin for i:=0 to PdfControl.PageCount-1 do begin PdfControl.PageIndex:=i; st:=PdfControl.GetTextInRect(pdfcontrol.GetPageRect); Memo1.Lines.Add(st); end; end; procedure TForm1.Button2Click(Sender: TObject); var i:Integer; r:TRect; bmp:TBitmap; begin bmp:=TBitmap.Create; try for i := 0 to PdfControl.PageCount-1 do begin r:=pdfcontrol.GetPageRect; bmp.Width:=2000; bmp.Height:=r.Height*2000 div r.Width; PdfControl.Document.Pages[i].Draw( bmp.Canvas.Handle,0,0, bmp.Width, bmp.Height ); bmp.SaveToFile(i.tostring+'.bmp'); end; finally bmp.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin PDFiumDllDir := ExtractFileDir(Application.ExeName); PdfControl := TPdfControl.Create(Self); PdfControl.Align := alClient; PdfControl.Parent := Panel1; PdfControl.LoadFromFile('Specify the PDF file you want to open'); end; procedure TForm1.FormDestroy(Sender: TObject); begin PdfControl.Free; end; end.
Running the Program
When you run the application and click Button1, the text content of the PDF is extracted and displayed in Memo1.
When you click Button2, all pages of the PDF are exported as bitmap images and saved in the Win32\Debug folder.
