如何在PDF中添加、复制和删除页面使用C#

How to Add, Copy, and Delete Pages in PDFs

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

在PDF中添加页面包括将新的内容,如文本、图像或现有的PDF页面,插入到文档中。 在PDF中复制页面意味着在同一个文档内,或从一个PDF文件到另一个文件,复制一个或多个页面。 从PDF中删除页面涉及从文档中移除不需要的页面。

标题:快速开始:立即添加、复制和删除PDF页面

开始使用IronPDF无缝地在PDF中添加、复制和删除页面。 这个快速示例展示了如何轻松将附加内容合并到现有的PDF中。 通过利用IronPDF的高效方法,开发人员可以轻松管理PDF页面,确保平滑集成到任何C#项目中。

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.

    IronPdf.PdfDocument.FromFile("/input/path.pdf")
        .AppendPdf(IronPdf.PdfDocument.FromFile("/additional/path.pdf"))
        .SaveAs("/output/path.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. 下载IronPDF库用于C#
  2. 使用MergeInsertPdf方法将页面添加到PDF
  3. 使用CopyPageCopyPages方法从PDF中复制页面
  4. 使用RemovePageRemovePages方法从PDF中删除页面
  5. 保存并导出您的PDF

向PDF添加页面

在一行代码中即可向PDF添加页面。 在这个例子中,生成的是一个报告的PDF,并将添加封面页在其开头。 为了合并这两个PDF,将使用Merge方法。 Let's take these two PDF documents for our example: download coverPage.pdf and download contentPage.pdf.

:path=/static-assets/pdf/content-code-examples/how-to/add-copy-delete-pages-pdf-add.cs
using IronPdf;

// Import cover page
PdfDocument coverPage = PdfDocument.FromFile("coverPage.pdf");

// Import content document
PdfDocument contentPage = PdfDocument.FromFile("contentPage.pdf");

// Merge the two documents
PdfDocument finalPdf = PdfDocument.Merge(coverPage, contentPage);

finalPdf.SaveAs("pdfWithCover.pdf");
Imports IronPdf

' Import cover page
Private coverPage As PdfDocument = PdfDocument.FromFile("coverPage.pdf")

' Import content document
Private contentPage As PdfDocument = PdfDocument.FromFile("contentPage.pdf")

' Merge the two documents
Private finalPdf As PdfDocument = PdfDocument.Merge(coverPage, contentPage)

finalPdf.SaveAs("pdfWithCover.pdf")
$vbLabelText   $csharpLabel

当我们运行以上代码时,得到一个输出的单个PDF文件,其中封面页在最前面:

我们也可以使用InsertPdf方法在PDF的任意索引处添加页面。 在这个例子中,通过在 'contentPage.pdf' 的开头插入 'coverPage.pdf' 实现上述效果。

:path=/static-assets/pdf/content-code-examples/how-to/add-copy-delete-pages-pdf-insert.cs
using IronPdf;

// Import cover page
PdfDocument coverPage = PdfDocument.FromFile("coverPage.pdf");

// Import content document
PdfDocument contentPage = PdfDocument.FromFile("contentPage.pdf");

// Insert PDF
contentPage.InsertPdf(coverPage, 0);
Imports IronPdf

' Import cover page
Private coverPage As PdfDocument = PdfDocument.FromFile("coverPage.pdf")

' Import content document
Private contentPage As PdfDocument = PdfDocument.FromFile("contentPage.pdf")

' Insert PDF
contentPage.InsertPdf(coverPage, 0)
$vbLabelText   $csharpLabel

<hr

从PDF中复制页面

要从PDF中复制页面,只需调用CopyPageCopyPages方法即可。 这些方法分别用于复制单个和多个页面。 这些方法会返回包含指定页面的PdfDocument对象。

:path=/static-assets/pdf/content-code-examples/how-to/add-copy-delete-pages-pdf-copy.cs
using IronPdf;
using System.Collections.Generic;

// Copy a single page into a new PDF object
PdfDocument myReport = PdfDocument.FromFile("report_final.pdf");
PdfDocument copyOfPageOne = myReport.CopyPage(0);

// Copy multiple pages into a new PDF object
PdfDocument copyOfFirstThreePages = myReport.CopyPages(new List<int> { 0, 1, 2 });
Imports IronPdf
Imports System.Collections.Generic

' Copy a single page into a new PDF object
Private myReport As PdfDocument = PdfDocument.FromFile("report_final.pdf")
Private copyOfPageOne As PdfDocument = myReport.CopyPage(0)

' Copy multiple pages into a new PDF object
Private copyOfFirstThreePages As PdfDocument = myReport.CopyPages(New List(Of Integer) From {0, 1, 2})
$vbLabelText   $csharpLabel

<hr

删除PDF中的页面

要从PDF中删除页面,可以调用RemovePageRemovePages方法。 这些方法分别用于删除单个和多个页面。

:path=/static-assets/pdf/content-code-examples/how-to/add-copy-delete-pages-pdf-delete.cs
using IronPdf;
using System.Collections.Generic;

PdfDocument pdf = PdfDocument.FromFile("full_report.pdf");

// Remove a single page
pdf.RemovePage(0);

// Remove multiple pages
pdf.RemovePages(new List<int> { 2, 3 });
Imports IronPdf
Imports System.Collections.Generic

Private pdf As PdfDocument = PdfDocument.FromFile("full_report.pdf")

' Remove a single page
pdf.RemovePage(0)

' Remove multiple pages
pdf.RemovePages(New List(Of Integer) From {2, 3})
$vbLabelText   $csharpLabel

准备好看看您还能做些什么吗? 在这里查看我们的教程页面:组织PDF

常见问题解答

如何在 C# 中为 PDF 报告添加封面页?

您可以使用 Merge 方法将封面页 PDF 与现有报告 PDF 合并,从而为 PDF 报告添加封面页。

在 PDF 中插入特定索引页面的方法是什么?

要在 PDF 中插入特定索引的页面,请使用 InsertPdf 方法,该方法允许您指定新页面插入的位置。

如何使用 C# 复制 PDF 中的页面?

要在 PDF 中使用 C# 复制页面,请使用单页的 CopyPage 方法或多页的 CopyPages 方法,以创建所需页面的副本。

我应该采取哪些步骤来删除 C# 中 PDF 的多余页面?

要删除 C# 中 PDF 的多余页面,利用单页的 RemovePage 方法或多页的 RemovePages 方法,从文档中删除它们。

我可以在修改 PDF 后保存修改后的版本吗,如添加或删除页面?

可以,在添加或删除页面等更改后,您可以使用 SaveAs 方法保存修改后的 PDF,以存储更新后的文档。

在 C# 项目中操控 PDF 页面有哪些必要条件?

要在 C# 项目中操控 PDF 页面,首先从 NuGet 下载 IronPDF 库并将其包含在项目中,以访问必要的功能。

是否可以使用 C# 将两个 PDF 文档合并为一个?

是的,可以使用 Merge 方法将两个 PDF 文档合并为一个,该方法可将多个 PDF 文件合并为一个单一文档。

如何在 C# 中快速向 PDF 添加页面?

要在 C# 中快速向 PDF 添加页面,使用 Merge 方法附加新的 PDF 文档或 InsertPdf 方法在特定索引处插入页面。

用于 PDF 页面操控教程的编程语言是什么?

用于操控 PDF 页面教程中使用的是 C#,以展示如何编程方式添加、复制和删除 PDF 页面。

IronPDF 在添加、复制或删除 PDF 页面时是否支持 .NET 10?

是的——IronPDF 完全支持 .NET 10,可执行所有页面操作任务(添加、复制、删除)。它与 .NET 10 项目开箱即用,确保您无需特殊变通即可使用相同的页面方法。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布