如何在 C# 中在 PDF 上绘制文本和位图

How to Draw Text and Bitmap on PDFs

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

在PDF上绘制文本和图像涉及将文本和图像添加到现有文档。 IronPDF无缝启用此功能。 通过合并文本和图像,用户可以使用水印、徽标和注释自定义PDF,改善文档的视觉外观和品牌形象。 此外,文本和图像有助于信息的呈现、数据可视化和交互式表单的创建。

快速入门:使用IronPDF向PDF添加文本和图像

通过IronPDF快速高效地增强您的PDF文档与文本和图像。 使用DrawTextDrawBitmap方法,您可以轻松自定义您的PDF,无论是添加水印、徽标还是注释。 此示例展示了如何在指定坐标处绘制文本并无缝插入图像到您的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.

    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");
  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. 下载用于在PDF上绘制文本和图像的IronPDF C#库
  2. 导入目标PDF文档
  3. 使用DrawText方法为导入的PDF添加所需字体的文本
  4. 使用DrawBitmap方法向PDF添加图像
  5. 导出编辑过的PDF文档


在PDF上绘制文本示例

通过利用PdfDocument对象中可用的DrawText方法,您可以在不改变其原始内容的情况下向现有PDF添加文本。

:path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-text.cs
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

FontTypes类中可用的字体

DrawText方法当前支持IronPDF中的所有标准字体,包括Courier、Arial(或Helvetica)、Times New Roman、Symbol和ZapfDingbats。 访问管理字体文章中的'IronPDF中的标准字体'部分,获取这些字体类型的斜体、粗体和倾斜体变体。

尤其是ZapfDingbats字体,可以用于显示诸如 ▲ 之类的符号。 欲了解支持符号的完整列表,您可以访问维基百科上的Zapf Dingbats

PDF上的输出字体样本

class="content-img-align-center">
class="center-image-wrapper"> PDF上的字体样本

绘制带换行符的文本

绘制文本操作支持换行符,允许您使用内置换行符来呈现文本,以实现更好的格式和视觉清晰度。

为实现此目标,请将换行符(\n)添加到文本字符串中。 使用上面的示例,您可以绘制:

string textWithNewlines = "Some text\nSecond line";
pdfDoc.DrawText(textWithNewlines, font, position);
string textWithNewlines = "Some text\nSecond line";
pdfDoc.DrawText(textWithNewlines, font, position);
Imports Microsoft.VisualBasic

Dim textWithNewlines As String = "Some text" & vbLf & "Second line"
pdfDoc.DrawText(textWithNewlines, font, position)
$vbLabelText   $csharpLabel

使用自定义字体

我们还支持在DrawText方法中使用自定义字体; 下面是一个将Pixelify Sans Font添加到文本中的示例。

:path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-custom-font.cs
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")
$vbLabelText   $csharpLabel

<hr

绘制图像示例

使用IronPDF的DrawBitmap方法,您可以轻松地向现有PDF文档添加位图。 此方法与图像图章功能类似,允许您将图像盖印到现有PDF上。

[{i:(DrawBitmap方法最佳用于大图像。使用小分辨率图像时,您可能会遇到以下异常:IronPdf.Exceptions.IronPdfNativeException:"绘制图像时出错:数据长度(567000)小于预期(756000)"。为解决此问题,您可以使用Image Stamper,它无缝处理所有大小的图像。)}] #### 示例图像

class="content-img-align-center">
class="center-image-wrapper"> ![1200 x 627图像](/static-assets/pdf/how-to/draw-text-and-bitmap/ironSoftware.png)

DrawBitmap附加参数

### 代码 ```csharp :path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-bitmap.cs ``` ### 输出 PDF 文件

DrawBitmap附加参数

- **PixelFormat**:`PixelFormat`属性指定位图的颜色数据格式,主要控制其对透明度的支持。 默认值为Format32bppArgb。 开发人员可以通过将参数作为选项传入,在像素枚举格式`Format32bppRgb`和`Format32bppArgb`之间进行选择。 - **IgnorePageRotation**:此`bool`属性决定在绘制位图时方法是否忽略页面旋转。 默认值为false。 在必须在所有页面上始终应用水印而不受旋转影响的情况下尤其有用。 Especially useful in scenarios where you have to apply a watermark consistently to all pages, regardless of rotation. 准备好看看您还能做些什么吗? 请查看我们的教程页面:[编辑 PDF](https://ironpdf.com/tutorials/csharp-edit-pdf-complete-tutorial/)

常见问题解答

如何使用 C# 在 PDF 上绘制文本和图像?

您可以使用 IronPDF 的 DrawText 方法将文本添加到 PDF 中,并使用 DrawBitmap 方法添加图像。首先,从 NuGet 下载 IronPDF 库,导入您的 PDF,应用文本或图像,然后导出已编辑的文档。

在向 PDF 添加文本时支持哪些字体?

IronPDF 支持标准字体,如 Courier、Arial (Helvetica)、Times New Roman、Symbol 和 ZapfDingbats。您还可以通过在使用 DrawText 方法时指定自定义字体文件来使用自定义字体。

如何使用 C# 向 PDF 添加带换行符的文本?

要在 IronPDF 中添加带换行符的文本,请在使用 DrawText 方法时在文本字符串中包含换行符 ('\n')。这允许您格式化文本以获得更好的清晰度和组织。

我可以在 PDF 文档中使用自定义字体吗?

可以,IronPDF 支持自定义字体。您可以在使用 DrawText 方法时指定自定义字体文件,以在 PDF 中包含独特的排版样式。

如果在 PDF 上绘制图像时遇到错误,我应该怎么做?

如果遇到错误,尤其是在处理小图像时,请考虑使用 IronPDF 的图像盖章功能,该功能能够无缝处理所有大小的图像并防止常见异常。

如何在C#中将HTML转换为PDF?

你可以使用IronPDF的RenderHtmlAsPdf方法将HTML字符串转换为PDF。你还可以使用RenderHtmlFileAsPdf将HTML文件转换为PDF。

如何开始使用 IronPDF 编辑 PDF?

首先从 NuGet 下载 IronPDF 库。然后,导入所需的 PDF 文档,使用 DrawTextDrawBitmap 方法添加文本或图像,最后导出已编辑的文档。

IronPDF 是否完全兼容 .NET 10,能够用于绘制文本和图像?

是的,IronPDF 可以开箱即用地与 .NET 10 配合使用。它支持所有主要版本,包括 .NET 10,无需任何特殊设置。您可以在 .NET 10 应用程序中使用DrawTextDrawBitmap等方法,就像在早期版本中一样。IronPDF 的产品文档中已明确确认其与 .NET 10 的兼容性。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布