如何在C#中将图像转换为PDF

How to Convert Images to a PDF

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

将图像转换为PDF是一个有用的过程,可以将多个图像文件(如JPG、PNG或TIFF)合并为一个PDF文档。 这通常用于创建数字作品集、演示文稿或报告,使分享和存储出一个更有组织和普遍可读的格式图像集合更为便捷。

IronPDF允许您将单个或多个图像转换为PDF,并具备独特的图像放置和行为。 这些行为包括适应页面、居中页面和裁剪页面。 Additionally, you can add text and HTML headers and footers using IronPDF, apply watermarks with IronPDF, set custom page sizes, and include background and foreground overlays.

快速入门:使用IronPDF将图像转换为PDF

借助IronPDF的ImageToPdfConverter类,轻松将图像转换为PDF文档。 此示例演示了如何快速将图像文件转换为PDF,让开发人员能轻松将图像到PDF功能集成到其.NET C# 应用中。 通过最少的代码,您可以将图像转换为PDF,确保创建数字作品集或报告的工作流程顺畅且高效。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    IronPdf.ImageToPdfConverter.ImageToPdf("path/to/image.png").SaveAs("imageToPdf.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5步)

  1. 下载IronPDF C#库以将图像转换为PDF
  2. 准备您要转换的图像或图像集
  3. ImageToPdf静态方法提供图像路径
  4. 调整输出PDF中的图像放置和行为
  5. 使用IronPDF添加自定义文本和HTML页眉和页脚


转换图像为PDF示例

使用ImageToPdfConverter类中的ImageToPdf静态方法将图像转换为PDF文档。 该方法仅需图像的文件路径即可,并将以默认的图像放置和行为将其转换为PDF文档。 支持的图像格式包括.bmp、.jpeg、.jpg、.gif、.png、.svg、.tif、.tiff、.webp、.apng、.avif、.cur、.dib、.ico、.jfif、.jif、.jpe、.pjp和.pjpeg。

样本图像

class="content-img-align-center">
style="width=50%">Image Sample

代码

: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")
$vbLabelText   $csharpLabel

输出 PDF 文件

<hr

转换图像为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")
$vbLabelText   $csharpLabel

输出 PDF 文件

<hr

图像放置和行为

为了便于使用,我们提供了一系列有用的图像放置和行为选项。 例如,您可以将图像居中于页面或者适应页面大小同时保持其纵横比。 所有可用的图像放置和行为如下:

  • 页面左上角:图像放置于页面左上角。
  • 页面右上角:图像放置于页面右上角。
  • 居中于页面:图像居中于页面。
  • 适合页面并保持纵横比:图像适应页面同时保持其原始纵横比。
  • 页面左下角:图像放置于页面左下角。
  • 页面右下角:图像放置于页面右下角。
  • 适合页面:图像适应页面。
  • 裁剪页面:页面调整以适应图像。
: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")
$vbLabelText   $csharpLabel

图像行为对比

class="content-img-align-center">
Place the image at the top-left of the page
class="content-img-align-center">
Place the image at the top-right of the page
class="content-img-align-center">
Place the image at the center of the page
class="content-img-align-center">
Fit the image to the page while maintaining the aspect ratio
class="content-img-align-center">
Place the image at the bottom-left of the page
class="content-img-align-center">
Place the image at the bottom-right of the page
class="content-img-align-center">
Stretch the image to fit the page
class="content-img-align-center">
Crop the page to fit the image

<hr

应用渲染选项

要在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")
$vbLabelText   $csharpLabel

输出 PDF 文件

如果您想将PDF文档转换为图像或栅格化,请参阅我们的将PDF栅格化为图像指南

准备好看看您还能做些什么吗? 查看我们的教程页面:转换PDF文档

常见问题解答

如何在.NET C#中将图像转换为PDF?

你可以通过使用IronPDF库的ImageToPdfConverter类在.NET C#中将图像转换为PDF。该类提供了一种简单的方法,通过指定图像路径将图像文件转换为PDF文档。

在.NET C#中可以将哪些图像格式转换为PDF?

使用IronPDF,你可以将多种图像格式转换为PDF,包括BMP、JPEG、GIF、PNG、SVG、TIFF、WEBP等。这种灵活性确保了与大多数图像文件的兼容性。

如何在C#中将多个图像转换为一个PDF文档?

要使用IronPDF将多个图像转换为一个PDF文档,可以提供一个包含图像文件路径的IEnumerable对象。这样可以将所有图像合并到一个连贯的PDF文件中。

在PDF中放置图像有什么选项?

IronPDF为在PDF中放置图像提供了几个选项,如TopLeftCornerOfPage、CenteredOnPage、FitToPageAndMaintainAspectRatio等。这些选项允许您自定义图像在PDF页面上的显示方式。

在转换图像时可以在PDF中添加页眉和页脚吗?

是的,在使用IronPDF转换图像时,你可以在PDF中添加自定义文本和HTML页眉页脚。此功能对于在PDF文档中添加附加信息或品牌标识非常有用。

在图像转换过程中是否可以在PDF中包含水印?

是的,IronPDF允许您在转换过程中为PDF应用水印。可以通过IronPDF的教程中添加水印的指南来实现。

如何自定义图像到PDF的转换过程?

你可以通过使用ChromePdfRenderOptions对象来自定义IronPDF中的图像到PDF的转换过程。这允许您根据特定需求调整渲染设置。

IronPDF使用什么方法将图像转换为PDF?

IronPDF通过将图像作为HTML <img>标签导入,然后将其渲染为PDF格式来将图像转换为PDF。这种方法在输出文件的自定义方面提供了灵活性。

我在哪里可以找到关于使用IronPDF将图像转换为PDF的更多资源?

有关使用IronPDF将图像转换为PDF的更多资源和教程可以在IronPDF官方网站及其综合文档中找到。

将图像转换为PDF对数字项目有什么好处?

将图像转换为PDF可以通过将多个图像组织成一个可共享的文档来使数字项目受益。这对于创建需要以通用可读格式分发的数字作品集、演示或报告是理想的。

IronPDF 在将图像转换为 PDF 时是否与 .NET 10 完全兼容?

是的,IronPDF 完全兼容 .NET 10。该库官方支持 .NET 10(以及 .NET 9、8、7、6 和 Core 版本),无需任何变通或配置即可实现图像到 PDF 的转换、HTML 渲染以及所有关键功能。([ironpdf.com](https://ironpdf.com/?utm_source=openai))

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

审核者
Jeff Fritz
Jeffrey T. Fritz
首席项目经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布