如何在C#中將圖像轉換為PDF
IronPDF使得C#開發者能夠使用ImageToPdfConverter.ImageToPdf方法,將單個或多個圖像(JPG、PNG、TIFF)轉換為PDF文件,並可自定義放置行為和渲染選項。
將圖像轉換為PDF是將多個圖像文件(如JPG、PNG或TIFF)合併到一個PDF文件中。 這個過程可以建立數位作品集、演示文稿或報告,使得更容易以有組織且通用可讀的格式分享和儲存圖像合集。
IronPDF將單個或多個圖像轉換為具有獨特圖像放置和行為的PDF。 這些行為包括適配頁面、中心放置以及裁剪頁面。 此外,您可以使用IronPDF新增文字和HTML表頭與表尾,應用水印,設置自定義頁面大小,以及包含背景和前景覆蓋。
快速入門:使用IronPDF將圖像轉換為PDF使用IronPDF的ImageToPdfConverter類將圖像轉換為PDF文件。 本範例演示瞭如何將圖像文件轉換為PDF,從而讓您將圖像轉換為PDF的功能整合到您的.NET C#應用程式中。 只需少量程式碼,您就可以將圖像轉換為PDF,以建立數字作品集或報告。
-
1Install IronPDF with NuGet Package Manager
-
2複製並運行這段程式碼片段。
IronPdf.ImageToPdfConverter.ImageToPdf("path/to/image.png").SaveAs("imageToPdf.pdf");IronPdf.ImageToPdfConverter.ImageToPdf("path/to/image.png").SaveAs("imageToPdf.pdf")IronPdf.ImageToPdfConverter.ImageToPdf("path/to/image.png").SaveAs("imageToPdf.pdf") -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronPDF,透過免費試用
最小工作流程 (5步)
- 下載用於圖像轉PDF的IronPDF C#程式庫
- 準備您要轉換的圖像
- 將圖像路徑提供給
ImageToPdf靜態方法 - 調整輸出PDF中的圖像放置和行為
- 使用IronPDF新增自定義文字和HTML表頭與表尾
如何將單個圖像轉換為PDF?
在ImageToPdf靜態方法將圖像轉換為PDF文件。 此方法僅需要圖像的文件路徑,並將其轉換為具有預設圖像放置和行為的PDF文件。 支持的圖像格式包括.bmp、.jpeg、.jpg、.gif、.png、.svg、.tif、.tiff、.webp、.apng、.avif、.cur、.dib、.ico、.jfif、.jif、.jpe、.pjp和.pjpeg。
在開始前,請確保您已經通過NuGet安裝IronPDF或下載Windows安裝程式。 此程式庫可在Windows、macOS和Linux平台上無縫運行,使其在各種開發環境中都具有多功能性。
支持哪些圖像格式?

