如何使用C#在PDF上繪製文字和位圖
IronPDF使您可以使用DrawText和DrawBitmap方法在現有的PDF上繪製文字和圖片,允許自訂水印、標誌、註釋,以及在不更改原內容的情況下改進視覺效果。
在 PDF 上繪製文字和影像涉及將內容新增至現有文件。 IronPDF 支援此功能。 透過加入文字和圖片,您可以自訂 PDF,加入水印、標誌和註釋,改善文件的視覺效果和品牌識別。 此外,文字和圖片也有助於資訊呈現、資料可視化和互動表單的建立。
快速入門:使用IronPDF向PDF添加文字和圖像開始用文字和圖像快速有效地增強您的 PDF 文件。 使用DrawText和DrawBitmap方法,您可以輕鬆自訂PDF,新增水印、標誌或註釋。 此範例演示如何在特定坐標繪製文字並在您的 PDF 中插入圖像。
-
1使用 IronPDF 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
IronPdf.PdfDocument.FromFile("input.pdf") .DrawText("Hello World", FontTypes.TimesNewRoman.Name, 12, 0, 100, 100, Color.Black, 0) .DrawBitmap(AnyBitmap.FromFile("logo.png"), 0, 50, 250, 500, 300) .SaveAs("output.pdf");C# -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronPDF,並使用 免費試用
最小工作流程(5 個步驟)
- 下載IronPDF的C#程式庫,在PDF上繪製文字和圖片
- 導入目標 PDF 文件
- 使用
DrawText方法為匯入的 PDF 檔案新增所需字型的文字。 - 使用
DrawBitmap方法為 PDF 新增影像 - 匯出已編輯的 PDF 文件
如何在 PDF 上繪製文字?
DrawText 方法可用於 PdfDocument 物件上,允許您在不更改原始內容的情況下向現有的PDF添加文字。 此方法特別適用於在 PDF 中加入動態內容,類似於更複雜疊加的蓋章文字及圖片功能。
瞭解座標系統
繪製文字之前,請先瞭解 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")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中的標準字體,包括Courier、Arial(或Helvetica)、TimesNewRoman、Symbol和ZapfDingbats。 訪問管理字體文章中的「IronPDF 的標準字體」部分以獲取這些字體類型的斜體、粗體及傾斜變體。
ZapfDingbats 字型可以顯示如 ▲ 等符號。 有關支援符號的完整列表,請訪問Wikipedia 上的 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0);
// 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0);// Multi-line text example with proper spacing
string textWithNewlines = "Some text\nSecond line\nThird line with more content";
pdfDoc.DrawText(textWithNewlines, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0);
// 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0);' 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0)
' 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0)' 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0)
' 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, FontTypes.Arial.Name, 12, 0, 100, 100, Color.Black, 0)如何使用自訂字體?
使用DrawText方法支援自訂字體,擴展您的字體選擇超出標準字體。 在保持品牌一致性或使用專用字型時,此功能至關重要。 有關進階字體管理,請參閱我們的PDF 字體管理指南。
以下是添加Pixelify Sans Font到文字的範例:
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");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")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].Width和pdf.Pages[pageIndex].Height獲取頁面尺寸 - 旋轉: 旋轉參數接受 0 到 360 度的角度文字
- 顏色選項:在基本顏色之外,使用RGB值:
Color.FromArgb(255, 100, 100)
如何在 PDF 上繪製圖片?
IronPDF的DrawBitmap方法使您可以將位圖添加到現有的PDF文檔中。 此方法的功能類似於 Image Stamper 功能,可讓您在現有的 PDF 上標示圖片。 對於複雜的圖片操作需求,請參閱我們的將圖片添加到 PDF指南。
DrawBitmap方法對於大圖像效果最好。 使用較小分辨率的圖片時,您可能會遇到以下異常:IronPdf.Exceptions.IronPdfNativeException:'繪製圖片時出錯:資料長度(567000)小於預期(756000)'。要解決此問題,請使用Image Stamper,該工具可以處理所有大小的圖片。範例影像

