مقالة تقنية

تحويل الصور إلى مستندات PDF باستخدام PDFium VCL في دلفي

· برمجة PDF

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

نظرة عامة

يتيح هذا العرض التوضيحي للمستخدمين تحديد صور متعددة، وعرضها مسبقًا، وإعادة ترتيبها إذا لزم الأمر، وتحويلها إلى مستند PDF واحد. تصبح كل صورة صفحة في ملف PDF، مع تغيير حجمها وتوسيطها بشكل صحيح.

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

  • دعم الصور المتعددة. - إضافة صور متعددة في وقت واحد.
  • دعم التنسيقات. - صور بتنسيقات JPEG وPNG وBMP وGIF وTIFF.
  • معاينة الصور. - معاينة الصور قبل التحويل.
  • تحديد حجم الصفحة تلقائيًا. - اتجاه الصفحة (عمودي أو أفقي) بناءً على اتجاه الصورة.
  • تغيير حجم ذكي. - يتم تغيير حجم الصور لتناسب الصفحة مع وجود هوامش.
  • معلومات الصورة. - يتم عرض الحجم الأصلي وعامل القياس على كل صفحة.

متطلبات ملف 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.

إنشاء ملفات PDF من الصور.

عملية التحويل الأساسية:

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
procedure TFormMain.CreatePDFFromImages;
var
  I: Integer;
  PageWidth, PageHeight: Double;
  ImageWidth, ImageHeight: Double;
  ScaleX, ScaleY, Scale: Double;
  ImageSize: TSize;
  X, Y: Double;
  Picture: TPicture;
begin
  if FImageList.Count = 0 then
    Exit;
    
  ProgressBar.Visible := True;
  ProgressBar.Max := FImageList.Count;
  ProgressBar.Position := 0;
  
  try
    // Create new PDF document
    Pdf.CreateDocument;
    Pdf.Active := True;
    
    for I := 0 to FImageList.Count - 1 do
    begin
      try
        // Load the image
        Picture := TPicture.Create;
        try
          Picture.LoadFromFile(FImageList[I]);
          
          // Get image dimensions
          ImageSize.cx := Picture.Width;
          ImageSize.cy := Picture.Height;
          
          // Set page orientation based on image
          if ImageSize.cx > ImageSize.cy then
          begin
            // Landscape
            PageWidth := 842;  // A4 landscape
            PageHeight := 595;
          end
          else
          begin
            // Portrait
            PageWidth := 595;  // A4 portrait
            PageHeight := 842;
          end;
          
          // Create page
          Pdf.AddPage(I + 1, PageWidth, PageHeight);
          Pdf.PageNumber := I + 1;
          
          // Calculate scaling
          ImageWidth := ImageSize.cx;
          ImageHeight := ImageSize.cy;
          
          ScaleX := (PageWidth - 80) / ImageWidth;   // 40pt margin each side
          ScaleY := (PageHeight - 120) / ImageHeight; // 40pt + space for text
          Scale := Min(ScaleX, ScaleY);
          
          ImageWidth := ImageWidth * Scale;
          ImageHeight := ImageHeight * Scale;
          
          // Center on page
          X := (PageWidth - ImageWidth) / 2;
          Y := (PageHeight - ImageHeight) / 2;
          
          // Add image to PDF
          Pdf.AddPicture(Picture, X, Y, ImageWidth, ImageHeight);
          
          // Add image information
          Pdf.AddText(Format('File: %s', [ExtractFileName(FImageList[I])]),
            'Arial', 10, X, Y + ImageHeight + 10, clBlack, $FF, 0.0);
          Pdf.AddText(Format('Original: %dx%d px, Scale: %.1f%%',
            [ImageSize.cx, ImageSize.cy, Scale * 100]),
            'Arial', 8, X, Y + ImageHeight + 25, clGray, $FF, 0.0);
            
        finally
          Picture.Free;
        end;
        
        ProgressBar.Position := I + 1;
        Application.ProcessMessages;
        
      except
        on E: Exception do
          // Continue with next image on error
          LabelStatus.Caption := 'Error: ' + E.Message;
      end;
    end;
    
    // Save the PDF
    Pdf.SaveAs(SaveDialog.FileName);
    ShowMessage(Format('PDF created with %d pages!', [FImageList.Count]));
    
  finally
    ProgressBar.Visible := False;
    Pdf.Active := False;
  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
26
27
28
29
30
31
32
33
34
35
36
37
38
procedure TFormMain.BtnAddImagesClick(Sender: TObject);
var
  I: Integer;
  ListItem: TListItem;
  FileSize: Int64;
  FileSizeStr: string;
begin
  if OpenPictureDialog.Execute then
  begin
    for I := 0 to OpenPictureDialog.Files.Count - 1 do
    begin
      FImageList.Add(OpenPictureDialog.Files[I]);
      
      // Add to ListView
      ListItem := ListView.Items.Add;
      ListItem.Caption := ExtractFileName(OpenPictureDialog.Files[I]);
      ListItem.SubItems.Add(OpenPictureDialog.Files[I]); // Full path
      
      // Get file size
      try
        FileSize := GetFileSizeHelper(OpenPictureDialog.Files[I]);
        if FileSize >= 1024 * 1024 then
          FileSizeStr := Format('%.1f MB', [FileSize / (1024 * 1024)])
        else if FileSize >= 1024 then
          FileSizeStr := Format('%.1f KB', [FileSize / 1024])
        else
          FileSizeStr := Format('%d bytes', [FileSize]);
      except
        FileSizeStr := 'Unknown';
      end;
      ListItem.SubItems.Add(FileSizeStr);
    end;
    
    UpdateUI;
    LabelStatus.Caption := Format('Added %d image(s). Total: %d images',
      [OpenPictureDialog.Files.Count, FImageList.Count]);
  end;
