如何使用 C# 在 PDF 上绘制文本和位图
IronPDF使您可以使用DrawBitmap方法在现有PDF上绘制文本和图像,允许通过水印、徽标和注释进行自定义,并在不更改原始内容的情况下改善视觉外观。
在 PDF 上绘制文本和图像涉及在现有文档中添加内容。 IronPDF无缝启用此功能。 通过加入文本和图像,您可以通过水印、徽标和注释自定义PDF,提升文档的视觉外观和品牌形象。 此外,文本和图像还有助于信息展示、数据可视化和交互式表单创建。
快速入门:使用IronPDF向 PDF 添加文本和图像开始用文本和图像快速高效地增强您的 PDF 文档。 使用DrawBitmap方法,您可以轻松定制PDF,通过添加水印、徽标或注释。 本示例演示了如何在特定坐标处绘制文本并将图像无缝插入 PDF。
-
1Install IronPDF with NuGet Package Manager
-
2复制并运行这段代码。
new ChromePdfRenderer() .RenderHtmlAsPdf("<h1>Doc</h1>") .DrawText("Hello World", FontTypes.TimesNewRoman.Name, 12, 0, 100, 100, Color.Black, 0) .DrawBitmap(AnyBitmap.FromFile("logo.png"), 0, 50, 250, 500, 300) .SaveAs("annotated.pdf");C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPDF
最小工作流程(5 个步骤)
- 下载 IronPDF 的 C# 库,在 PDF 上绘制文本和图像。
- 导入目标 PDF 文档
- 使用
DrawText方法为导入的 PDF 添加所需字体的文本 - 使用
DrawBitmap方法在 PDF 中添加图像 - 导出编辑后的 PDF 文档
如何在 PDF 上绘制文本?
可在DrawText方法允许您在不更改其原始内容的情况下向现有PDF加入文本。 这种方法尤其适用于向 PDF 添加动态内容,类似于 stamp text and image 功能,可用于更复杂的叠加。
了解坐标系
在绘制文本之前,请先了解 PDF 坐标系。 原点(0,0)位于页面左下角,X 值向右递增,Y 值向上递增。 这与许多图形系统不同,后者的原点位于左上方。
using IronPdf;
using IronSoftware.Drawing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Draw text on PDF
pdf.DrawText("Some text", FontTypes.TimesNewRoman.Name, FontSize: 12, PageIndex: 0, X: 100, Y: 100, Color.Black, Rotation: 0);
pdf.SaveAs("drawText.pdf");Imports IronPdf
Imports IronSoftware.Drawing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Draw text on PDF
pdf.DrawText("Some text", FontTypes.TimesNewRoman.Name, FontSize:=12, PageIndex:=0, X:=100, Y:=100, Color.Black, Rotation:=0)
pdf.SaveAs("drawText.pdf")我可以使用哪些字体?
DrawText方法目前支持IronPDF中的所有标准字体,包括ZapfDingbats。 访问管理字体文章中的'IronPDF中的标准字体'部分,获取这些字体类型的斜体、粗体和倾斜体变体。
ZapfDingbats 字体可以显示 ▲ 等符号。 有关受支持符号的完整列表,请访问 Wikipedia on Zapf Dingbats。
PDF上的输出字体样本

如何添加带换行符的文本?
绘制文本操作支持换行符,允许您使用内置换行符来呈现文本,以实现更好的格式和视觉清晰度。 这在添加多行注释或创建结构化文本布局时非常有用。
要实现这一点,请将newline characters (\n)加到文本字符串中。 以上面的例子为例:
// Multi-line text example with proper spacing
string textWithNewlines = "Some text\nSecond line\nThird line with more content";
pdfDoc.DrawText(textWithNewlines, font, position);
// You can also use Environment.NewLine for platform-specific line breaks
string platformText = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3";
pdfDoc.DrawText(platformText, font, position);' Multi-line text example with proper spacing
Dim textWithNewlines As String = "Some text" & vbLf & "Second line" & vbLf & "Third line with more content"
pdfDoc.DrawText(textWithNewlines, font, position)
' You can also use Environment.NewLine for platform-specific line breaks
Dim platformText As String = $"Line 1{Environment.NewLine}Line 2{Environment.NewLine}Line 3"
pdfDoc.DrawText(platformText, font, position)如何使用自定义字体?
自定义字体由DrawText方法支持,将您的字体选项扩展到标准字体之外。 在保持品牌一致性或使用专用字体时,这一特点至关重要。 有关高级字体管理,请参阅我们的管理 PDF 中的字体指南。
下面是一个示例,文本中添加了 Pixelify Sans 字体:
using IronPdf;
using IronSoftware.Drawing;
using System.IO;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Add custom font to the PDF
byte[] fontByte = File.ReadAllBytes(@".\PixelifySans-VariableFont_wght.ttf");
var addedFont = pdf.Fonts.Add(fontByte);
// Draw text on PDF
pdf.DrawText("Iron Software", addedFont.Name, FontSize: 12, PageIndex: 0, X: 100, Y: 600, Color.Black, Rotation: 0);
pdf.SaveAs("drawCustomFont.pdf");Imports IronPdf
Imports IronSoftware.Drawing
Imports System.IO
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Add custom font to the PDF
Private fontByte() As Byte = File.ReadAllBytes(".\PixelifySans-VariableFont_wght.ttf")
Private addedFont = pdf.Fonts.Add(fontByte)
' Draw text on PDF
pdf.DrawText("Iron Software", addedFont.Name, FontSize:= 12, PageIndex:= 0, X:= 100, Y:= 600, Color.Black, Rotation:= 0)
pdf.SaveAs("drawCustomFont.pdf")高级文本定位
在精确定位文本时,请考虑以下提示:
- 页面尺寸:使用
pdf.Pages[pageIndex].Height获取页面尺寸 - 旋转:旋转参数可接受角度(0-360),以显示有角度的文本。
- 颜色选项:超越基本颜色,使用RGB值:
Color.FromArgb(255, 100, 100)
如何在 PDF 上绘制图像?
DrawBitmap方法使您可以将位图添加到现有PDF文档中。 此方法的功能与图像印记功能类似,允许您将图像印记到现有的 PDF 文件上。 对于复杂的图像处理需求,请参阅我们的向 PDF 添加图像指南。
DrawBitmap方法对大图像效果最好。 使用较小分辨率图像时,可能会遇到以下异常: **IronPDF:'绘制图像时出错:数据长度 (567000) 小于预期 (756000)'。**要解决此问题,请使用图像标记器,它可以处理所有尺寸的图像。样本图像

