技術記事

Delphi で PDFium VCL を使い機能豊富な PDF ビューアを構築する

· PDFプログラミング

このチュートリアルでは、PDFium VCLを使って、プロフェッショナルなPDF ビューアアプリケーションを作成する方法について説明します。 PDFium VCLは、GoogleのPDFiumレンダリングエンジンをラップする、強力なDelphiコンポーネントです。このデモでは、あらゆるPDFアプリケーションの基盤となる基本的な表示機能を実証します。

概要

PDF ビューアのデモは、DelphiアプリケーションでPDF ドキュメントを表示するために必要な基本的な機能を示しています。

主な機能

  • ドキュメントの読み込み – パスワード保護に対応したPDF ファイルのオープン
  • ページナビゲーション – 最初のページ、前のページ、次のページ、最後のページボタンと、キーボードショートカット
  • ズームコントロール – 複数のズームレベル、ページ全体表示と幅全体表示を含む
  • ページ回転 – ページを左または右に90度回転
  • テキスト選択 – PDF ドキュメントからテキストを選択してコピー
  • ブックマーク – ドキュメントの目次を使ってナビゲート
  • テキスト検索 – 文書内のテキストを検索
  • 印刷 – ページ範囲指定で文書を印刷
  • 名前を付けて保存 – 文書を新しいPDF ファイルとしてエクスポート

PDFium DLL の要件

PDFium VCLアプリケーションを実行する前に、PDFium DLLファイルをインストールする必要があります。DLLファイルは次のフォルダにあります。 DLLs PDFium VCLパッケージのフォルダ:

  • pdfium32.dll – 32ビット版(約5MB)
  • pdfium64.dll – 64ビット版(約6MB)
  • 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が含まれる場合にのみ必要です。

中核コンポーネント

デモプログラムでは、2つの主要なPDFium VCLコンポーネントを利用します:

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

TPdfコンポーネント

The TPdf このコンポーネントは、すべてのPDF ドキュメント操作を処理します。これには、読み込み、保存、ドキュメントのプロパティ(メタデータ、ブックマーク、ページ情報など)へのアクセスが含まれます。

TPdfViewコンポーネント

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

ズームコントロール

The TPdfView このコンポーネントは、柔軟なズームオプションを提供します。

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 コンポーネントを通じてアクセスできます。

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 デモは、Delphi アプリケーションに PDF ビューイング機能を組み込むための堅牢な基盤を提供します。PDFium VCL を使うと、Google Chrome で利用されているのと同じ PDF レンダリングエンジンにアクセスでき、高品質で正確な PDF 表示を実現します。

このコンポーネントは、注釈、フォームフィールド、埋め込みフォントなど、複雑な PDF 機能を自動的に処理するため、低レベルの PDF 解析ではなく、アプリケーション独自の機能の開発に集中できます。

PDFium VCL を loslab.com からダウンロードして、Delphi での PDF 開発を開始してください。 loslab.com から PDFium VCL をダウンロードして、Delphi での PDF 開発を開始してください。