如何在 C# 中添加、複製和刪除 PDF 頁面

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">

最小化工作流程(五个步骤)

  1. 下载适用于 C# 的 IronPDF 库
  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

从 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

在 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 內的頁面?

要在 C# 中複製 PDF 內的頁面,請使用 CopyPage 方法複製單個頁面,或使用 CopyPages 方法複製多個頁面,以創建所需頁面的副本。

在 C# 中刪除不需要的 PDF 頁面時應採取哪些步驟?

在 C# 中刪除不需要的頁面時,使用 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 頁面。

在哪裡可以找到用於練習頁面操作的範例 PDF 文件?

如 'coverPage.pdf' 和 'contentPage.pdf' 的範例 PDF 文件可以從教程中提供的鏈接下載進行練習。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。
準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布