技術文章

在 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 開發。