IronPDF 教程 在 C# 中编辑 PDF How to Edit PDFs in C# Curtis Chau 已更新:八月 27, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English Iron Software已将各种PDF编辑功能简化为IronPDF库中易于理解的方法。 无论是添加签名、添加HTML页脚、盖上水印,还是添加注释。 IronPDF是适合您的工具,允许您编写可读代码、程序化PDF生成、轻松调试,以及无缝部署到任何支持的环境或平台。 IronPDF拥有许多编辑PDF的功能。 在这篇教程文章中,我们将逐步讲解其中的一些主要功能,并提供代码示例和解释。 通过这篇文章,您将对如何使用IronPDF在C#中编辑您的PDF有一个很好的理解。 快速开始:几秒钟内编辑您的PDF文件 使用IronPDF在C#中轻松编辑PDF文档。 本快速指南向您展示如何向现有PDF文件添加文本戳记。只需几行代码,您就可以修改PDF并立即保存更改。 非常适合需要快速高效PDF编辑解决方案的开发人员。 Get started making PDFs with NuGet now: Install IronPDF with NuGet Package Manager PM > Install-Package IronPdf Copy and run this code snippet. var pdf = IronPdf.PdfDocument.FromFile("example.pdf"); pdf.ApplyStamp(new IronPdf.Editing.TextStamper("Confidential")); pdf.SaveAs("edited_example.pdf"); Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial 目录 编辑文档结构 访问PDF DOM对象 保存和导出PDF文档 从内存加载PDF 导出PDF到内存 编辑文档属性 在C#中解析PDF 提取文本和图像 编辑文本和区域 在PDF中替换文本 增强PDF设计 添加和编辑注释 戳记文本和图像 自定义水印 背景和前景 绘制文本和位图 绘制线条和矩形 旋转文本和页面 转换PDF页面 今天在您的项目中使用 IronPDF,免费试用。 第一步: 免费开始 使用 NuGet 安装 PM > Install-Package IronPdf 在 IronPDF 上查看 NuGet 快速安装。超过 1000 万次下载,它正以 C# 改变 PDF 开发。 您也可以下载 DLL 或 Windows 安装程序。 编辑文档结构 访问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 $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 保存和导出文档 为了保存和导出文档,IronPDF允许用户快速将编辑后的文档PdfDocument.SaveAs保存到磁盘,同时也允许导出为其他格式,如二进制数据和内存流。 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 从内存加载PDF IronPDF完美地集成到现有的C# .NET应用程序中; 我们可以通过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) $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 导出PDF到内存 同样,我们可以通过MemoryStream对象在C# .NET中将PDF导出到内存流。 :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 $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 编辑文档文本 在C#中解析PDF 使用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) $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 提取文本和图像 使用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 $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 编辑文本和区域 在需要通过编辑敏感信息以保护隐私的情况下,我们有一个强大的工具: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") $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 在PDF中替换文本 为了提高您的PDF文档的质量,您可以通过IronPDF轻松替换整个文件中的文本。通过利用ReplaceTextonAllPages功能,您可以提供需要替换的oldText以及作为其替代的newText。 这方法有效地更新了文档中所有oldText的实例,确保了一致且专业的外观。 :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") $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 增强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"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 戳记文本和图像 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") $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 自定义水印 我们还可以通过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") $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 背景和前景 除了水印和印章,我们还可以添加背景来完全自定义您的PDF,使用AddBackgroundPdf。 :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") $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 绘制文本和位图 在PDF上绘制文本既直观又简单; 我们使用DrawText并为其提供必要的参数。 在本例中,我们使用Times New Roman字体添加了新短语Some text。 :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 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 绘制线条和矩形 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") $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 旋转文本和页面 要旋转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") $vbLabelText $csharpLabel 有关旋转角度的枚举列表,请参阅这里。 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 转换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"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 如需更详细的代码片段说明并探索其附加功能,请参阅我们的综合操作指南。 结论 上面的示例列表表明,IronPDF在编辑PDF时具有即开即用的关键功能。 如果您想要提出功能请求或对IronPDF或许可有任何常见问题,请联系支持团队。 我们将非常乐意为您提供帮助。 常见问题解答 如何在 C# 中编辑 PDF 文档? IronPDF 提供了全面的工具来编辑 C# 中的 PDF 文档。您可以操作页面,添加或删除内容,并通过使用诸如 AddPage 和 RemovePage 之类的方法高效地应用各种修改。 使用 C# 向 PDF 添加水印的步骤是什么? 要在 C# 中向 PDF 添加水印,请使用 IronPDF 的 ApplyWatermark 方法与 TextStamper 类一起使用,允许您自定义水印的文本、不透明度和旋转。 如何使用 C# 压缩 PDF 文件? IronPDF 提供 CompressImages 方法,可减小嵌入在 PDF 中的图像的大小,从而有效地压缩文件大小。 是否可以在 C# 中为 PDF 添加页眉和页脚? 是的,使用 IronPDF,您可以使用 HtmlHeaderFooter 或 TextHeaderFooter 类为 PDF 添加页眉和页脚,您可以定义 HTML 内容或文本并调整属性如高度。 可以使用什么方法将多个 PDF 合并为一个文档? 要合并多个 PDF,使用 PdfDocument.FromFile 加载每个文档,然后使用 Merge 方法将它们合并为一个文件,并可以将其保存为新文档。 如何使用 C# 在 PDF 中替换特定文本? IronPDF 允许您通过使用方法 ReplaceTextOnPage 替换 PDF 中的文本,该方法会在页面上搜索特定文本然后程序化地替换它。 IronPDF 能否促进 PDF 表单的创建和填写? 可以,IronPDF 支持创建 PDF 表单并程序化填写现有表单字段,从而能够动态互动 PDF 文档。 如何使用 IronPDF 向 PDF 添加注释? IronPDF 提供向 PDF 添加注释的功能。您可以通过使用注释对象将文本注释和其他类型的注释放置在所需页面上。 如何使用 C# 处理 PDF 中的数字签名? IronPDF 支持使用数字证书 X509Certificate2 将数字签名添加到 PDF 文档中,确保文档的真实性和完整性。 是否可以在 C# 中为 PDF 页面设置背景? 可以,使用 IronPDF,通过应用图像或颜色背景来增强文档设计和展示,您可以为 PDF 页面设置背景。 IronPDF 是否兼容 .NET 10?兼容 .NET 10 能带来哪些好处? 是的,IronPDF 完全兼容 .NET 10,并充分利用了其性能和语言方面的增强功能。它在 .NET 10 项目中“开箱即用”,支持桌面、Web、MAUI 和服务等多种应用场景,并受益于数组接口方法去虚拟化和堆栈分配增强等改进。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 16,133,208 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:16,133,208 查看许可证