PDFiumでPDFファイルを表示したりテキストを読んだり画像に変換する ~Delphiソースコード集
PDFiumを使うとDelphiでPDFファイルを表示したりテキストを読んだり画像に変換することが出来ます。
必要なものは、①DLLファイル、②ライブラリファイルです。
①DLLファイルのダウンロード
DLLファイルは以下URLを開いて
https://github.com/bblanchon/pdfium-binaries
真ん中あたりにある
Windows ⇒ x86 ⇒ pdfium-win-x86.tgz
と
Windows ⇒ x64 ⇒ pdfium-win-x64.tgz
をダウンロードして解凍します。
「pdfium-win-x86.tgz」を解凍した「bin」ディレクトリにある「pdfium.dll」ファイルを
「Delphiのプロジェクトフォルダ\win32\debug」と「Delphiのプロジェクトフォルダ\win32\release」に配置して使います。
「pdfium-win-x64.tgz」を解凍した「bin」ディレクトリにある「pdfium.dll」ファイルを
「Delphiのプロジェクトフォルダ\win64\debug」と「Delphiのプロジェクトフォルダ\win64\release」に配置して使います。
②ライブラリファイルのダウンロード
PDFiumLibを使用させていただきます。
以下URLを開いて
https://github.com/ahausladen/PdfiumLib
「Code」⇒「Download Zip」をクリックして「PdfiumLib-master.zip」ファイルをダウンロードします。
ダウンロードした「PdfiumLib-master.zip」ファイルを解凍します。
「Source」ディレクトリ内にある「PdfiumCore.pas」「PdfiumCtrl.pas」「PdfiumLib.pas」ファイルを
「Delphiのプロジェクトフォルダ」にコピーして使います。
プロジェクトの作成
Delphiを起動し[ファイル]⇒[新規作成]⇒[VCL フォーム アプリケーション -Delphi]をクリックします。
フォームに「TButton」×2個と、「TPanel」×1個、「TMemo」×1個、「TOpenDialog」をドラッグ&ドロップします。
[ファイル]⇒[すべて保存]をクリックして、
「c:\Users\・・・\Embarcadero\Studio\Projects」に「VCL_PDFium」フォルダを作成して、ユニットを「Unit1.pas」、プロジェクトを「Project1.dproj」として保存します。
「VCL_PDFium」フォルダに「PdfiumCore.pas」「PdfiumCtrl.pas」「PdfiumLib.pas」ファイルをコピーします。
「VCL_PDFium\win32\debug」フォルダに「pdfium-win-x86.tgz」を解凍した「bin」ディレクトリにある「pdfium.dll」ファイルをコピーします。
ソースコードの記述
IDEを「コード」に切り替えて(「F12」キーを押す)以下ソースコードを貼り付けます。
ソースコードの'開きたいPDFファイル名を指定する'を開きたいPDFファイル名に変更します。
「デザイン」に切り替えて(「F12」キーを押す)、
「Form1のOnCreate」イベントプロパティに「FormCreate」を割り当て、
「Form1のOnDestroy」イベントプロパティに「FormDestroy」を割り当て、
「Button1のOnClick」イベントプロパティに「Button1Click」を割り当て、
「Button2のOnClick」イベントプロパティに「Button2Click」を割り当てます。
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, pdfiumcore, pdfiumCtrl; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Panel1: TPanel; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private 宣言 } PdfControl:TPdfControl; public { Public 宣言 } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var i:Integer; st:string; begin for i:=0 to PdfControl.PageCount-1 do begin PdfControl.PageIndex:=i; st:=PdfControl.GetTextInRect(pdfcontrol.GetPageRect); Memo1.Lines.Add(st); end; end; procedure TForm1.Button2Click(Sender: TObject); var i:Integer; r:TRect; bmp:TBitmap; begin bmp:=TBitmap.Create; try for i := 0 to PdfControl.PageCount-1 do begin r:=pdfcontrol.GetPageRect; bmp.Width:=2000; bmp.Height:=r.Height*2000 div r.Width; PdfControl.Document.Pages[i].Draw( bmp.Canvas.Handle,0,0, bmp.Width, bmp.Height ); bmp.SaveToFile(i.tostring+'.bmp'); end; finally bmp.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin PDFiumDllDir := ExtractFileDir(Application.ExeName); PdfControl := TPdfControl.Create(Self); PdfControl.Align := alClient; PdfControl.Parent := Panel1; PdfControl.LoadFromFile('開きたいPDFファイル名を指定する'); end; procedure TForm1.FormDestroy(Sender: TObject); begin PdfControl.Free; end; end.
実行する
実行して「Button1」をクリックするとテキストを抜き出して「Memo1」に出力します。
「Button2」をクリックすると、PDFの全ページがビットマップ画像として「Win32\Debug」フォルダに出力されます。