Building a Feature-Rich PDF Viewer with PDFium VCL in Delphi
In this tutorial, we’ll explore how to create a professional PDF viewer application using PDFium VCL, a powerful Delphi component that wraps Google’s PDFium rendering engine. This demo showcases the core viewing capabilities that form the foundation of any PDF application.
Overview
The PDF Viewer demo demonstrates the essential functionality needed for viewing PDF documents in a Delphi application. It provides a complete, ready-to-use PDF viewing solution with features comparable to commercial PDF readers.
Key Features
- Document Loading – Open PDF files with password protection support
- Page Navigation – First, previous, next, last page buttons with keyboard shortcuts
- Zoom Controls – Multiple zoom levels including fit-to-page and fit-to-width
- Page Rotation – Rotate pages 90° left or right
- Text Selection – Select and copy text from PDF documents
- Bookmarks – Navigate using the document’s table of contents
- Text Search – Find text within the document
- Printing – Print documents with page range selection
- Save As – Export the document to a new PDF file
PDFium DLL Requirements
Before running any PDFium VCL application, you must install the PDFium DLL files. The DLLs are located in the DLLs folder of the PDFium VCL package:
pdfium32.dll– 32-bit version (~5 MB)pdfium64.dll– 64-bit version (~6 MB)pdfium32v8.dll– 32-bit with V8 JavaScript engine (~23 MB)pdfium64v8.dll– 64-bit with V8 JavaScript engine (~27 MB)
Installation: Run PDFiumVCL\DLLs\CopyDlls.bat as Administrator. This script automatically copies the appropriate DLLs to the Windows system directories:
|
1
2
3
4
5
6
|
@echo off
REM On 64-bit Windows:
REM - 32-bit DLLs → %SystemRoot%\SysWOW64\
REM - 64-bit DLLs → %SystemRoot%\System32\
REM On 32-bit Windows:
REM - 32-bit DLLs → %SystemRoot%\System32\
|
Note: Use the standard DLLs (pdfium32.dll/pdfium64.dll) for most applications. The V8 versions are only needed if your PDFs contain JavaScript that requires execution.
Core Components
The demo uses two main PDFium VCL components:
|
1
2
|
Pdf: TPdf; // Non-visual component for PDF operations
PdfView: TPdfView; // Visual component for rendering PDF pages
|
TPdf Component
The TPdf component handles all PDF document operations including loading, saving, and accessing document properties like metadata, bookmarks, and page information.
TPdfView Component
The TPdfView component is a scrollable visual control that renders PDF pages with smooth scrolling, zoom support, and user interaction handling.
Loading a PDF Document
Opening a PDF file is straightforward:
|
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
|
procedure TFormMain.SpeedButtonOpenPdfClick(Sender: TObject);
var
Password: string;
begin
if OpenDialog.Execute then
begin
Pdf.FileName := OpenDialog.FileName;
Pdf.Password := '';
Pdf.PageNumber := 0;
try
PdfView.Active := True;
except
on Error: EPdfError do
if Error.Message = 'Password required or incorrect password' then
begin
if InputQuery('Enter Password', 'Password: ', Password) then
begin
Pdf.Password := Password;
PdfView.Active := True;
end
else
raise;
end
else
raise;
end;
if PdfView.PageCount > 0 then
PdfView.PageNumber := 1;
end;
end;
|
Page Navigation
Implementing page navigation is simple with the PageNumber property:
|
1
2
3
4
5
6
7
8
9
10
11
|
// Navigate to first page
PdfView.PageNumber := 1;
// Navigate to last page
PdfView.PageNumber := PdfView.PageCount;
// Previous page
PdfView.PageNumber := PdfView.PageNumber - 1;
// Next page
PdfView.PageNumber := PdfView.PageNumber + 1;
|
Zoom Control
The TPdfView component offers flexible zoom options:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// Set specific zoom levels
PdfView.Zoom := 1.0; // 100%
PdfView.Zoom := 0.5; // 50%
PdfView.Zoom := 2.0; // 200%
// Fit to page width
PdfView.Zoom := PdfView.PageWidthZoom[PdfView.PageNumber];
// Fit entire page in view
PdfView.Zoom := PdfView.PageZoom[PdfView.PageNumber];
// Actual size (based on DPI)
PdfView.Zoom := PdfView.ActualSizeZoom[PdfView.PageNumber];
|
Page Rotation
Rotate pages using the Rotation property:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// Rotate right (clockwise)
case PdfView.Rotation of
ro0: PdfView.Rotation := ro90;
ro90: PdfView.Rotation := ro180;
ro180: PdfView.Rotation := ro270;
ro270: PdfView.Rotation := ro0;
end;
// Rotate left (counter-clockwise)
case PdfView.Rotation of
ro0: PdfView.Rotation := ro270;
ro90: PdfView.Rotation := ro0;
ro180: PdfView.Rotation := ro90;
ro270: PdfView.Rotation := ro180;
end;
|
Displaying Document Information
Access document metadata through the TPdf component:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
procedure TFormMain.SpeedButtonShowInfoClick(Sender: TObject);
begin
ShowMessage(
'Author: ' + Pdf.Author + #13#10 +
'Creator: ' + Pdf.Creator + #13#10 +
'Keywords: ' + Pdf.Keywords + #13#10 +
'Producer: ' + Pdf.Producer + #13#10 +
'Subject: ' + Pdf.Subject + #13#10 +
'Title: ' + Pdf.Title + #13#10 +
'Creation date: ' + Pdf.CreationDate + #13#10 +
'Modified date: ' + Pdf.ModifiedDate
);
end;
|
Working with Bookmarks
The demo populates a TreeView with the document’s bookmarks for easy navigation:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
procedure TFormMain.AddBookmarks(Node: TTreeNode; Bookmarks: TBookmarks);
var
ChildNode: TTreeNode;
I: Integer;
begin
for I := 0 to Length(Bookmarks) - 1 do
begin
ChildNode := TreeViewBookmarks.Items.AddChildObject(
Node,
Bookmarks[I].Title,
Bookmarks[I].Handle
);
ChildNode.HasChildren := Pdf.HasBookmarkChildren[Bookmarks[I]];
if ChildNode.HasChildren then
AddBookmarks(ChildNode, Pdf.BookmarkChildren[Bookmarks[I]]);
end;
end;
|
Render Options
Customize rendering with various options:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// Available render options
type
TRenderOption = (
reAnnotations, // Render annotations
reLcd, // LCD optimized rendering
reNoNativeText, // Don't use native text output
reGrayscale, // Render in grayscale
reLimitCache, // Limit image cache size
reHalftone, // Use halftone for image stretching
rePrinting, // Optimize for printing
reNoSmoothText, // Disable text anti-aliasing
reNoSmoothImage, // Disable image anti-aliasing
reNoSmoothPath // Disable path anti-aliasing
);
// Apply options to the view
PdfView.Options := [reAnnotations, reLcd];
|
Conclusion
The PDF Viewer demo provides a solid foundation for building PDF viewing functionality into your Delphi applications. With PDFium VCL, you get access to the same PDF rendering engine used by Google Chrome, ensuring high-quality, accurate PDF display.
The component handles complex PDF features like annotations, form fields, and embedded fonts automatically, allowing you to focus on building your application’s unique features rather than low-level PDF parsing.
Download PDFium VCL from loslab.com to get started with PDF development in Delphi.
浙公网安备 33010602011771号