مقالة تقنية

دمج ملفات PDF متعددة في مستند واحد باستخدام PDFium VCL

· برمجة PDF

دمج مستندات PDF متعددة في ملف واحد هو متطلب شائع في سير عمل إدارة المستندات. دمج ملفات PDF يوضح هذا المثال كيفية دمج أي عدد من ملفات PDF في ملف واحد باستخدام PDFium VCL في Delphi.

نظرة عامة

يوفر هذا المثال واجهة بسيطة لاختيار ملفات PDF متعددة، وإعادة ترتيبها، ودمجها في ملف PDF واحد. إنه يحافظ على جميع المحتويات من المستندات المصدر، بما في ذلك النص والصور والتنسيق.

الميزات الرئيسية

  • تحديد ملفات متعددة - إضافة ملفات PDF متعددة في وقت واحد.
  • إعادة ترتيب الملفات - نقل الملفات لأعلى أو لأسفل في ترتيب الدمج.
  • إزالة الملفات. - إزالة الملفات غير المرغوب فيها من القائمة.
  • تتبع التقدم. - عرض مرئي للتقدم أثناء عملية الدمج.
  • الحفاظ على المحتوى. - جميع الصفحات مدمجة بجودة أصلية.

متطلبات ملف PDFium DLL.

قبل تشغيل أي تطبيق PDFium VCL، تأكد من تثبيت ملفات PDFium DLL:

  • pdfium32.dll / pdfium64.dll – الإصدارات القياسية (حوالي 5-6 ميجابايت).
  • pdfium32v8.dll / pdfium64v8.dll – مع محرك JavaScript V8 (حوالي 23-27 ميجابايت).

التثبيت: تشغيل 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.

الـ ImportPages الطريقة هي المفتاح لدمج ملفات 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;

تحديث حالة واجهة المستخدم.

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;

حالات الاستخدام.

  • تجميع التقارير. دمج أقسام متعددة من التقارير في مستند واحد.
  • تجميع الفواتير. دمج الفواتير اليومية في دفعات شهرية.
  • تجميع المستندات. إنشاء مستندات كاملة من القوالب والمحتوى.
  • إنشاء الأرشيف. دمج المستندات ذات الصلة للأرشفة.

الخلاصة.

عرض توضيحي لدمج ملفات PDF يوضح مدى سهولة دمج مستندات PDF باستخدام PDFium VCL. ImportPages الطريقة تتعامل مع كل تعقيدات الحفاظ على هيكل ومحتوى المستند.

سواء كنت تقوم ببناء أداة بسيطة لدمج المستندات أو نظام معقد لتجميع المستندات، فإن PDFium VCL يوفر الأدوات التي تحتاجها.

ابدأ مع مكون PDFium VCL. من loslab.com وقم بتبسيط سير عمل PDF الخاص بك.