技術文章

在 Delphi 中使用 PDFium VCL 將影像轉換為 PDF 文件

· PDF 程式設計

將影像轉換為PDF是一種常見的需求,用於文件管理、歸檔和共享。 影像轉PDF 該演示程式展示瞭如何使用PDFium VCL從一組影像建立多頁PDF文件。

概述

此演示程式允許使用者選擇多個影像,預覽它們,如果需要可以重新排序,並將它們轉換為單個PDF文件。每個影像都將成為PDF中的一頁,並進行適當的縮放和居中。

主要特性

  • 多影像支援 – 一次新增多個影像
  • 格式支援 – 支援JPEG、PNG、BMP、GIF和TIFF影像。
  • 影像預覽 – 在轉換前預覽影像
  • 自動頁面大小 – 根據影像方向選擇縱向或橫向
  • 智慧縮放 – 影像按比例縮放以適應頁面,並留有邊距
  • 影像資訊 – 在每頁上顯示原始大小和縮放比例因子

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 系統目錄。

從影像建立 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 方法:

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 報告中包含截圖和圖表。

結論。

Image to PDF 演示版展示了 PDFium VCL 如何輕鬆建立專業的 PDF 文件,它具有自動頁面大小調整、智慧縮放以及對多種影像格式的支援,可用於構建強大的影像到 PDF 轉換工具。

好的。 AddPicture 該方法處理將影像嵌入到 PDF 頁面中的複雜性,而您則專注於應用程式的使用者介面和工作流程。

試用 PDFium VCL 元件。 從 loslab.com 開始,立即建立 PDF 文件。