如何新增、複製及刪除 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提供了所有必要的工具,使這一過程簡單快速。

開始使用 IronPDF

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

第一步:
green arrow pointer


將頁面添加到PDF中

在一行代碼中即可新增一頁至PDF文件。 在這個範例中,將會生成一份報告的 PDF 並在開頭添加封面頁。 要合併這兩個PDF,使用Merge方法。 讓我們以這兩個PDF文件為例: 下載 coverPage.pdf下載 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")
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 related to 刪除 PDF 中的頁面

喬迪·巴迪亞

軟體工程師

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