PDFドキュメントにページを追加

Aspose.PDF for .NET APIは、C#または他の.NET言語を使用してPDFドキュメント内のページを操作するための完全な柔軟性を提供します。PDFドキュメントのすべてのページは、PDFページを操作するために使用できるPageCollectionに保持されます。 Aspose.PDF for .NETを使用すると、ファイル内の任意の位置にPDFドキュメントにページを挿入したり、PDFファイルの最後にページを追加したりできます。 このセクションでは、C#を使用してPDFにページを追加する方法を示します。

PDFファイルにページを追加または挿入

Aspose.PDF for .NETを使用すると、ファイル内の任意の位置にPDFドキュメントにページを挿入したり、PDFファイルの最後にページを追加したりできます。

次のコードスニペットは、Aspose.PDF.Drawingライブラリでも動作します。

所定の位置にPDFファイルに空白ページを挿入

PDFファイルに空白ページを挿入するには:

  1. 入力PDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  2. 指定されたインデックスでPageCollectionコレクションのInsertメソッドを呼び出します。
  3. Saveメソッドを使用して出力PDFを保存します。

次のコードスニペットは、PDFファイルにページを挿入する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPage()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "InsertEmptyPage.pdf"))
    {
       // Insert an empty page in a PDF
       document.Pages.Insert(2);
        // Save PDF document
       document.Save(dataDir + "InsertEmptyPage_out.pdf");
    }
}

上記の例では、デフォルトのパラメータで空白ページを追加しました。ドキュメント内の他のページと同じサイズにする必要がある場合は、いくつかのコード行を追加する必要があります。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPageWithParameters()
{
    var page = document.Pages.Insert(2);
    //copy page parameters from page 1
    page.ArtBox = document.Pages[1].ArtBox;
    page.BleedBox = document.Pages[1].BleedBox;
    page.CropBox = document.Pages[1].CropBox;
    page.MediaBox = document.Pages[1].MediaBox;
    page.TrimBox = document.Pages[1].TrimBox;
}

PDFファイルの最後に空白ページを追加

時には、ドキュメントが空白ページで終了することを確認したい場合があります。このトピックでは、PDFドキュメントの最後に空白ページを挿入する方法を説明します。

PDFファイルの最後に空白ページを挿入するには:

  1. 入力PDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  2. パラメータなしでPageCollectionコレクションのAddメソッドを呼び出します。
  3. Saveメソッドを使用して出力PDFを保存します。

次のコードスニペットは、PDFファイルの最後に空白ページを挿入する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPageAtTheEnd()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
    
    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "InsertEmptyPageAtEnd.pdf"))
    {
        // Insert an empty page at the end of a PDF file
        document.Pages.Add();
        // Save PDF document
        document.Save(dataDir + "InsertEmptyPageAtEnd_out.pdf");
    }
}