技術文章

使用 PDFium VCL 將多個 PDF 檔案合併為一個文件

· PDF 程式設計

將多個PDF文件合併成一個檔案是文件管理工作流程中的常見需求。 合併PDF 演示程式展示瞭如何使用Delphi中的PDFium VCL將任意數量的PDF檔案合併為一個檔案。

概述

此演示程式提供了一個簡單的介面,用於選擇多個PDF檔案、重新排序它們,並將它們合併成一個輸出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 方法:

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。

好的。 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;

用例

  • 報告編譯. 將多個報告部分合併為一個文件。
  • 發票批次處理。 將每日發票合併為每月批次。
  • 文件組裝。 從模板和內容構建完整的文件。
  • 歸檔建立。 將相關文件合併用於歸檔。

結論。

Merge PDF 演示展示瞭如何使用 PDFium VCL 輕鬆合併 PDF 文件。 ImportPages 該方法處理了保留文件結構和內容的全部複雜性。

無論您是構建一個簡單的文件合併工具,還是一個複雜的文件組裝系統,PDFium VCL 都提供您所需的功能。

立即開始使用 loslab.com 上的產品,簡化您的 PDF 工作流程。 PDFium VCL 元件 from loslab.com 並且簡化您的 PDF 工作流程。