مقالة تقنية

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

· برمجة PDF

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

نظرة عامة

يقوم هذا العرض التوضيحي بتحويل صفحات PDF إلى صور JPEG بإعدادات DPI وجودة قابلة للتخصيص واختيار الصفحات. إنه مثالي لإنشاء الصور المصغرة أو إنشاء أرشيفات تعتمد على الصور أو إعداد ملفات PDF للعرض على الويب.

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

  • DPI قابل للتخصيص. – التحكم في دقة الإخراج (72-600 DPI).
  • إعدادات الجودة. – ضبط جودة ضغط JPEG.
  • Page Selection – تحويل جميع الصفحات أو نطاقات صفحات محددة.
  • الدليل الإخراجي. – اختر مكان حفظ الصور المحولة.
  • تتبع التقدم. – ملاحظات بصرية أثناء التحويل.
  • دعم الإلغاء. – قم بإلغاء التحويلات الطويلة في أي وقت.

متطلبات ملف 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
procedure ConvertPdfToImages;
var
  Pdf: TPdf;
  I: Integer;
  Bitmap: TBitmap;
  JpegImage: TJpegImage;
  Dpi: Integer;
begin
  Dpi := 150; // 150 DPI is good for screen viewing
  
  Pdf := TPdf.Create(nil);
  try
    Pdf.FileName := 'document.pdf';
    Pdf.Active := True;
    
    for I := 1 to Pdf.PageCount do
    begin
      Pdf.PageNumber := I;
      
      // Calculate size based on DPI
      // PDF default is 72 DPI, so scale accordingly
      Bitmap := Pdf.RenderPage(
        0, 0,
        Round(Pdf.PageWidth * Dpi / 72),
        Round(Pdf.PageHeight * Dpi / 72),
        ro0,      // No rotation
        [],       // Default render options
        clWhite   // Background color
      );
      
      try
        // Convert to JPEG
        JpegImage := TJpegImage.Create;
        try
          JpegImage.Assign(Bitmap);
          JpegImage.CompressionQuality := 85;
          JpegImage.SaveToFile(Format('page_%03d.jpg', [I]));
        finally
          JpegImage.Free;
        end;
      finally
        Bitmap.Free;
      end;
    end;
    
  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
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
101
102
procedure TFormMain.ButtonConvertClick(Sender: TObject);
var
  I: Integer;
  OutDir, BaseName, FileName: string;
  JpegImage: TJpegImage;
  Bitmap: TBitmap;
  Dpi, Quality: Integer;
  Pages: TIntArray;
  PageDigits: Integer;
  UseAllPages: Boolean;
begin
  ProgressBar.Position := 0;
  FCancelRequested := False;
  Screen.Cursor := crHourGlass;
  
  try
    try
      if not FileExists(EditPdfFile.Text) then
        raise Exception.Create('PDF file does not exist.');
        
      // Parse settings
      Dpi := StrToIntDef(Trim(EditDPI.Text), 150);
      if Dpi <= 0 then Dpi := 150;
      if Dpi > 600 then Dpi := 600;
      
      Quality := StrToIntDef(Trim(EditQuality.Text), 85);
      if Quality < 1 then Quality := 1;
      if Quality > 100 then Quality := 100;
      
      // Load PDF
      Pdf.FileName := EditPdfFile.Text;
      Pdf.Active := True;
      
      // Parse page ranges
      UseAllPages := Trim(EditPageRange.Text) = '';
      if UseAllPages then
      begin
        SetLength(Pages, Pdf.PageCount);
        for I := 1 to Pdf.PageCount do
          Pages[I - 1] := I;
      end
      else
      begin
        if not ParsePageRanges(EditPageRange.Text, Pdf.PageCount, Pages) then
          raise Exception.Create('Invalid page range.');
      end;
      
      ProgressBar.Max := Length(Pages);
      PageDigits := Length(IntToStr(Pdf.PageCount));
      
      // Setup output
      OutDir := EnsureOutputDir(Pdf.FileName, EditOutputDir.Text);
      BaseName := ChangeFileExt(ExtractFileName(Pdf.FileName), '');
      
      JpegImage := TJpegImage.Create;
      try
        for I := 0 to High(Pages) do
        begin
          if FCancelRequested then
            Break;
            
          Pdf.PageNumber := Pages[I];
          
          // Render page to bitmap
          Bitmap := Pdf.RenderPage(
            0, 0,
            Round(Pdf.PageWidth * Dpi / 72),
            Round(Pdf.PageHeight * Dpi / 72),
            ro0, [], clWhite
          );
          
          try
            // Save as JPEG
            FileName := Format('%s\%s_%.*d.jpg',
              [OutDir, BaseName, PageDigits, Pages[I]]);
              
            JpegImage.Assign(Bitmap);
            JpegImage.CompressionQuality := Quality;
            JpegImage.SaveToFile(FileName);
          finally
            Bitmap.Free;
          end;
          
          ProgressBar.Position := I + 1;
          Application.ProcessMessages;
        end;
      finally
        JpegImage.Free;
      end;
      
    except
      on E: Exception do
        Application.MessageBox(
          PChar('Conversion failed: ' + E.Message),
          'Error', MB_ICONERROR or MB_OK);
    end;
    
  finally
    Screen.Cursor := crDefault;
    Pdf.Active := False;
  end;