实施是什么样的?
using IronPdf;
using IronSoftware.Drawing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Open the image from file
AnyBitmap bitmap = AnyBitmap.FromFile("ironSoftware.png");
// Draw the bitmp on PDF
pdf.DrawBitmap(bitmap, 0, 50, 250, 500, 300);
pdf.SaveAs("drawImage.pdf");Imports IronPdf
Imports IronSoftware.Drawing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Open the image from file
Dim bitmap As AnyBitmap = AnyBitmap.FromFile("ironSoftware.png")
' Draw the bitmap on PDF
pdf.DrawBitmap(bitmap, 0, 50, 250, 500, 300)
pdf.SaveAs("drawImage.pdf")输出PDF
还有哪些其他参数可用?
-
像素格式:
PixelFormat属性指定位图的颜色数据格式,主要控制透明度支持。 默认值是Format32bppArgb。 通过将参数作为一个选项传递,在像素枚举格式Format32bppArgb之间选择。 这在处理 PDF 中的 背景和前景元素时非常有用。 -
忽略页面旋转:此
bool属性确定在绘制位图时该方法是否忽略页面旋转。默认情况下,该值为false。 在对所有页面(无论旋转与否)一致地应用水印时尤其有用。
常见用例和最佳实践
在 PDF 上绘制文本和图像时,请考虑这些实际应用:
1.动态水印:为敏感文档添加公司徽标或 "机密 "标记 2.页码:在所有页面的一致位置绘制页码 3.签名位置:在表单指定位置添加签名图像 4.页眉/页脚增强:用动态内容补充现有页眉
为调试和监控 PDF 操作,请执行 自定义日志 以跟踪绘图操作并确保正确执行。
性能考虑
在处理多页或大文档时:
- 尽可能进行批量操作,以尽量减少内存使用量
- 使用后处置位图对象以释放资源
- 考虑预加载常用图片以提高性能
准备好看看您还能做些什么吗? 请查看我们的教程页面:编辑 PDF
常见问题解答
我可以使用哪些方法在现有 PDF 文件中添加文本和图像?
IronPDF 提供了两种向现有 PDF 添加内容的主要方法:用于添加文本的 DrawText 方法和用于添加图像的 DrawBitmap 方法。通过这些方法,您可以为 PDF 定制水印、徽标和注释,而无需更改原始文档内容。
在绘制文本时,PDF 坐标系是如何工作的?
在 IronPDF 中,PDF 坐标系的原点(0,0)位于页面的左下角。X 值向右增加,Y 值向上增加。这与原点位于左上角的许多图形系统不同。
在 PDF 上绘制文本时支持哪些字体?
IronPDF 的 DrawText 方法支持所有标准字体,包括 Courier、Arial (Helvetica)、Times New Roman、Symbol 和 ZapfDingbats。这些字体还提供斜体、粗体和斜体变体。ZapfDingbats 字体可用于显示特殊符号。
能否在一次操作中将文本和图像同时添加到 PDF 中?
是的,IronPDF 允许您将各种方法串联起来。您可以使用 ChromePdfRenderer 创建 PDF,然后在一行代码中依次应用 DrawText 和 DrawBitmap 方法,从而高效地为 PDF 文档添加多个元素。
在 PDF 上绘制文本和图像的常见用例有哪些?
IronPDF 的绘图功能通常用于添加水印以保护文档、插入公司徽标以树立品牌形象、创建注释以供文档审阅、改善视觉外观、促进数据可视化以及创建交互式表单。
How is the PDF coordinate system structured in IronPDF?
In IronPDF, the PDF coordinate system originates at the bottom-left corner of the page. X values increase to the right, and Y values increase upward, differing from many graphics systems where the origin is at the top-left.
What fonts are supported by the DrawText method in IronPDF?
The DrawText method in IronPDF supports several standard fonts, including Courier, Arial (Helvetica), TimesNewRoman, Symbol, and ZapfDingbats. Additionally, it offers italic, bold, and oblique variants of these fonts.
How can I add text with line breaks in IronPDF?
You can add text with line breaks in IronPDF by including newline characters '\n' within the text string. This feature is useful for creating multi-line annotations or structured text layouts in PDFs.
What are some performance considerations when using IronPDF for large documents?
When handling large documents in IronPDF, consider batching operations to reduce memory usage, disposing of bitmap objects after use to free resources, and pre-loading frequently used images to boost performance.
How does the DrawBitmap method handle small resolution images?
For smaller resolution images causing exceptions with DrawBitmap, using the Image Stamper is recommended as it effectively handles images of all sizes and avoids errors like the 'IronPdfNativeException'.

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