如何新增、複製及刪除 PDF 頁面
向 PDF 添加頁面涉及將新內容,如文字、圖像或現有的 PDF 頁面,插入到文件中。 在 PDF 中複製頁面意味著在同一文件內或從一個 PDF 文件複製到另一個 PDF 文件中重複一個或多個頁面。 從PDF刪除頁面涉及從文件中移除不需要的頁面。
可以向任何PDF文件添加、複製和刪除頁面,IronPDF提供了所有必要的工具,使這一過程簡單快速。
如何在 C# 中添加、複製和刪除 PDF 頁面
- 下載 IronPDF 庫用於 C#
- 使用該工具將頁面新增至 PDF
合併
和插入PDF
方法 - 使用它從 PDF 中複製頁面
複製頁面
和複製頁面
方法 - 使用...從PDF中刪除頁面
移除頁面
和移除頁面
方法 - 儲存並匯出您的 PDF
開始使用 IronPDF
立即在您的專案中使用IronPDF,並享受免費試用。
將頁面添加到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")
執行上面的程式碼後,我們會得到一個單一的 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})