iTextSharp:向 PDF 添加图像
IronPDF 提供更简单的图像添加功能,代码行数更少,并且提供永久许可证;而 iTextSharp 提供更精细的控制,但需要更多代码,并且商业用途的许可协议为 AGPL,许可较为复杂。
在.NET环境中处理PDF文件时,向文档添加图像是许多应用程序的常见需求。 无论您是生成带有公司徽标的发票、创建带有嵌入式图表的报告,还是制作带有水印的自定义文档,将图像嵌入 PDF 的功能都至关重要。 C# 中两个流行的 PDF 库是IronPDF和iTextSharp 。 本文从易用性、性能、许可模式以及图像压缩和格式支持等高级功能等方面,对这两个库在向 PDF 添加图像方面的能力进行了比较。
IronPDF 和 iTextSharp 的主要区别是什么?
每个图库提供哪些图像功能?
IronPDF: IronPDF 简化了向 PDF 添加图像的操作,内置支持来自 URL 的本地图像和远程图像。 其 API 提供对位置、宽度和缩放的控制。 该库支持 JPEG、PNG、GIF、 SVG和WebGL 内容。 印章功能提供了添加水印、信头和叠加图像的方法。 Chrome渲染引擎可确保复杂图形和响应式图像的精确渲染。
iTextSharp: iTextSharp 通过其底层 API 提供图像嵌入功能。 它提供了一个灵活的接口,可以处理 JPG 和 PNG 格式。 但是,自定义需要更多代码,尤其是在精确定位图像或实现透明度和旋转时。 该库需要手动处理坐标系和比例计算。 为了实现响应式尺寸,您通常需要实现自己的视口控件。
它们如何处理大型文件和复杂操作?
商业用途的成本影响是什么?
如何使用 IronPDF 向 PDF 添加图片?
如何安装和配置 IronPDF?
通过NuGet将 IronPDF 安装到您的 C# 或 Visual Basic 项目中。 该库支持多种安装方法,包括Windows Installer和Docker 部署:
Install-Package IronPdf
安装完成后,您可以使用 IronPDF 的API 参考开始添加图像。 该库可在Windows 、 Linux和macOS平台上无缝运行。
IronPDF 中的基本图像添加功能是什么样的?
快速入门:使用 IronPDF 向 PDF 添加图像
使用 IronPDF 的印章 API 向 PDF 添加图像只需要编写极少的代码。
立即开始使用 NuGet 创建 PDF 文件:
使用 NuGet 包管理器安装 IronPDF
复制并运行这段代码。
using IronPdf; using IronPdf.Editing; class Program { public static void Main(string[] args) { // Create a renderer to convert HTML to PDF ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render a basic PDF with some HTML content PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>"); // Create an ImageStamper with the image URL ImageStamper stamper = new ImageStamper(new Uri("___PROTECTED_URL_61___")); // Configure image position and size stamper.HorizontalAlignment = HorizontalAlignment.Center; stamper.VerticalAlignment = VerticalAlignment.Middle; stamper.Width = 200; // Width in pixels stamper.Height = 100; // Height in pixels // Apply the stamp (image) to the PDF document pdf.ApplyStamp(stamper); // Save the modified PDF to a file pdf.SaveAs("output.pdf"); } }部署到您的生产环境中进行测试
如何添加水印和背景图片?
IronPDF 擅长为现有 PDF 添加水印和背景图像。 该库支持多种冲压操作和自定义定位:
using IronPdf;
using IronPdf.Editing;
// Load an existing PDF
PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");
// Create a watermark with transparency
ImageStamper watermark = new ImageStamper("watermark.png")
{
Opacity = 30, // 30% opacity for subtle watermarking
Rotation = -45, // Diagonal watermark
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Middle
};
// Apply watermark to all pages
pdf.ApplyStamp(watermark);
// Add a header logo
ImageStamper logo = new ImageStamper("company-logo.png")
{
HorizontalOffset = Length.Millimeters(10),
VerticalOffset = Length.Millimeters(10),
Width = 150,
Height = 50
};
pdf.ApplyStamp(logo, PageSelection.FirstPage);
pdf.SaveAs("watermarked-invoice.pdf");using IronPdf;
using IronPdf.Editing;
// Load an existing PDF
PdfDocument pdf = PdfDocument.FromFile("invoice.pdf");
// Create a watermark with transparency
ImageStamper watermark = new ImageStamper("watermark.png")
{
Opacity = 30, // 30% opacity for subtle watermarking
Rotation = -45, // Diagonal watermark
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Middle
};
// Apply watermark to all pages
pdf.ApplyStamp(watermark);
// Add a header logo
ImageStamper logo = new ImageStamper("company-logo.png")
{
HorizontalOffset = Length.Millimeters(10),
VerticalOffset = Length.Millimeters(10),
Width = 150,
Height = 50
};
pdf.ApplyStamp(logo, PageSelection.FirstPage);
pdf.SaveAs("watermarked-invoice.pdf");您还可以创建包含多张图片的复杂布局:
// Add images from Base64 strings
string base64Image = Convert.ToBase64String(File.ReadAllBytes("signature.png"));
HtmlStamper signatureStamp = new HtmlStamper($"<img src='data:image/png;base64,{base64Image}'/>");
signatureStamp.HorizontalAlignment = HorizontalAlignment.Right;
signatureStamp.VerticalAlignment = VerticalAlignment.Bottom;
pdf.ApplyStamp(signatureStamp);// Add images from Base64 strings
string base64Image = Convert.ToBase64String(File.ReadAllBytes("signature.png"));
HtmlStamper signatureStamp = new HtmlStamper($"<img src='data:image/png;base64,{base64Image}'/>");
signatureStamp.HorizontalAlignment = HorizontalAlignment.Right;
signatureStamp.VerticalAlignment = VerticalAlignment.Bottom;
pdf.ApplyStamp(signatureStamp);PDF 查看器显示一个简单的文档,其中包含"Hello World"文本和位于页面中心的 IronPDF 徽标水印——演示了 IronPDF 的图像水印功能,该功能可精确定位并控制不透明度。
本示例使用ChromePdfRenderer类从HTML 字符串渲染新的 PDF。 使用 IronPDF 的图像盖章工具,您可以从提供的 URL 创建图像并将其应用到 PDF 中。 这演示了如何用最少的代码向PDF 页面添加图像。 渲染选项可让您完全控制输出质量。
如何在IronPDF中控制图像位置和尺寸?
IronPDF 为图像放置提供了广泛的自定义选项。 您可以通过印章工具的定位功能设置对齐方式和偏移量来指定页面位置。 该库支持使用坐标进行绝对定位和使用对齐选项进行相对定位。 高级功能包括自定义纸张尺寸和边距控制,以实现精确的布局。
如何使用 iTextSharp 向 PDF 添加图片?
如何安装和配置 iTextSharp?
通过 NuGet 安装 iTextSharp。 与 IronPDF 的全面平台支持不同,iTextSharp 的部署选项有限:
Install-Package itext7
配置完成后,即可使用 iTextSharp 的文档操作功能添加图像。 该库需要额外的配置才能进行字体管理和UTF-8 支持。
iTextSharp 中的基本图像添加功能是什么样的?
以下是如何使用 iTextSharp 插入图片:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
class Program
{
public static void Main(string[] args)
{
// Initialize PDF writer
var pdfWriter = new PdfWriter("output.pdf");
// Initialize PDF document
var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter);
// Initialize document
var document = new Document(pdfDocument);
// Create ImageData from image file
ImageData imageData = ImageDataFactory.Create("iText.png");
// Create an Image instance
Image image = new Image(imageData);
// Set fixed position for the image in the PDF
image.SetFixedPosition(50, 100); // x, y coordinates
// Scale image to fit within specified dimensions
image.ScaleToFit(200, 200); // Width, Height
// Add image to document
document.Add(image);
// Close the document
document.Close();
}
}using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
class Program
{
public static void Main(string[] args)
{
// Initialize PDF writer
var pdfWriter = new PdfWriter("output.pdf");
// Initialize PDF document
var pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfWriter);
// Initialize document
var document = new Document(pdfDocument);
// Create ImageData from image file
ImageData imageData = ImageDataFactory.Create("iText.png");
// Create an Image instance
Image image = new Image(imageData);
// Set fixed position for the image in the PDF
image.SetFixedPosition(50, 100); // x, y coordinates
// Scale image to fit within specified dimensions
image.ScaleToFit(200, 200); // Width, Height
// Add image to document
document.Add(image);
// Close the document
document.Close();
}
}如何添加多张图片并处理复杂的布局?
对于创建产品目录或相册等复杂场景,您需要手动管理布局。 IronPDF的目录功能和书签支持可以整理多页文档:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
using iText.Layout.Properties;
// Create a grid of images
var pdfWriter = new PdfWriter("catalog.pdf");
var pdfDocument = new PdfDocument(pdfWriter);
var document = new Document(pdfDocument);
// Add images in a grid layout
float xPosition = 50;
float yPosition = 750;
int imagesPerRow = 3;
int currentImage = 0;
string[] imageFiles = { "product1.jpg", "product2.jpg", "product3.jpg",
"product4.jpg", "product5.jpg", "product6.jpg" };
foreach (string imagePath in imageFiles)
{
ImageData imageData = ImageDataFactory.Create(imagePath);
Image image = new Image(imageData);
// Calculate position
int column = currentImage % imagesPerRow;
int row = currentImage / imagesPerRow;
float x = xPosition + (column * 180);
float y = yPosition - (row * 250);
image.SetFixedPosition(x, y);
image.ScaleToFit(150, 200);
document.Add(image);
currentImage++;
}
document.Close();using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.IO.Image;
using iText.Layout.Properties;
// Create a grid of images
var pdfWriter = new PdfWriter("catalog.pdf");
var pdfDocument = new PdfDocument(pdfWriter);
var document = new Document(pdfDocument);
// Add images in a grid layout
float xPosition = 50;
float yPosition = 750;
int imagesPerRow = 3;
int currentImage = 0;
string[] imageFiles = { "product1.jpg", "product2.jpg", "product3.jpg",
"product4.jpg", "product5.jpg", "product6.jpg" };
foreach (string imagePath in imageFiles)
{
ImageData imageData = ImageDataFactory.Create(imagePath);
Image image = new Image(imageData);
// Calculate position
int column = currentImage % imagesPerRow;
int row = currentImage / imagesPerRow;
float x = xPosition + (column * 180);
float y = yPosition - (row * 250);
image.SetFixedPosition(x, y);
image.ScaleToFit(150, 200);
document.Add(image);
currentImage++;
}
document.Close();PDF 查看器显示一个居中的 ITEXT 徽标,徽标下方是"CORE"字样,页面其余部分为空白——这是 iTextSharp 图像插入功能的示例输出,演示了其基本的定位和缩放功能。
图片被添加到坐标 (50, 100) 处,并缩放至 200x200 像素大小。 PdfWriter创建输出文件,使PDF 写入器能够处理图像。 为了更好地组织内容,可以考虑使用页码和页眉/页脚。
如何在iTextSharp中控制图像位置和尺寸?
iTextSharp 通过手动坐标计算提供对定位和大小的控制。 您可以缩放图像,同时保持宽高比,或者拉伸到特定尺寸。 SetFixedPosition方法提供精确的放置控制,您可以调整对齐方式和旋转角度。 但是,它缺少 IronPDF 的视口控制和响应式尺寸选项。
IronPDF 和 iTextSharp 的性能对比如何?
这两个库在处理向 PDF 添加图像方面存在显著差异:
有哪些高级图像功能可用?
它们如何处理不同的图像格式?
IronPDF支持所有图像格式:
- JPEG/JPG质量优化 -支持透明度的 PNG GIF(静态和动态帧)
- SVG 矢量图形
- TIFF 文件,包括多页 TIFF 文件
- WebP 用于现代网络图像
- Base64编码图像 来自 Azure Blob 存储的图像
iTextSharp支持标准格式:
- JPEG/JPG
- 带透明通道的PNG图像
- GIF(仅第一帧)
- TIFF(单页)
- BMP 和其他位图格式
图像质量和压缩情况如何?
IronPDF提供内置的压缩选项,可在文件大小和质量之间取得平衡。 该库提供数据清理功能和数据扁平化功能:
// Configure compression when rendering
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.JpegQuality = 85; // 0-100 quality scale
// Compress existing PDFs with images
PdfDocument pdf = PdfDocument.FromFile("large-images.pdf");
pdf.CompressImages(60); // Compress to 60% quality
pdf.SaveAs("compressed.pdf");
// Additional optimization options
renderer.RenderingOptions.GrayScale = true; // Convert to grayscale
renderer.RenderingOptions.DPI = 150; // Reduce DPI for web viewing// Configure compression when rendering
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.JpegQuality = 85; // 0-100 quality scale
// Compress existing PDFs with images
PdfDocument pdf = PdfDocument.FromFile("large-images.pdf");
pdf.CompressImages(60); // Compress to 60% quality
pdf.SaveAs("compressed.pdf");
// Additional optimization options
renderer.RenderingOptions.GrayScale = true; // Convert to grayscale
renderer.RenderingOptions.DPI = 150; // Reduce DPI for web viewingiTextSharp需要通过图像数据处理进行手动压缩处理,这增加了质量控制的实现复杂性。 您必须自行实现灰度转换和DPI调整。
如何从现有PDF文件中提取图像?
IronPDF 提供可靠的图像提取功能:
// Extract all images from a PDF
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
var images = pdf.ExtractAllImages();
foreach (var image in images)
{
// Save extracted images
image.SaveAs($"extracted_image_{images.IndexOf(image)}.png");
}
// Extract images from specific pages
var pageImages = pdf.ExtractImagesFromPage(0); // First page// Extract all images from a PDF
PdfDocument pdf = PdfDocument.FromFile("document.pdf");
var images = pdf.ExtractAllImages();
foreach (var image in images)
{
// Save extracted images
image.SaveAs($"extracted_image_{images.IndexOf(image)}.png");
}
// Extract images from specific pages
var pageImages = pdf.ExtractImagesFromPage(0); // First page许可和定价模式有哪些?
我应该选择哪个库来向PDF中添加图像?
! IronPDF 和 iTextSharp PDF 库的功能对比表,展示了 IronPDF 在易用性、性能、许可灵活性、完整的图像格式支持以及企业级 PDF 生成高级定位功能方面的优势
IronPDF和 iTextSharp 都提供了在 C# 中向 PDF 添加图像的有效工具,各有其独特的优势。 IronPDF 以易用性、性能和许可灵活性著称,是处理复杂 PDF 和图像的理想选择,且代码量极少。 它为大型 PDF 文件提供了更好的可扩展性,并提供一次性购买许可,避免了重复性成本或复杂的开源限制。
该库支持SVG和 WebP 等现代格式,并具有内置压缩功能和直观的定位 API,适合需要高质量文档生成的企业应用程序。 IronPDF 的Chrome 渲染引擎可确保在所有平台上实现像素级完美输出。
iTextSharp 适合开源项目,但需要编写更多代码才能获得相同的结果,并且处理较大的 PDF 文件时可能会遇到性能问题。 其底层 API 提供精细的控制,但增加了开发复杂性。 该库缺少响应式 CSS 支持和JavaScript 渲染等现代功能。
对于企业应用而言,IronPDF 的优势包括Docker 支持、 AWS Lambda 兼容性和Azure Functions 集成。 该图书馆还提供符合监管要求的PDF/A 合规性和PDF/UA 可访问性。
想要简化您的 PDF 图片处理流程吗? IronPDF 可以用最少的代码高效地将图片添加到 PDF 中。 试用IronPDF ,体验在 C# 项目中简化图像集成。 该实现方案为您处理了复杂性,节省了时间并改进了您的PDF 生成工作流程。 探索完整的功能集,加入数千名简化 PDF 操作的开发者行列。
常见问题解答
如何在 .NET 应用程序中将图像添加到 PDF?
您可以使用 IronPDF 的 ImageStamper 功能将图像添加到 PDF,从而轻松嵌入图像,并可以精确控制图像的位置和缩放。
使用 IronPDF 处理大型 PDF 文件有哪些性能优势?
IronPDF 专为高性能而优化,特别是在处理大型 PDF 文件和高分辨率图像时,使其非常适合需要高效处理的服务器端应用程序。
IronPDF 如何简化将图像嵌入 PDF 的过程?
IronPDF 通过提供直观的 API 来简化图像嵌入,只需少量代码即可在 PDF 文档中放置和缩放图像,提高开发者的生产力。
与 iTextSharp 相比 IronPDF 的许可模式是什么?
IronPDF 提供永久许可证,一次性购买,消除经常性的订阅费用,而 iTextSharp 使用 AGPL 许可证进行开源使用,并为专有项目提供商业选项。
能提供一个使用 IronPDF 添加图像到 PDF 的代码示例吗?
当然!您可以使用 IronPDF 的 PdfDocument 和 ImageStamper 类通过几行代码将图像嵌入 PDF,示例在文章中有演示。
IronPDF 和 iTextSharp 的图像放置自定义有何不同?
IronPDF 提供简单的图像放置自定义,具有对齐和偏移设置,而 iTextSharp 提供详细的图像定位控制,使用诸如 SetFixedPosition 的方法。
在选择 IronPDF 和 iTextSharp 时有哪些关键考虑因素?
选用 IronPDF 和 iTextSharp 的决定取决于易用性、大文件性能和许可需求等因素。IronPDF 因其用户友好和可扩展性而受到青睐,而 iTextSharp 则适合开源项目。
为什么推荐使用 IronPDF 进行高性能的服务器应用程序?
IronPDF 的架构是为高性能而设计的,能够高效处理大型文档和图像,使其成为需要速度和可靠性的服务器应用程序的绝佳选择。
在将图像添加到 PDF 时常见的故障排除步骤是什么?
在故障排除 PDF 中的图像嵌入问题时,确保图像路径正确,检查文件访问权限,并验证图像格式是否被所用 PDF 库支持。
IronPDF 的试用版如何帮助开发人员?
IronPDF 的试用版允许开发人员探索其功能,并测试其在处理 PDF 图像嵌入方面的能力,提供简化 C# 项目中 PDF 处理的机会。