end;

استخدام طريقة AddPicture.

الـ AddPicture هذه الطريقة تقبل كائنًا، والذي يدعم تنسيقات صور مختلفة: TPicture ، والذي يدعم تنسيقات صور مختلفة:

1
2
3
4
5
// Add picture with automatic sizing
procedure TPdf.AddPicture(Picture: TPicture; X, Y: Double);
 
// Add picture with specific size
procedure TPdf.AddPicture(Picture: TPicture; X, Y, Width, Height: Double);

إضافة صور JPEG مباشرة.

بالنسبة لصور JPEG، يمكنك استخدام الطريقة المحسنة. AddJpegImage 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
function AddJpegFromFile(const FileName: string): Boolean;
var
  Pdf: TPdf;
  FileStream: TFileStream;
begin
  Pdf := TPdf.Create(nil);
  try
    Pdf.CreateDocument;
    Pdf.Active := True;
    Pdf.AddPage(1, 595, 842);
    Pdf.PageNumber := 1;
    
    FileStream := TFileStream.Create(FileName, fmOpenRead);
    try
      // Add JPEG directly - more efficient than loading as TPicture
      Result := Pdf.AddJpegImage(FileStream, 50, 50, 495, 742);
    finally
      FileStream.Free;
    end;
    
    Pdf.SaveAs('output.pdf');
    
  finally
    Pdf.Active := False;
    Pdf.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
26
27
procedure TFormMain.ListViewSelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  if Selected and (Item <> nil) then
  begin
    LoadImagePreview(Item.SubItems[0]); // Full path
  end;
end;
 
procedure TFormMain.LoadImagePreview(const FileName: string);
begin
  try
    if FileExists(FileName) then
    begin
      ImagePreview.Picture.LoadFromFile(FileName);
      ImagePreview.Proportional := True;
      ImagePreview.Stretch := True;
      LabelStatus.Caption := 'Preview: ' + ExtractFileName(FileName);
    end;
  except
    on E: Exception do
    begin
      ImagePreview.Picture.Assign(nil);
      LabelStatus.Caption := 'Error loading preview: ' + E.Message;
    end;
  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
26
27
procedure TFormMain.BtnRemoveSelectedClick(Sender: TObject);
var
  I, RemovedCount: Integer;
begin
  RemovedCount := 0;
  
  if ListView.SelCount > 0 then
  begin
    // Remove from back to front to avoid index issues
    for I := ListView.Items.Count - 1 downto 0 do
    begin
      if ListView.Items[I].Selected then
      begin
        FImageList.Delete(I);
        ListView.Items.Delete(I);
        Inc(RemovedCount);
      end;
    end;
    
    if (FImageList.Count = 0) or (ListView.Selected = nil) then
      ImagePreview.Picture.Assign(nil);
      
    UpdateUI;
    LabelStatus.Caption := Format('Removed %d item(s). Total: %d images',
      [RemovedCount, FImageList.Count]);
  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
26
27
28
29
procedure TFormMain.FormCreate(Sender: TObject);
begin
  FImageList := TStringList.Create;
  
  // Setup ListView
  ListView.ViewStyle := vsReport;
  ListView.Columns.Add.Caption := 'File Name';
  ListView.Columns.Add.Caption := 'Path';
  ListView.Columns.Add.Caption := 'Size';
  ListView.Columns[0].Width := 200;
  ListView.Columns[1].Width := 300;
  ListView.Columns[2].Width := 100;
  ListView.RowSelect := True;
  ListView.MultiSelect := True;
  
  // Setup dialogs
  OpenPictureDialog.Filter :=
    'Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff;*.tif|' +
    'JPEG Files|*.jpg;*.jpeg|' +
    'PNG Files|*.png|' +
    'Bitmap Files|*.bmp|' +
    'All Files|*.*';
  OpenPictureDialog.Options := [ofAllowMultiSelect, ofPathMustExist, ofFileMustExist];
  
  SaveDialog.Filter := 'PDF Files|*.pdf|All Files|*.*';
  SaveDialog.DefaultExt := 'pdf';
  
  UpdateUI;
end;

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

  • ألبومات الصور. – إنشاء ألبومات PDF من الصور الرقمية.
  • مسح المستندات. – دمج صور الصفحات الممسوحة ضوئيًا في ملف PDF واحد.
  • إنشاء ملفات أعمال. قم بإنشاء ملفات PDF للمحافظ من صور الأعمال الفنية.
  • الأرشفة. قم بتحويل مجموعات الصور إلى ملفات PDF للتخزين طويل الأمد.
  • إنشاء التقارير. قم بتضمين لقطات الشاشة والرسوم البيانية في تقارير PDF.

الخلاصة.

يوضح عرض توضيحي لتحويل الصور إلى PDF كيف يتيح PDFium VCL إنشاء مستندات PDF احترافية بسهولة من الصور. بفضل أحجام الصفحات التلقائية، والقياس الذكي، والدعم لتنسيقات الصور المتعددة، يمكنك إنشاء أدوات تحويل صور قوية إلى PDF.

الـ AddPicture تتعامل الطريقة مع تعقيد تضمين الصور في صفحات PDF، بينما تركز على واجهة المستخدم وسير العمل في تطبيقك.

جرب مكون PDFium VCL. من loslab.com وابدأ في إنشاء ملفات PDF من الصور اليوم.