技术文章

使用 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 工作流程。