如何新增、複製及刪除 PDF 頁面

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

喬迪·巴迪亞

將頁面添加到 PDF 涉及向文件中插入新內容,如文字、圖像或現有的 PDF 頁面。複製 PDF 中的頁面是指在同一文件內或從一個 PDF 文件到另一個 PDF 文件中複製一個或多個頁面。從 PDF 中刪除頁面則涉及從文件中移除不需要的頁面。

任何 PDF 文件的頁面都可以被添加、複製或刪除,IronPDF 提供了所有必要的工具,使這過程變得簡單而迅速。

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

如何為 PDF 添加頁面

僅需一行程式碼即可在 PDF 中新增頁面。在此範例中,將生成一份報告的 PDF,並在開始部分添加封面頁。要合併這兩個 PDF,使用 Merge 方法。讓我們以這兩個 PDF 文件作為範例: 封面.pdfcontentPage.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")
VB   C#

當我們執行上述程式碼時,我們會得到一個包含封面頁的單一 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)
VB   C#

從 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})
VB   C#

刪除 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})
VB   C#

喬迪·巴迪亞

軟體工程師

Jordi 最擅長 Python、C# 和 C++,當他不在 Iron Software 發揮技能時,他會進行遊戲編程。他負責產品測試、產品開發和研究,為持續產品改進增添了巨大的價值。多樣化的經驗使他感到挑戰和投入,他說這是與 Iron Software 合作的最喜歡的方面之一。Jordi 在佛羅里達州邁阿密長大,並在佛羅里達大學學習計算機科學和統計學。