技術記事

PDFium VCL を利用して複数の PDFファイルを 1 つのドキュメントに結合する

· PDFプログラミング

複数の PDF ドキュメントを 1 つのファイルに結合することは、ドキュメント管理ワークフローでよく必要になります。 PDF の結合 Merge PDF デモでは、Delphi の PDFium VCL を使用して任意の数の PDF ファイルを 1 つのファイルへ結合する方法を示します。

概要

このデモは、複数の PDF ファイルを選択し、順序を並べ替え、1 つの出力 PDF ファイルへ結合するためのシンプルな画面を提供します。元ドキュメント内のテキスト、画像、書式などの内容は維持されます。

主な機能

  • 多ファイル選択 – 複数の PDF ファイルを一度に追加します
  • ファイルの並べ替え – 結合順序の中でファイルを上または下へ移動します
  • ファイルの削除 – リストから不要なファイルを削除します
  • 印刷進捗状況 – 結合処理中の進捗を視覚的に表示します
  • 内容の保持 – すべてのページを元の品質のまま結合します

PDFium DLL の要件

PDFium VCL アプリケーションを実行する前に、PDFium DLL ファイルがインストールされていることを確認してください。

  • pdfium32.dll / pdfium64.dll – 標準バージョン(約 5-6 MB)
  • pdfium32v8.dll / pdfium64v8.dll – V8 JavaScript エンジン付き(約 23-27 MB)

インストール: 実行 PDFiumVCL\DLLs\CopyDlls.bat 管理者権限で、DLLファイルをWindowsシステムディレクトリに自動的にコピーします。

結合処理

中心となる結合機能では、次のメソッドを使用します。 ImportPages method:

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
38
39
40
41
42
43
44
45
46
47
48
49
procedure TFormMain.ButtonMergeClick(Sender: TObject);
var
  Page, I: Integer;
begin
  ProgressBar.Position := 0;
  ProgressBar.Enabled := True;
  Screen.Cursor := crHourGlass;
  
  try
    // Create new empty document for merged result
    PdfNew.CreateDocument;
    
    Page := 1;
    ProgressBar.Max := ListBox.Items.Count;
    
    // Process each source PDF
    for I := 0 to ListBox.Items.Count - 1 do
    begin
      // Load source PDF
      Pdf.FileName := ListBox.Items[I];
      Pdf.Active := True;
      
      // Import all pages from source
      PdfNew.ImportPages(Pdf,
        IntToStr(1) + '-' + IntToStr(Pdf.PageCount), Page);
        
      // Track page position for next document
      Inc(Page, Pdf.PageCount);
      
      // Close source document
      Pdf.Active := False;
      
      ProgressBar.Position := I + 1;
    end;
    
    // Save merged document
    PdfNew.SaveAs(ExtractFilePath(Application.ExeName) + 'Merged.pdf');
    PdfNew.Active := False;
    
    MessageDlg('Merge completed', mtInformation, [mbOK], 0);
    
  finally
    Screen.Cursor := crDefault;
    ProgressBar.Position := 0;
    ProgressBar.Enabled := False;
    Pdf.Active := False;
    PdfNew.Active := False;
  end;
end;

ImportPages の理解

The ImportPages method は PDF の結合における重要な要素です。

1
2
3
4
5
function TPdf.ImportPages(
  Pdf: TPdf;           // Source PDF document
  const Range: string; // Page range (e.g., "1-5", "1,3,5", "1-")
  PageNumber: Integer  // Insert position in destination
): Boolean;

対応するページ範囲の形式:

  • "1-5" – 1 ページから 5 ページ
  • "1,3,5" – 1 ページ、3 ページ、および 5 ページ
  • "1-" – 1 ページから最後まで
  • "-5" – 最初のページから 5 ページ

ファイルをリストに追加する

1
2
3
4
5
6
7
8
9
10
11
12
13
procedure TFormMain.ButtonAddFilesClick(Sender: TObject);
var
  I: Integer;
begin
  if OpenDialog.Execute then
  begin
    for I := 0 to OpenDialog.Files.Count - 1 do
      ListBox.AddItem(OpenDialog.Files[I], nil);
      
    SelectItem(ListBox.Items.Count - 1);
    UpdateButtons;
  end;
end;

ファイルの並べ替え

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
procedure TFormMain.ButtonMoveUpClick(Sender: TObject);
var
  ItemIndex: Integer;
begin
  ItemIndex := ListBox.ItemIndex;
  ListBox.Items.Move(ItemIndex, ItemIndex - 1);
  SelectItem(ItemIndex - 1);
  UpdateButtons;