IronPDF支持全面的圖像格式,包括像JPEG、PNG、BMP和GIF這樣的點陣格式,以及SVG圖形等向量格式。 該程式庫還處理標準和現代格式,如WebP和AVIF,確保相容於當代網頁應用程式。 如有特定需求,請使用圖標文件(.ico)和游標文件(.cur)。
如何編寫程式碼?
using IronPdf;
string imagePath = "meetOurTeam.jpg";
// Convert an image to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath);
// Export the PDF
pdf.SaveAs("imageToPdf.pdf");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")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文件。
在處理多個圖像時,請考慮可用的PDF壓縮選項,以避免犧牲質量的情況下優化文件大小。 這在建立大型作品集或相簿時尤為重要。
如何同時處理多個圖像?
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");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")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看起來如何?
如何控制圖像放置和行為?
IronPDF提供多種圖像放置和行為選項。 您可以將圖像置於頁面中心或調整到頁面尺寸,同時保持其縱橫比。 所有可用的圖像放置和行為選項有:
:圖像放置在頁面左上角。:圖像放置在頁面右上角。:圖像置於頁面中心。:圖像調整至適合頁面,保持其原始長寬比。:圖像放置在頁面左下角。:圖像放置在頁面右下角。:圖像適合頁面。:頁面調整以適應圖像。
這些放置選項與IronPDF的頁面方向和旋轉功能無縫協作,無論您的圖像尺寸如何,都能建立出專業文件。
如何在程式碼中應用圖像行為?
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");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")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")圖像行為間的視覺差異是什麼?
![]()
| ![]()
|
![]()
| ![]()
|
![]()
| ![]()
|
![]()
| ![]()
|
如何自定義PDF渲染選項?
ImageToPdf靜態方法通過將圖像導入為HTML <img>標籤後轉換為PDF來轉換圖像。 這允許您將ChromePdfRenderOptions物件作為第三個參數傳遞,以直接自定義渲染過程。
這個方法提供了存取IronPDF中所有強大渲染選項的途徑,包括JavaScript執行、CSS媒體型別和視窗配置。 您甚至可以整合網頁字體和圖標來提升您文件的視覺吸引力。
如何新增表頭和自定義樣式?
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");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")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")對於更複雜的表頭和表尾需求,請瀏覽我們關於HTML表頭和表尾的綜合指南。 您還可以為基於圖像的PDF新增頁碼以更好地組織它們。
定制的輸出看起來如何?
如何處理效能與大規模圖像合集?
在處理大規模圖像合集或高解析度圖像時,實施異步和多執行緒技術以改善效能。 對於企業應用程式,探索IronPDF的效能優化策略,以確保即使在大量作業情況下也能順利運行。
此外,在處理雲端服務中儲存的圖像時,查閱我們關於從Azure Blob Storage嵌入圖像的指南,以實現無縫雲端整合。
如何將PDF轉換回圖像?
如需將PDF文件轉換或光柵化為圖像,請參考我們如何將PDF光柵化為圖像的指南。
準備好看看您還能做什麼嗎? 在此查看我們的教程頁面:轉換PDF
常見問題
哪些圖片格式可以轉換為PDF?
IronPDF支持一系列圖片格式的PDF轉換,包括 .bmp、.jpeg、.jpg、.gif、.png、.svg、.tif、.tiff、.webp、.apng、.avif、.cur、.dib、.ico、.jfif、.jif、.jpe、.pjp 和 .pjpeg。這包括像JPEG、PNG、BMP 和 GIF的光柵格式,以及SVG圖形等向量格式。
如何在C#中將單張圖片轉換為PDF?
要使用IronPDF將單張圖片轉換為PDF,請使用ImageToPdfConverter類中的ImageToPdf靜態方法。只需提供圖片的文件路徑:IronPdf.ImageToPdfConverter.ImageToPdf("path/to/image.png").SaveAs("imageToPdf.pdf");。這個單行解決方案會以預設放置和行為將您的圖片轉換為PDF文件。
我可以將多張圖片轉換成一個PDF文件嗎?
可以,IronPDF允許您使用ImageToPdfConverter.ImageToPdf方法將多張圖片(JPG、PNG、TIFF)轉換成一個PDF文件。這非常適合建立數位作品集、演示文稿或報告,當您需要將多個圖像文件合併成一個有組織的,普遍可讀的PDF格式。
轉換圖片為PDF時有哪些自訂選項可用?
IronPDF提供廣泛的自訂選項,包括獨特的圖片放置和行為(適應頁面、置中、裁剪),新增文字和HTML的頁眉及頁腳,應用水印,設置自訂頁面大小,以及包含背景和前景疊加。這些功能可讓您從圖片建立外觀專業的PDF文件。
圖片轉PDF功能支持哪些平台?
IronPDF的圖片轉PDF功能在Windows、macOS和Linux平台上無縫運作,使其對各種開發環境非常靈活。您可以透過NuGet安裝或下載Windows安裝程式來在您偏好的平台上開始使用。
怎麼在轉換為PDF時控制圖片放置?
IronPDF提供獨特的圖片放置和行為,包括適應頁面、置中於頁面和裁剪頁面。這些導出圖片行為讓您可以精確控制圖片在最終PDF文件中的外觀,確保您的視覺內容專業呈現。
What are the rendering customization options available in IronPDF?
IronPDF allows you to use the ChromePdfRenderOptions object to customize the rendering process. This includes options like JavaScript execution, CSS media types, and custom headers. You can also incorporate web fonts and icons for a more polished document.
Is it possible to convert a PDF back to an image using IronPDF?
Yes, IronPDF supports converting or rasterizing PDF documents back into images. This can be useful for various applications that require different formats for distribution or presentation.
Can IronPDF work on different operating systems?
IronPDF is versatile and can operate across Windows, macOS, and Linux environments, making it an excellent choice for various development platforms.
How do I optimize PDF size when combining multiple images using IronPDF?
IronPDF offers PDF compression options to optimize the file size when merging multiple images. It's especially useful when creating large digital portfolios or photo albums, ensuring quality is maintained without large file sizes.
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。







