이미지 파일을 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 方法
The AddPicture method 接受一个 TPicture object,该对象支持各种图像格式:
|
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 보고서에 포함합니다.
결론
Image to PDF 데모는 PDFium VCL을 사용하여 이미지에서 전문적인 PDF 문서를 쉽게 만드는 방법을 보여줍니다. 자동 페이지 크기 조정, 지능형 크기 조정 및 다양한 이미지 형식 지원을 통해 강력한 이미지-PDF 변환 도구를 만들 수 있습니다.
The AddPicture 이 메서드는 이미지 파일을 PDF 페이지에 포함하는 복잡한 작업을 처리하므로, 사용자는 애플리케이션의 사용자 인터페이스 및 워크플로우에 집중할 수 있습니다.
PDFium VCL 컴포넌트를 사용해 보세요. loslab.com에서 PDF를 이미지에서 생성하는 작업을 시작하세요.