技术文章

在 Delphi 中使用 PDFium VCL 构建功能丰富的 PDF 查看器

· PDF 编程

在本教程中,我们将探讨如何使用 PDFium VCL 创建一个专业的 PDF 观看器应用程序。 PDFium VCL 是一种强大的 Delphi 组件,它封装了 Google 的 PDFium 渲染引擎。此演示展示了构成任何 PDF 应用程序基础的核心查看功能。

概述

PDF 观看器演示程序展示了在 Delphi 应用程序中查看 PDF 文档所需的 essential 功能。它提供了一个完整、可立即使用的 PDF 查看解决方案,其功能可与商业 PDF 阅读器相媲美。

主要特性

  • 文档加载 – 支持密码保护的 PDF 文件打开
  • 页面导航 – 包含“第一页”、“上一页”、“下一页”、“最后一页”按钮,并带有键盘快捷方式。
  • 缩放控制 – 多种缩放级别,包括自适应页面和自适应宽度
  • 页面旋转 – 将页面向左或向右旋转 90°
  • 文本选择 – 从 PDF 文档中选择和复制文本
  • 书签 – 使用文档的目录导航
  • 文本搜索 查找文档中的文本。
  • 打印。 打印具有分页选择功能的文档。
  • 另存为。 将文档导出为新的 PDF 文件。

PDFium DLL 的要求

在运行任何 PDFium VCL 应用程序之前,必须安装 PDFium DLL 文件。这些 DLL 文件位于: DLLs PDFium VCL 软件包的文件夹:

  • pdfium32.dll 32 位版本(约 5 MB)。
  • pdfium64.dll 64 位版本(约 6 MB)。
  • pdfium32v8.dll 32位版本,带V8 JavaScript引擎(约23 MB)。
  • pdfium64v8.dll 64位版本,带V8 JavaScript引擎(约27 MB)。

安装: 运行 PDFiumVCL\DLLs\CopyDlls.bat 以管理员身份运行。此脚本会自动将适当的DLL文件复制到Windows系统目录中:

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\

注意: 大多数应用程序应使用标准DLL文件()。pdfium32.dll/pdfium64.dllV8版本仅在您的PDF文件中包含需要执行的JavaScript时才需要。

核心组件

该演示程序使用了两个主要的PDFium VCL组件:

1
2
Pdf: TPdf;        // Non-visual component for PDF operations
PdfView: TPdfView; // Visual component for rendering PDF pages

TPdf 组件

好的。 TPdf 该组件处理所有 PDF 文档操作,包括加载、保存以及访问文档属性,例如元数据、书签和页面信息。

TPdfView 组件

好的。 TPdfView 该组件是一个可滚动可视控件,用于渲染 PDF 页面,支持平滑滚动、缩放以及用户交互处理。

加载 PDF 文档

打开 PDF 文件非常简单:

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;

页面导航

使用以下方法可以轻松实现页面导航: PageNumber 属性支持多种显示模式:

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;

缩放控制

好的。 TPdfView component 提供了灵活的缩放选项:

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];

页面旋转

使用以下方法旋转页面: Rotation 属性支持多种显示模式:

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;

显示文档信息

通过以下方式访问文档元数据: 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;

使用书签

演示程序使用 TreeView 填充文档的书签,以便于导航:

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;

渲染选项。

使用各种选项自定义渲染。

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];

结论。

PDF Viewer 演示版为将 PDF 查看功能集成到您的 Delphi 应用程序中提供了坚实的基础。通过 PDFium VCL,您可以访问与 Google Chrome 相同的 PDF 渲染引擎,从而确保高质量、准确的 PDF 显示。

该组件自动处理复杂的 PDF 功能,例如批注、表单字段和嵌入式字体,让您可以专注于构建应用程序的独特功能,而无需处理低级别的 PDF 解析。

下载 PDFium VCL 从 loslab.com 开始在 Delphi 中进行 PDF 开发。