このガイドでは、PDFium VCL コンポーネントをわずか数分で使用開始できるよう支援します。
Pdf プロパティを TPdf コンポーネントに設定しますシンプルな PDF 読み込み:
// PDF ファイルを読み込む
Pdf1.FileName := 'C:\Sample.pdf';
Pdf1.Active := True;
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
// またはメモリストリームから読み込む
var
Stream: TMemoryStream;
begin
Stream := TMemoryStream.Create;
try
Stream.LoadFromFile('C:\Sample.pdf');
Pdf1.LoadDocument(Stream, True);
Pdf1.Active := True;
finally
Stream.Free;
end;
end;
// 次のページへ移動
if PdfView1.PageNumber < Pdf1.PageCount then
PdfView1.PageNumber := PdfView1.PageNumber + 1;
// 前のページへ移動
if PdfView1.PageNumber > 1 then
PdfView1.PageNumber := PdfView1.PageNumber - 1;
// 現在のページからすべてのテキストを抽出
Pdf1.PageNumber := 1;
Memo1.Text := Pdf1.Text;
// 矩形領域からテキストを抽出
var
RectText: WString;
begin
RectText := Pdf1.TextInRectangle(100, 100, 500, 200);
ShowMessage(RectText);
end;
// ドキュメント内のテキストを検索
var
Position: Integer;
begin
Position := Pdf1.FindFirst('search term');
if Position >= 0 then
ShowMessage('Found at position: ' + IntToStr(Position))
else
ShowMessage('Not found');
end;
// テキスト付きの新規 PDF を作成
Pdf1.CreateDocument;
Pdf1.Compressed := True; // ストリーム圧縮を有効化
Pdf1.Active := True;
Pdf1.AddPage(0, 595, 842); // A4 サイズ
Pdf1.AddText('Hello World!', 'Arial', 24, 100, 700, clBlack, $FF, 0.0);
Pdf1.SaveAs('C:\NewDocument.pdf');
Pdf1.Active := False;
// ズームレベルを設定 (1.0 = 100%)
PdfView1.Zoom := 1.5; // 150%
// 表示モードを設定
PdfView1.DisplayMode := dmSingleContinuous;
// ページを回転
PdfView1.Rotation := ro90;
// ドキュメントメタデータを読み取り
ShowMessage('Title: ' + Pdf1.Title);
ShowMessage('Author: ' + Pdf1.Author);
ShowMessage('Creator: ' + Pdf1.Creator);
ShowMessage('Producer: ' + Pdf1.Producer);
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
ShowMessage('Version: ' + IntToStr(Ord(Pdf1.PdfVersion)));
// パスワード保護された PDF を開く
Pdf1.FileName := 'C:\Protected.pdf';
Pdf1.Password := 'mypassword';
Pdf1.Active := True;
複数 GB の PDF やリモート / データベースバックエンドのソースでは、PDFium がファイル全体をメモリにバッファリングする代わりにブロックをオンデマンドで取得するように
LoadCustomDocument を使用します:
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create('C:\Huge.pdf', fmOpenRead or fmShareDenyWrite);
Pdf1.LoadCustomDocument(FileStream, True); // AOwnsStream
Pdf1.Active := True;
end;
FitMode は Resize 時および現在のページ変更時に、長いドキュメントのフレーミングを自動的に維持します:
PdfView1.FitMode := pfmFitPage; // ページ全体をビューポートに収める
// pfmFitWidth — ページ幅をビューポート幅に一致
// pfmActualSize — 100%
// pfmNone — 手動ズーム
// ダークモードビューアパターン:暗いスクロール領域、紙のように白い PDF ページ
PdfView1.Color := clBlack;
PdfView1.PageColor := clWhite;
HighlightSearchText は現在のビューページをスキャンし、再描画ごとにすべてのマッチに
HighlightColor マスクを描画します:
var
MatchCount: Integer;
begin
PdfView1.HighlightColor := clYellow;
MatchCount := PdfView1.HighlightSearchText('invoice');
Status.Caption := IntToStr(MatchCount) + ' matches';
end;
// 別のページに切り替えるとハイライトは自動的にクリアされます
SaveAsPdfA は基本保存をインクリメンタル更新で後処理し、XMP メタデータストリーム、sRGB ICC OutputIntent、更新されたドキュメント Catalog を注入します:
if Pdf1.SaveAsPdfA('C:\Report.pdfa.pdf', pac1b) then
ShowMessage('Saved PDF/A-1b');
// 受信ドキュメントの準拠を確認
case Pdf1.PdfAConformance of
pac1b: Log.Add('PDF/A-1b');
pacNone: Log.Add('Not PDF/A');
end;