end;

فهم RenderPage.

الـ RenderPage الطريقة هي المفتاح لتحويل ملفات 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
// Render to a new bitmap
function TPdf.RenderPage(
  Left, Top, Width, Height: Integer;
  Rotation: TRotation = ro0;
  Options: TRenderOptions = [];
  Color: TColor = clWhite
): TBitmap;
 
// Render to an existing bitmap
procedure TPdf.RenderPage(
  Bitmap: TBitmap;
  Left, Top, Width, Height: Integer;
  Rotation: TRotation = ro0;
  Options: TRenderOptions = [];
  Color: TColor = clWhite
);
 
// Render directly to a device context (for printing)
procedure TPdf.RenderPage(
  DeviceContext: HDC;
  Left, Top, Width, Height: Integer;
  Rotation: TRotation = ro0;
  Options: TRenderOptions = []
);

خيارات العرض.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type
  TRenderOption = (
    reAnnotations,       // Include annotations in output
    reLcd,               // Optimize for LCD displays
    reNoNativeText,      // Don't use system text rendering
    reGrayscale,         // Output in grayscale
    reLimitCache,        // Limit memory cache
    reHalftone,          // Better image downscaling
    rePrinting,          // Optimize for printing
    reNoSmoothText,      // Disable text anti-aliasing
    reNoSmoothImage,     // Disable image anti-aliasing
    reNoSmoothPath       // Disable path anti-aliasing
  );
 
// Example: High-quality output with annotations
Bitmap := Pdf.RenderPage(0, 0, Width, Height, ro0,
  [reAnnotations, reLcd], clWhite);
  
// Example: Fast, grayscale thumbnail
Bitmap := Pdf.RenderPage(0, 0, 200, 280, ro0,
  [reGrayscale, reNoSmoothText, reNoSmoothImage], clWhite);

إرشادات حول DPI والجودة.

Use Case Recommended DPI JPEG Quality
Thumbnails 72 60-70
Screen viewing 96-150 80-85
High-quality display 200-300 85-95
Printing 300-600 90-100

تحليل نطاقات الصفحات.

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
function TFormMain.ParsePageRanges(const S: string; MaxPage: Integer;
  out Pages: TIntArray): Boolean;
var
  List: TStringList;
  I, A, B, J: Integer;
  Part: string;
  DashPos: Integer;
  Used: array of Boolean;
  Count: Integer;
begin
  Result := False;
  SetLength(Pages, 0);
  
  if Trim(S) = '' then
  begin
    Result := True;
    Exit;
  end;
  
  SetLength(Used, MaxPage + 1);
  for I := 0 to High(Used) do
    Used[I] := False;
    
  List := TStringList.Create;
  try
    List.Delimiter := ',';
    List.DelimitedText := StringReplace(Trim(S), ' ', '', [rfReplaceAll]);
    
    for I := 0 to List.Count - 1 do
    begin
      Part := Trim(List[I]);
      DashPos := Pos('-', Part);
      
      if DashPos > 0 then
      begin
        // Range: "1-5"
        A := StrToIntDef(Trim(Copy(Part, 1, DashPos - 1)), 0);
        B := StrToIntDef(Trim(Copy(Part, DashPos + 1, Length(Part))), 0);
        
        if (A < 1) or (B < A) or (B > MaxPage) then
          Exit;
          
        for J := A to B do
          Used[J] := True;
      end
      else
      begin
        // Single page
        A := StrToIntDef(Part, 0);
        if (A < 1) or (A > MaxPage) then
          Exit;
        Used[A] := True;
      end;
    end;
  finally
    List.Free;
  end;
  
  // Build result array
  Count := 0;
  for I := 1 to MaxPage do
    if Used[I] then Inc(Count);
    
  SetLength(Pages, Count);
  J := 0;
  for I := 1 to MaxPage do
    if Used[I] then
    begin
      Pages[J] := I;
      Inc(J);
    end;
    
  Result := True;
end;

إنشاء صور مصغرة.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function CreateThumbnail(Pdf: TPdf; PageNumber: Integer;
  MaxWidth, MaxHeight: Integer): TBitmap;
var
  PageWidth, PageHeight: Double;
  Scale: Double;
  ThumbWidth, ThumbHeight: Integer;
begin
  Pdf.PageNumber := PageNumber;
  
  PageWidth := Pdf.PageWidth;
  PageHeight := Pdf.PageHeight;
  
  // Calculate scale to fit in max dimensions
  Scale := Min(MaxWidth / PageWidth, MaxHeight / PageHeight);
  
  ThumbWidth := Round(PageWidth * Scale);
  ThumbHeight := Round(PageHeight * Scale);
  
  Result := Pdf.RenderPage(0, 0, ThumbWidth, ThumbHeight,
    ro0, [reGrayscale], clWhite);
end;

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

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

الخلاصة.

عرض توضيحي لتحويل PDF إلى JPG يوضح مدى سهولة تحويل صفحات PDF إلى صور عالية الجودة باستخدام PDFium VCL. RenderPage الطريقة تمنحك تحكمًا كاملاً في الدقة والجودة وخيارات العرض.

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

ابدأ تحويل ملفات PDF إلى صور اليوم. مع مكون PDFium VCL. اليوم.