實施看起來如何?
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");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")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:
PixelFormat屬性指定位圖的顏色數據格式,主要控制透明度支援。 預設值是Format32bppArgb。 通過將參數作為選項傳遞,選擇像素枚舉格式Format32bppRgb和Format32bppArgb。 這在處理 PDF 中的背景和前景元素時很有用。 - IgnorePageRotation:
bool屬性決定在繪製位圖時該方法是否忽略頁面旋轉。預設值為false。 特別是在所有頁面上一致地應用水印時,不論轉換方式如何,都會非常有用。
常見用例與最佳實踐
在 PDF 上繪製文字和圖片時,請考慮這些實際應用:
- 動態加水印: 在敏感文件上加上公司標誌或「機密」印記
- 頁碼標註: 在所有頁面中一致位置標出頁碼
- 簽名位置: 在表單上指定位置添加簽名圖像
- 頁眉/頁尾增強: 配合現有頁眉添加動態內容
為了調試和監控 PDF 操作,實施自訂日誌以追蹤繪製操作並確保正確執行。
性能考量
處理多頁或大型文件時:
- 盡可能進行批次作業,以盡量減少記憶體使用量
- 使用後棄置位圖物件以釋放資源
- 考慮預先載入常用的圖片以改善效能
準備好看看您還能做些什麼嗎? 查看我們的教程頁面:編輯PDF
常見問題
什麼是IronPDF中的DrawText方法?
IronPDF中的DrawText方法允許您使用指定的字體、大小和位置將文字添加到現有的PDF中。此方法對於插入動態內容(如註解、水印或品牌徽標)時非常有用,且不會改變原始PDF內容。
如何使用IronPDF在PDF上繪製圖像?
要使用IronPDF在PDF上繪製圖像,請使用DrawBitmap方法。此功能允許您將點陣圖圖像插入PDF文件中,用於水印、品牌標識或文件增強。您可以在PDF中指定圖像的位置和大小。
IronPDF可以使用自定義字體嗎?
是的,IronPDF支持使用DrawText方法的自定義字體。這種靈活性允許品牌一致性或使用專門字體。您可以從文件中添加自定義字體並使用它來在PDF中繪製文字。
在PDF上繪製文字和圖像的一些常見用途是什麼?
在PDF上繪製文字和圖像的一些常見用途包括添加動態水印(如公司徽標或「機密」印章)、一致地編頁碼、在表單上放置簽名圖像,以及用動態內容增強頁眉和頁腳。
在DrawBitmap方法中有哪些附加的圖像參數?
DrawBitmap方法允許您指定附加參數,如PixelFormat(控制位圖的顏色數據格式)和IgnorePageRotation(決定在繪製位圖時是否忽略頁面旋轉的布林值)。
IronPDF中的PDF座標系統如何構建?
在IronPDF中,PDF座標系統起始於頁面的左下角。X值向右增加,而Y值向上增加,這與許多圖形系統中起源於左上角不同。
IronPDF中的DrawText方法支持哪些字體?
IronPDF中的DrawText方法支持幾種標準字體,包括Courier、Arial (Helvetica)、TimesNewRoman、Symbol和ZapfDingbats。此外,還提供這些字體的斜體、粗體和傾斜變體。
如何在IronPDF中添加帶換行符的文本?
您可以通過在文本字符串中包含換行字符(' ')在IronPDF中添加帶換行的文本。此功能對於在PDF中創建多行註解或結構化文本佈局非常有用。
使用IronPDF處理大型文檔時有哪些性能考慮因素?
在IronPDF中處理大型文檔時,應考慮批量操作以減少內存使用,使用後釋放位圖對象以釋放資源,並預先加載常用的圖像以提高性能。
DrawBitmap方法如何處理低解析度的圖像?
對於因低解析度圖像而造成DrawBitmap異常的情況,建議使用Image Stamper,因為它能有效處理所有大小的圖像,並避免出現如「IronPdfNativeException」的錯誤。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。