如何使用 C# 在 PDF 上繪製文字與位圖
IronPDF 讓您能透過 DrawText 和 DrawBitmap 方法,在現有 PDF 上繪製文字與圖像,讓您在不改變原始內容的前提下,透過浮水印、標誌、註解等進行客製化,並提升視覺效果。
在 PDF 上繪製文字和圖片,是指將內容新增至現有文件中。 IronPDF 可無縫實現此功能。 透過整合文字與圖片,您可以自訂 PDF 文件中的浮水印、標誌和註解,從而提升文件的視覺效果與品牌形象。 此外,文字與圖片有助於資訊呈現、資料視覺化及互動式表單的建立。
快速入門:使用 IronPDF 向 PDF 文件新增文字與圖片立即開始快速且高效地為您的 PDF 文件新增文字與圖片。 透過 DrawText 和 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 檔案中 - 使用PDF工具新增圖像
DrawBitmap方法 - 匯出已編輯的 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")我可以使用哪些字型?
DrawText 方法目前支援 IronPDF 中的所有標準字型,包括 Arial(或 Symbol 以及 ZapfDingbats。 請參閱《管理字型》文章中的"IronPDF 標準字型"章節,以了解這些字型型別的斜體、粗體及斜體變體。
ZapfDingbats 字型可顯示 ▲ 等符號。 如需支援符號的完整清單,請參閱維基百科的 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].Width和pdf.Pages[pageIndex].Height取得頁面尺寸 - 旋轉:旋轉參數接受度數(0-360)作為角度值,用於調整文字角度
- 顏色選項:除了基本顏色外,請使用 RGB 數值:
Color.FromArgb(255, 100, 100)
如何在 PDF 上繪製圖像?
IronPDF 的 DrawBitmap 方法可讓您將位圖新增至現有的 PDF 文件中。 此方法的功能類似於"圖片加蓋"功能,可讓您將圖片加蓋至現有的 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");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
常見問題
我可以使用什麼方法將文字和圖片新增到現有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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。