How to Edit PDFs in C
Iron Software 已將各種 PDF 編輯功能簡化為易於理解的方法,並整合於 IronPDF 函式庫中。 無論是添加簽名、添加 HTML 頁尾、加蓋浮水印,還是添加註解。 IronPDF 正是您所需的工具,它能讓您擁有易讀的程式碼、程式化 PDF 生成、輕鬆的除錯,並可無縫部署至任何受支援的環境或平台。
IronPDF 具備眾多 PDF 編輯功能。 在這篇教學文章中,我們將逐步介紹其中幾項主要工具,並提供程式碼範例與說明。
透過本文,您將能充分了解如何使用 IronPDF 在 C# 中編輯 PDF 檔案。
快速入門:幾秒鐘內編輯您的 PDF 檔案 using IronPDF,輕鬆地在 C# 中編輯 PDF 文件。 本快速指南將向您展示如何在現有的 PDF 檔案中添加文字水印。只需幾行程式碼,您即可修改 PDF 並立即儲存變更。 非常適合需要快速且高效 PDF 編輯解決方案的開發人員。
目錄
- 編輯文件結構
- 編輯文件屬性
- 強化 PDF 設計
編輯文件結構
存取 PDF DOM 物件
操作與存取 PDF 物件既快速又直觀。 IronPDF 透過讓開發人員熟悉如何操作網頁的 DOM,從而簡化開發人員與 DOM 物件的互動方式——使開發人員能夠透過程式化方式存取並操作各種元素,例如文字。
:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object.cs
using IronPdf;
using System.Linq;
// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create a PDF from a URL
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Access DOM Objects
var objects = pdf.Pages.First().ObjectModel;
Imports IronPdf
Imports System.Linq
' Instantiate Renderer
Private renderer As New ChromePdfRenderer()
' Create a PDF from a URL
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Access DOM Objects
Private objects = pdf.Pages.First().ObjectModel
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
儲存與匯出文件
為了儲存和匯出文件,IronPDF 允許使用者將編輯後的文件 PdfDocument.SaveAs 快速儲存至磁碟,同時也支援匯出至其他格式,例如二進位資料和記憶體串流。
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
從記憶體載入 PDF
IronPDF 可與現有的 C# .NET 應用程式完美整合; 我們可以透過 MemoryStream 物件,從 MemoryStream 載入並建立 PDF 檔案。
:path=/static-assets/pdf/content-code-examples/how-to/pdf-memory-stream-from-stream.cs
using IronPdf;
using System.IO;
// Read PDF file as stream
var fileByte = File.ReadAllBytes("sample.pdf");
// Instantiate PDF object from stream
PdfDocument pdf = new PdfDocument(fileByte);
Imports IronPdf
Imports System.IO
' Read PDF file as stream
Private fileByte = File.ReadAllBytes("sample.pdf")
' Instantiate PDF object from stream
Private pdf As New PdfDocument(fileByte)
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
將 PDF 匯出至記憶體
同樣地,我們也可以透過 MemoryStream 物件,在 C# .NET 中將 PDF 匯出至 MemoryStream。
:path=/static-assets/pdf/content-code-examples/how-to/pdf-to-memory-stream-to-stream.cs
using IronPdf;
using System.IO;
var renderer = new ChromePdfRenderer();
// Convert the URL into PDF
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export PDF as Stream
MemoryStream pdfAsStream = pdf.Stream;
// Export PDF as Byte Array
byte[] pdfAsByte = pdf.BinaryData;
Imports IronPdf
Imports System.IO
Private renderer = New ChromePdfRenderer()
' Convert the URL into PDF
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Export PDF as Stream
Private pdfAsStream As MemoryStream = pdf.Stream
' Export PDF as Byte Array
Private pdfAsByte() As Byte = pdf.BinaryData
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
編輯文件內容
Parse PDFs in C
using IronPDF 從 PDF 中擷取文字既快速又簡單。 只需使用 ExtractAllText 方法即可擷取每頁的所有文字,讓您能輕鬆存取並運用文件內容。 這項強大功能能提升生產力並簡化您的工作流程!
:path=/static-assets/pdf/content-code-examples/how-to/csharp-parse-pdf-parse-pdf.cs
using IronPdf;
// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Extract all text from an pdf
string allText = pdf.ExtractAllText();
// Extract all text from page 1
string page1Text = pdf.ExtractTextFromPage(0);
Imports IronPdf
' Select the desired PDF File
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Extract all text from an pdf
Private allText As String = pdf.ExtractAllText()
' Extract all text from page 1
Private page1Text As String = pdf.ExtractTextFromPage(0)
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
擷取文字與圖片
using IronPDF,您不僅能提取文字,還能極其輕鬆地從 PDF 中擷取圖片! 透過 ExtractAllImages 功能,您可以迅速擷取所需的所有視覺元素。
:path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-image.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Extract images
var images = pdf.ExtractAllImages();
for(int i = 0; i < images.Count; i++)
{
// Export the extracted images
images[i].SaveAs($"images/image{i}.png");
}
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Extract images
Private images = pdf.ExtractAllImages()
For i As Integer = 0 To images.Count - 1
' Export the extracted images
images(i).SaveAs($"images/image{i}.png")
Next i
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
隱去文字與區域
在需要透過遮蔽敏感資訊來保護其機密性的情境下,我們手邊有一項強大的工具:RedactTextonAllPages。 透過此功能,我們能夠輕鬆識別並隱藏整個 PDF 文件中所有特定關鍵字的出現位置。 這是一種有效的方法,既能確保機密資訊受到保護,同時也能讓我們放心分享該文件。
:path=/static-assets/pdf/content-code-examples/how-to/redact-text-redact-text.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("novel.pdf");
// Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric");
pdf.SaveAs("redacted.pdf");
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")
' Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric")
pdf.SaveAs("redacted.pdf")
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
替換 PDF 中的文字
若要使用 IronPDF 增強您的 PDF 文件,您可以輕鬆地在整個檔案中替換文字。透過使用 ReplaceTextonAllPages 函式,您可以提供需要被替換的 oldText,以及將作為其替代內容的 newText。 此方法可高效更新文件中所有 oldText 的實例,確保呈現一致且 Professional 的外觀。
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-all-page.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>");
string oldText = ".NET6";
string newText = ".NET7";
// Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText);
pdf.SaveAs("replaceText.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>")
Private oldText As String = ".NET6"
Private newText As String = ".NET7"
' Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText)
pdf.SaveAs("replaceText.pdf")
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
強化 PDF 設計
新增與編輯註解
IronPDF 提供豐富的 PDF 註解自訂選項,讓使用者能直接在 PDF 頁面中添加"便利貼"風格的註解。 透過 TextAnnotation 類別,可透過程式碼添加註解,並具備尺寸調整、透明度設定、圖示選擇及編輯功能等進階選項。
:path=/static-assets/pdf/content-code-examples/how-to/annotation-add-annotation.cs
using IronPdf;
using IronPdf.Annotations;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>");
// Create a PDF annotation object on a specified page index
TextAnnotation annotation = new TextAnnotation(0)
{
Title = "This is the title",
Contents = "This is the long 'sticky note' comment content...",
X = 50,
Y = 700,
};
// Add the annotation
pdf.Annotations.Add(annotation);
pdf.SaveAs("annotation.pdf");
Imports IronPdf
Imports IronPdf.Annotations
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>")
' Create a PDF annotation object on a specified page index
Dim annotation As New TextAnnotation(0) With {
.Title = "This is the title",
.Contents = "This is the long 'sticky note' comment content...",
.X = 50,
.Y = 700
}
' Add the annotation
pdf.Annotations.Add(annotation)
pdf.SaveAs("annotation.pdf")
如需更詳細的程式碼片段說明,或探索其更多功能,請參閱我們的完整操作指南。
戳記文字與圖片
IronPDF 提供豐富的自訂選項,讓您能自由設定如何將文字與圖片烙印至 PDF 檔案中。 在此範例中,我們將使用 TextStamper 類別,透過 ApplyStamp 方法在 PDF 上加蓋印章,如下所示。
:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-stamp-text.cs
using IronPdf;
using IronPdf.Editing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");
// Create text stamper
TextStamper textStamper = new TextStamper()
{
Text = "Text Stamper!",
FontFamily = "Bungee Spice",
UseGoogleFont = true,
FontSize = 30,
IsBold = true,
IsItalic = true,
VerticalAlignment = VerticalAlignment.Top,
};
// Stamp the text stamper
pdf.ApplyStamp(textStamper);
pdf.SaveAs("stampText.pdf");
Imports IronPdf
Imports IronPdf.Editing
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")
' Create text stamper
Private textStamper As New TextStamper() With {
.Text = "Text Stamper!",
.FontFamily = "Bungee Spice",
.UseGoogleFont = True,
.FontSize = 30,
.IsBold = True,
.IsItalic = True,
.VerticalAlignment = VerticalAlignment.Top
}
' Stamp the text stamper
pdf.ApplyStamp(textStamper)
pdf.SaveAs("stampText.pdf")
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
自訂浮水印
我們亦可採用 ApplyWatermark 方法,將浮水印無縫整合至新渲染的 PDF 檔案及現有文件中。 此功能有助於提升品牌辨識度,確保您的宣傳資料能展現專業形象。
:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark-apply-watermark.cs
using IronPdf;
string watermarkHtml = @"
<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>
<h1>Iron Software</h1>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>");
// Apply watermark
pdf.ApplyWatermark(watermarkHtml);
pdf.SaveAs("watermark.pdf");
Imports IronPdf
Private watermarkHtml As String = "
<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>
<h1>Iron Software</h1>"
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>")
' Apply watermark
pdf.ApplyWatermark(watermarkHtml)
pdf.SaveAs("watermark.pdf")
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
背景與前景
除了浮水印和印章外,我們還能透過 AddBackgroundPdf 完全自訂您的 PDF 背景。
:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-background.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");
// Render background
PdfDocument background = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>");
// Add background
pdf.AddBackgroundPdf(background);
pdf.SaveAs("addBackground.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")
' Render background
Private background As PdfDocument = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>")
' Add background
pdf.AddBackgroundPdf(background)
pdf.SaveAs("addBackground.pdf")
如需更詳細的程式碼片段說明,或探索其更多功能,請參閱我們的完整操作指南。
繪製文字與位圖
在 PDF 上繪製文字的操作直觀且簡單; 我們使用 DrawText 並提供必要的參數。 在此範例中,我們將新增字串 Some text,並採用 Times New Roman 字型。
: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");
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")
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
繪製直線與矩形
IronPDF 亦支援在 PDF 上繪製線條。 我們首先透過初始化兩個 PointF 類別來建立起始點與終點,接著使用 DrawLine 方法將其套用,如下所示。
:path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-line.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Configure the required parameters
int pageIndex = 0;
var start = new IronSoftware.Drawing.PointF(200,150);
var end = new IronSoftware.Drawing.PointF(1000,150);
int width = 10;
var color = new IronSoftware.Drawing.Color("#000000");
// Draw line on PDF
pdf.DrawLine(pageIndex, start, end, width, color);
pdf.SaveAs("drawLine.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Configure the required parameters
Private pageIndex As Integer = 0
Private start = New IronSoftware.Drawing.PointF(200,150)
Private [end] = New IronSoftware.Drawing.PointF(1000,150)
Private width As Integer = 10
Private color = New IronSoftware.Drawing.Color("#000000")
' Draw line on PDF
pdf.DrawLine(pageIndex, start, [end], width, color)
pdf.SaveAs("drawLine.pdf")
如需更詳細的程式碼片段說明,或想進一步探索其額外功能,請參閱我們的完整操作指南。
旋轉文字與頁面
若要旋轉 PDF 頁面,我們可以使用 SetPageRotation 方法來旋轉特定頁面。 在以下範例中,我們僅旋轉第 2 至 4 頁,並保留第 1 頁不變,以展示其功能。
:path=/static-assets/pdf/content-code-examples/how-to/rotating-text-set-page-rotation.cs
using IronPdf;
using IronPdf.Rendering;
using System.Linq;
// Import PDF
PdfDocument pdf = PdfDocument.FromFile("multi-page.pdf");
// Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90);
// Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270);
// Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180);
pdf.SaveAs("rotated.pdf");
Imports IronPdf
Imports IronPdf.Rendering
Imports System.Linq
' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("multi-page.pdf")
' Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90)
' Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270)
' Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180)
pdf.SaveAs("rotated.pdf")
有關旋轉角度的可用枚舉清單,請參閱此處。
如需更詳細的程式碼片段說明,或探索其更多功能,請參閱我們的完整操作指南。
轉換 PDF 頁面
除了旋轉功能外,我們還能透過提供一系列參數來轉換 PDF 頁面。 在以下範例中,我們選取 PDF 的第一頁,並使用 Transform 將第一頁的內容向右及向下移動 50 點,並將其縮放至原始大小的 80%。
:path=/static-assets/pdf/content-code-examples/how-to/transform-pdf-pages-transform-pdf.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("basic.pdf");
pdf.Pages[0].Transform(50, 50, 0.8, 0.8);
pdf.SaveAs("transformPage.pdf");
Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("basic.pdf")
pdf.Pages(0).Transform(50, 50, 0.8, 0.8)
pdf.SaveAs("transformPage.pdf")
如需更詳細的程式碼片段說明及探索其額外功能,請參閱我們的完整操作指南。
結論
上述範例清單顯示,IronPDF 具備多項關鍵功能,在編輯 PDF 時可立即使用。
若您想提出功能需求,或對 IronPDF 及授權有任何一般性疑問,請聯絡我們的支援團隊。 我們非常樂意為您提供協助。
常見問題
如何在 C# 中編輯 PDF 文件?
IronPDF 提供一套完整的工具,用於在 C# 中編輯 PDF 文件。您可以透過以下方法高效地操作頁面、新增或刪除內容,並進行各種修改: AddPage 以及 RemovePage.
using C# 為 PDF 添加浮水印的步驟有哪些?
若要在 C# 中為 PDF 添加浮水印,請使用 IronPDF 的 ApplyWatermark 方法並搭配 TextStamper 類別,讓您能自訂浮水印的文字、不透明度及旋轉角度。
如何使用 C# 壓縮 PDF 檔案?
IronPDF 提供 CompressAndSaveAs 方法,可在單次呼叫中壓縮內嵌圖片,並可選擇性地移除 PDF 結構樹。使用 PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40) 來壓縮並以指定的 JPEG 品質 (0–100) 節省。傳入 removeStructureTree: true 作為第三個參數,可進一步縮小檔案大小。若為記憶體內工作流程,請使用 CompressPdfToBytes 或 CompressPdfToStream 來取得壓縮後的輸出,以位元組陣列或 Stream 形式呈現,無需寫入磁碟。
是否可以在 C# 中為 PDF 檔案新增頁首和頁尾?
是的,透過 IronPDF,您可以使用 HtmlHeaderFooter 或 TextHeaderFooter 類別,您可以在其中定義 HTML 內容或文字,並調整高度等屬性。
有哪些方法可以將多個 PDF 合併為一個文件?
若要合併多個 PDF 檔案,請先載入每個文件, PdfDocument.FromFile ,然後使用 Merge 方法將其合併為單一檔案,並可儲存為新文件。
如何使用 C# 替換 PDF 文件中的特定文字?
IronPDF 允許您透過 ReplaceTextOnPage 方法,該方法會搜尋頁面上的特定文字並透過程式化方式進行替換。
IronPDF 能否協助建立及填寫 PDF 表單?
是的,IronPDF 支援程式化建立 PDF 表單及填寫現有表單欄位,可實現與 PDF 文件的動態互動。
如何使用 IronPDF 在 PDF 文件中添加註解?
IronPDF 提供在 PDF 文件中添加註解的功能。您可以透過註解物件建立文字註解及其他類型的註解,並將其放置於指定頁面。
如何使用 C# 處理 PDF 中的數位簽章?
IronPDF 支援使用數位簽名添加到 PDF 文件中 X509Certificate2 數位憑證向 PDF 文件添加數位簽章,以確保文件的真實性與完整性。
是否可以在 C# 中為 PDF 頁面設定背景?
是的,透過 IronPDF,您可以為 PDF 頁面設定背景,透過套用圖片或純色背景來提升文件設計與呈現效果。
IronPDF 是否相容於 .NET 10?它能帶來哪些好處?
是的——IronPDF 完全相容於 .NET 10,並充分利用其效能與語言增強功能。它能在桌面、網頁、MAUI 及服務等各類 .NET 10 專案中「直接運作」,並受益於陣列介面方法去虛擬化及堆疊分配增強等改進。

