如何新增、複製及刪除 PDF 頁面
將頁面添加到 PDF 涉及向文件中插入新內容,如文字、圖像或現有的 PDF 頁面。複製 PDF 中的頁面是指在同一文件內或從一個 PDF 文件到另一個 PDF 文件中複製一個或多個頁面。從 PDF 中刪除頁面則涉及從文件中移除不需要的頁面。
任何 PDF 文件的頁面都可以被添加、複製或刪除,IronPDF 提供了所有必要的工具,使這過程變得簡單而迅速。
如何在 C# 中添加、複製和刪除 PDF 頁面
- 下載用於添加、複製和刪除 PDF 頁面的 C# 庫
- 使用該工具將頁面新增至 PDF
合併
和插入PDF
方法 - 使用它從 PDF 中複製頁面
複製頁面
和複製頁面s
方法 - 使用...從PDF中刪除頁面
移除頁面
和移除頁面s
方法 - Save 和 export your PDF
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronPDF 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。
Install-Package IronPdf
請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip
手動安裝到您的項目中
下載DLL如何為 PDF 添加頁面
僅需一行程式碼即可在 PDF 中新增頁面。在此範例中,將生成一份報告的 PDF,並在開始部分添加封面頁。要合併這兩個 PDF,使用 Merge
方法。讓我們以這兩個 PDF 文件作為範例: 封面.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")
當我們執行上述程式碼時,我們會得到一個包含封面頁的單一 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)
從 PDF 複製頁面
要從 PDF 複製頁面,只需調用 CopyPage
或 CopyPages
方法。這些方法分別用於複製單個和多個頁面。這些方法返回包含指定頁面的 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})
刪除 PDF 中的頁面
要從 PDF 中刪除頁面,您可以呼叫 RemovePage
或 RemovePages
方法。這些方法分別用於刪除單個和多個頁面。
: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})