Technical Article

Meerdere PDF-bestanden samenvoegen tot één document met PDFium VCL

· PDF-programmeren

Leer hoe u meerdere PDF-bestanden samenvoegt tot één document met PDFium VCL en waar u paginavolgorde en foutafhandeling controleert.

Dit artikel is bedoeld voor ontwikkelaars die met pdf-programmeren werken. Productnamen, API-namen, bestandsnamen en codefragmenten zijn bewust ongewijzigd gehouden, zodat de voorbeelden direct met de oorspronkelijke documentatie en broncode te vergelijken zijn.

Overzicht

De pagina beschrijft het probleemgebied, de relevante implementatiekeuzes en de controles die belangrijk zijn voordat de oplossing in een echte toepassing wordt gebruikt.

Codevoorbeeld

Het onderstaande codefragment is ongewijzigd uit de Engelse bron overgenomen om identifiers, API-aanroepen en syntaxis exact te behouden.

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;
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
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;
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;

Praktische aandachtspunten

  • Controleer invoerbestanden en foutpaden expliciet voordat u de routine in productie gebruikt.
  • Houd paginalay-out, lettertypen en coördinaten reproduceerbaar, vooral bij server-side verwerking.
  • Test het resultaat in meer dan één PDF-viewer wanneer rendering, annotaties of interactieve elementen belangrijk zijn.
  • Laat componentlevensduur en bestandshandles altijd via try/finally-achtige patronen opruimen.