end;
 
procedure TFormMain.ButtonMoveDownClick(Sender: TObject);
var
  ItemIndex: Integer;
begin
  ItemIndex := ListBox.ItemIndex;
  ListBox.Items.Move(ItemIndex, ItemIndex + 1);
  SelectItem(ItemIndex + 1);
  UpdateButtons;
end;

ファイルの削除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
procedure TFormMain.ButtonDeleteClick(Sender: TObject);
var
  ItemIndex, ItemCount: Integer;
begin
  ItemIndex := ListBox.ItemIndex;
  
  if MessageDlg('Delete ' + ListBox.Items[ItemIndex] + '?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    ListBox.Items.Delete(ItemIndex);
    ItemCount := ListBox.Items.Count;
    
    // Select appropriate item after deletion
    if ItemIndex < ItemCount then
      SelectItem(ItemIndex)
    else if ItemCount > 0 then
      SelectItem(ItemIndex - 1);
      
    UpdateButtons;
  end;
end;

UI 状態の更新

1
2
3
4
5
6
7
8
9
10
11
12
13
procedure TFormMain.UpdateButtons;
var
  ItemIndex, ItemCount: Integer;
begin
  ItemIndex := ListBox.ItemIndex;
  ItemCount := ListBox.Items.Count;
  
  ButtonAddFiles.Enabled := True;
  ButtonDelete.Enabled := ItemIndex <> -1;
  ButtonMoveUp.Enabled := (ItemIndex <> -1) and (ItemIndex > 0);
  ButtonMoveDown.Enabled := (ItemIndex <> -1) and (ItemIndex < ItemCount - 1);
  ButtonMerge.Enabled := ItemCount > 1; // Need at least 2 files to merge
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
31
32
33
34
35
36
procedure MergeSpecificPages;
var
  PdfSource, PdfDest: TPdf;
begin
  PdfSource := TPdf.Create(nil);
  PdfDest := TPdf.Create(nil);
  try
    PdfDest.CreateDocument;
    
    // Import pages 1-3 from first document
    PdfSource.FileName := 'document1.pdf';
    PdfSource.Active := True;
    PdfDest.ImportPages(PdfSource, '1-3', 1);
    PdfSource.Active := False;
    
    // Import only page 5 from second document
    PdfSource.FileName := 'document2.pdf';
    PdfSource.Active := True;
    PdfDest.ImportPages(PdfSource, '5', 4);
    PdfSource.Active := False;
    
    // Import last 2 pages from third document
    PdfSource.FileName := 'document3.pdf';
    PdfSource.Active := True;
    PdfDest.ImportPages(PdfSource,
      IntToStr(PdfSource.PageCount - 1) + '-' + IntToStr(PdfSource.PageCount), 5);
    PdfSource.Active := False;
    
    PdfDest.SaveAs('merged_custom.pdf');
    
  finally
    PdfDest.Active := False;
    PdfSource.Free;
    PdfDest.Free;
  end;
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
procedure CopyViewerPreferences;
var
  PdfSource, PdfDest: TPdf;
begin
  PdfSource := TPdf.Create(nil);
  PdfDest := TPdf.Create(nil);
  try
    PdfSource.FileName := 'template.pdf';
    PdfSource.Active := True;
    
    PdfDest.FileName := 'output.pdf';
    PdfDest.Active := True;
    
    // Copy viewer preferences (zoom, page layout, etc.)
    PdfDest.ImportPreferences(PdfSource);
    
    PdfDest.SaveAs('output.pdf');
    
  finally
    PdfSource.Active := False;
    PdfDest.Active := False;
    PdfSource.Free;
    PdfDest.Free;
  end;
end;

利用例

  • 报告编译 複数のレポートセクションを1つのドキュメントに結合します。
  • 請求書一括処理 毎日の請求書を月間のバッチに統合します。
  • ドキュメントアセンブリ テンプレートとコンテンツから完全なドキュメントを構築します。
  • アーカイブ作成 関連ドキュメントをまとめてアーカイブします。

結論

Merge PDFのデモでは、PDFium VCLを利用してPDFドキュメントを結合する際の簡単な手順を示しています。 ImportPages このメソッドは、ドキュメントの構造とコンテンツを維持する複雑な処理をすべて処理します。

シンプルなドキュメント結合ツールを作成する場合でも、複雑なドキュメントアセンブリシステムを作成する場合でも、PDFium VCL は必要なツールを提供します。

から始めましょう。 PDFium VCLコンポーネント を loslab.com から入手して、PDF ワークフローを効率化します。