如何將圖片轉換為 PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

將圖片轉換為PDF是一個有用的過程,可以將多個圖像文件結合在一起 (例如 JPG、PNG 或 TIFF) 整合成單一 PDF 文件。這通常用於創建數字作品集、演示文稿或報告,使將圖像集合以更有條理和通用可讀的格式共享和存儲變得更容易。

IronPDF 允許您將單個或多個圖像轉換為具有獨特性的 PDF 圖片位置和行為這些行為包括適應頁面、在頁面中居中和裁剪頁面。此外,您還可以添加 文本和HTML頁眉和頁腳, 添加浮水印,設置自訂頁面大小,並包含背景和前景覆蓋。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
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")
VB   C#

輸出 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")
VB   C#

輸出 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")
VB   C#

影像行為比較

將圖片置於頁面的左上方
將圖片放在頁面的右上角
將圖片置於頁面中央
將圖像調整為適合頁面,同時保持長寬比
將圖像放置在頁面左下角
將圖像放置在頁面的右下角
拉伸圖像以適應頁面
裁剪頁面以適合圖片

套用渲染選項

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")
VB   C#

輸出 PDF

如果您想將 PDF 文件轉換或光柵化為圖像,請參考我們的 如何將 PDF 光柵化為圖像 文章。

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。