如何將圖片轉換為 PDF
將圖片轉換為PDF是一個有用的過程,可以將多個圖像文件結合在一起 (例如 JPG、PNG 或 TIFF) 整合成單一 PDF 文件。這通常用於創建數字作品集、演示文稿或報告,使將圖像集合以更有條理和通用可讀的格式共享和存儲變得更容易。
IronPDF 允許您將單個或多個圖像轉換為具有獨特性的 PDF 圖片位置和行為這些行為包括適應頁面、在頁面中居中和裁剪頁面。此外,您還可以添加 文本和HTML頁眉和頁腳, 添加浮水印,設置自訂頁面大小,並包含背景和前景覆蓋。
如何將圖片轉換為 PDF
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronPDF 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。
Install-Package IronPdf
請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip
手動安裝到您的項目中
下載DLL將圖像轉換成 PDF 的範例
使用 ImageToPdfConverter 類中的 ImageToPdf
靜態方法將圖像轉換成 PDF 文件。此方法僅需提供圖像的文件路徑,便可將其轉換成具備預設圖像位置和行為的 PDF 文件。支持的圖像格式包括 .bmp, .jpeg, .jpg, .gif, .png, .svg, .tif, .tiff, .webp, .apng, .avif, .cur, .dib, .ico, .jfif, .jif, .jpe, .pjp 和 .pjpeg。
範例圖片
代碼
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image.cs
using IronPdf;
string imagePath = "meetOurTeam.jpg";
// Convert an image to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath);
// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf
Private imagePath As String = "meetOurTeam.jpg"
' Convert an image to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath)
' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
輸出 PDF
將圖像轉換為 PDF 示例
要將多個圖像轉換為 PDF 文件,您應該提供一個包含檔案路徑而不是單個檔案路徑的 IEnumerable 對象,如我們之前的示例所示。這將再次生成具有預設圖像佈局和行為的 PDF 文件。
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-multiple-images.cs
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
// Retrieve all JPG and JPEG image paths in the 'images' folder.
IEnumerable<String> imagePaths = Directory.EnumerateFiles("images").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));
// Convert images to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePaths);
// Export the PDF
pdf.SaveAs("imagesToPdf.pdf");
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
' Retrieve all JPG and JPEG image paths in the 'images' folder.
Private imagePaths As IEnumerable(Of String) = Directory.EnumerateFiles("images").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg"))
' Convert images to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePaths)
' Export the PDF
pdf.SaveAs("imagesToPdf.pdf")
輸出 PDF
圖片位置與行為
為了便於使用,我們提供了一系列的有用圖片位置和行為選項。例如,您可以將圖片置於頁面中心,或在保持其縱橫比的同時適應頁面大小。所有可用的圖片位置和行為如下:
- TopLeftCornerOfPage: 圖片置於頁面左上角。
- TopRightCornerOfPage: 圖片置於頁面右上角。
- CenteredOnPage: 圖片置於頁面中心。
- FitToPageAndMaintainAspectRatio: 圖片適應頁面,同時保持其原始縱橫比。
- BottomLeftCornerOfPage: 圖片置於頁面左下角。
- BottomRightCornerOfPage: 圖片置於頁面右下角。
- FitToPage: 圖片適應頁面。
- CropPage: 頁面調整以適應圖片。
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-image-behavior.cs
using IronPdf;
using IronPdf.Imaging;
string imagePath = "meetOurTeam.jpg";
// Convert an image to a PDF with image behavior of centered on page
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage);
// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf
Imports IronPdf.Imaging
Private imagePath As String = "meetOurTeam.jpg"
' Convert an image to a PDF with image behavior of centered on page
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage)
' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
影像行為比較
頁面的左上角 | 頁面的右上角 |
頁面居中 | 適應頁面並保持縱橫比 |
頁面左下角 | 頁面右下角 |
縮放至頁面 | 裁剪頁面 |
套用渲染選項
在 ImageToPdf
靜態方法下將各種類型的圖像轉換成 PDF 文件的關鍵是將圖像以 HTML img 標籤的形式導入,然後將 HTML 轉換成 PDF。這也是我們可以將 ChromePdfRenderOptions 物件作為 ImageToPdf
方法的第三個參數,來直接自訂渲染過程的原因。
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-rendering-options.cs
using IronPdf;
string imagePath = "meetOurTeam.jpg";
ChromePdfRenderOptions options = new ChromePdfRenderOptions()
{
HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
DrawDividerLine = true,
},
};
// Convert an image to a PDF with custom header
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, options: options);
// Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf");
Imports IronPdf
Private imagePath As String = "meetOurTeam.jpg"
Private options As New ChromePdfRenderOptions() With {
.HtmlHeader = New HtmlHeaderFooter() With {
.HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
.DrawDividerLine = True
}
}
' Convert an image to a PDF with custom header
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, options:= options)
' Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf")
輸出 PDF
如果您想將 PDF 文件轉換或光柵化為圖像,請參考我們的 如何將 PDF 光柵化為圖像 文章。