PDFium VCL で PDF ドキュメントを作成する
PDFium VCL は PDF の表示や解析だけでなく、新しい PDF ドキュメントの作成にも利用できます。このデモでは、Delphi アプリケーションからページを作成し、テキスト、図形、表、透かし、画像を配置する基本的な流れを確認します。
概要
- 新しい PDF 文書とページを作成する。
- 座標、フォント、色を指定してテキストを配置する。
- 線、矩形、複雑なパスを描画する。
- 表、透かし、画像を組み合わせた複数ページ文書を生成する。
PDFium DLL の要件
PDFium VCL を使うアプリケーションでは、実行ファイルと同じビット数の PDFium DLL を配置します。開発環境、テスト環境、配布パッケージで同じ DLL を使うようにすると、描画差やロード失敗を避けやすくなります。
新しい 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 |
procedure CreateNewPdf; var Pdf: TPdf; begin Pdf := TPdf.Create(nil); try // Create empty document Pdf.CreateDocument; Pdf.Active := True; // Add first page (A4 size: 595 x 842 points) Pdf.AddPage(1, 595, 842); Pdf.PageNumber := 1; // Add content here... // Save the document Pdf.SaveAs('output.pdf'); finally Pdf.Active := False; Pdf.Free; end; end; |
テキストを追加する
フォント名、サイズ、色、座標を明示して、ページ上の期待した位置に文字列を描画します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
procedure TPdf.AddText( const Text, Font: WString; // Text content and font name FontSize: Single; // Font size in points X, Y: Double; // Position (from bottom-left) Color: TColor = clBlack; // Text color Alpha: Byte = $FF; // Transparency (0-255) Angle: Double = 0.0 // Rotation angle in degrees ); // Examples Pdf.AddText('Hello World', 'Arial', 24, 50, 750, clBlack, $FF, 0.0); Pdf.AddText('Subtitle', 'Times New Roman', 14, 50, 720, clGray, $FF, 0.0); Pdf.AddText('Rotated Text', 'Arial', 18, 300, 400, clRed, $80, 45.0); |
完全な作成例
テキストと基本的なページ設定を含む、最小限の 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 33 34 35 36 37 |
procedure TFormMain.CreateSimplePDF; begin Pdf.CreateDocument; Pdf.Active := True; try // Add A4 page Pdf.AddPage(0, 595, 842); // Title Pdf.AddText(EditTitle.Text, 'Arial', 18, 50, 750, clBlack, $FF, 0.0); // Author line Pdf.AddText('By: ' + EditAuthor.Text, 'Arial', 12, 50, 720, clGray, $FF, 0.0); // Body text Pdf.AddText('This is a simple PDF document created with PDFium VCL.', 'Arial', 12, 50, 680, clBlack, $FF, 0.0); Pdf.AddText('You can add various elements to your PDF:', 'Arial', 12, 50, 650, clBlack, $FF, 0.0); // Optional features if CheckShapes.Checked then AddShapes; if CheckTable.Checked then AddTable; if CheckWatermark.Checked then AddWatermark; // Save Pdf.SaveAs('Sample.pdf'); finally Pdf.Active := False; end; end; |
図形とパスを描画する
線や曲線を組み合わせると、罫線、枠、図版、簡単なアイコンを PDF 内で直接表現できます。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Create a path starting at (X, Y) procedure TPdf.CreatePath( X, Y: Single; // Starting point FillMode: TPdfFillMode = fmNone; // None, Alternate, or Winding FillColor: TColor = clBlack; // Fill color FillAlpha: Byte = $FF; // Fill transparency Stroke: Boolean = True; // Draw outline StrokeColor: TColor = clBlack; // Outline color StrokeAlpha: Byte = $FF; // Outline transparency StrokeWidth: Single = 1.0; // Line width LineCap: TPdfLineCap = lcDefault; // Line end style LineJoin: TPdfLineJoin = ljDefault; // Line join style BlendMode: TPdfBlendMode = bmDefault // Color blending ); // Create a rectangle path procedure TPdf.CreatePath( X, Y, Width, Height: Single; // Rectangle bounds // Same optional parameters as above... ); |
表、透かし、画像を追加する
帳票やレポートでは、表形式データ、薄い透かし、ロゴ画像を組み合わせることがよくあります。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
procedure TFormMain.AddShapes; begin // Filled rectangle with blue color at 50% opacity Pdf.CreatePath(100, 550, 150, 80, fmWinding, $FFCCAA, $80); Pdf.AddPath; Pdf.AddText('Rectangle', 'Arial', 10, 120, 580, clBlack, $FF, 0.0); // Another filled shape (green) Pdf.CreatePath(350, 590, 80, 80, fmWinding, $CCFFCC, $80); Pdf.AddPath; Pdf.AddText('Shape', 'Arial', 10, 370, 580, clBlack, $FF, 0.0); end; |
複数ページ文書
ページを追加しながら同じ描画パターンを繰り返すと、レポートや請求書を生成できます。
|
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 |
procedure DrawTriangle(Pdf: TPdf); begin // Start path at first vertex Pdf.CreatePath(200, 300, fmWinding, clBlue, $80, True, clNavy, $FF, 2.0); // Draw lines to other vertices Pdf.LineTo(300, 300); Pdf.LineTo(250, 400); // Close the path Pdf.ClosePath; // Add to page Pdf.AddPath; end; procedure DrawCurve(Pdf: TPdf); begin Pdf.CreatePath(100, 200, fmNone, clBlack, $FF, True, clRed, $FF, 2.0); Pdf.MoveTo(100, 200); // Bezier curve with control points Pdf.BezierTo( 150, 250, // Control point 1 200, 250, // Control point 2 250, 200 // End point ); Pdf.AddPath; end; |
実装時の注意点
- 座標系と単位を統一し、ページサイズごとの余白を定数化する。
- フォントが対象環境で利用できるか、または埋め込みが必要かを確認する。
- 例外発生時にも文書ハンドルや一時ファイルを確実に解放する。
- 生成後の PDF を複数のビューアで開き、表示、印刷、テキスト選択を確認する。
まとめ
PDFium VCL を使うと、Delphi からコードだけで PDF 文書を生成できます。ページ、テキスト、図形、画像の責務を分けて実装しておくと、帳票や自動レポートにも拡張しやすくなります。