PDFでページを追加、コピー、削除する方法
PDFにページを追加することは、ドキュメントに新しいコンテンツ(テキスト、画像、既存のPDFページなど)を挿入することを意味します。 PDFでページをコピーするとは、同じドキュメント内または別のPDFファイルから一つ以上のページを複製することを意味します。 PDFからページを削除することは、ドキュメントから不要なページを取り除くことを意味します。
ページを追加したり、コピーしたり、PDFドキュメントから削除したりすることができ、IronPDFはこれを簡単かつ迅速に行うために必要なすべてのツールを提供します。
C#でPDFのページを追加、コピー、削除する方法
- C#用IronPDFライブラリのダウンロード
- PDFにページを追加するために
マージ
そしてPDF挿入
メソッド - PDFのページをコピーする
CopyPage コピー ページ
そしてコピー ページ
メソッド - 次のツールを使用してPDFからページを削除する
ページを削除
そしてページを削除
メソッド - PDFを保存およびエクスポート
IronPDFを始めましょう
今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。
PDFにページを追加
PDFにページを追加するには、1行のコードで実行できます。 この例では、レポートのPDFが生成され、その冒頭にカバーページが追加されます。 両方のPDFを結合するには、『Merge
メソッド』を使用します。 この2つの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")
上記のコードを実行すると、出力として1つの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})