PDFページをプログラムで移動する C#

PDFドキュメントから別のPDFドキュメントへのページの移動

このトピックでは、C#を使用して1つのPDFドキュメントから別のドキュメントの最後にページを移動する方法を説明します。

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

ページを移動するには、次の手順を実行します。

  1. ソースPDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  2. 目的地PDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  3. PageCollectionコレクションからページを取得します。
  4. Addメソッドを使用して目的地ドキュメントにページを追加します。
  5. Saveメソッドを使用して出力PDFを保存します。
  6. ソースドキュメントでDeleteメソッドを使用してページを削除します。
  7. Saveメソッドを使用してソースPDFを保存します。

次のコードスニペットは、1ページを移動する方法を示しています。

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

    // Open PDF documents
    using (var srcDocument = new Aspose.Pdf.Document(dataDir + "MovingPageInput.pdf"))
    {
        using (var dstDocument = new Aspose.Pdf.Document())
        {
            var page = srcDocument.Pages[2];
            dstDocument.Pages.Add(page);
            // Save PDF document
            dstDocument.Save(dataDir + "MovingPage_out.pdf");
            srcDocument.Pages.Delete(2);
            // Save PDF document
            srcDocument.Save(dataDir + "MovingPageInput_out.pdf");
        }
    }
}

PDFドキュメントから別のPDFドキュメントへの複数ページの移動

  1. ソースPDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  2. 目的地PDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  3. 移動するページ番号の配列を定義します。
  4. 配列をループします:
    1. PageCollectionコレクションからページを取得します。
    2. Addメソッドを使用して目的地ドキュメントにページを追加します。
  5. Saveメソッドを使用して出力PDFを保存します。
  6. 配列を使用してソースドキュメントでDeleteメソッドを使用してページを削除します。
  7. Saveメソッドを使用してソースPDFを保存します。

次のコードスニペットは、1つのPDFドキュメントから別のPDFドキュメントに複数のページを移動する方法を示しています。

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

    // Open PDF documents
    using (var srcDocument = new Aspose.Pdf.Document(dataDir + "MovingBunchOfPagesInput.pdf"))
    {
        using (var dstDocument = new Aspose.Pdf.Document())
        {
            var pages = new[] { 1, 3 };
            foreach (int pageIndex in pages)
            {
                var page = srcDocument.Pages[pageIndex];
                dstDocument.Pages.Add(page);
            }
            // Save PDF document
            dstDocument.Save(dataDir + "MovingBunchOfPages_out.pdf");
            srcDocument.Pages.Delete(pages);
            // Save PDF document
            srcDocument.Save(dataDir + "MovingBunchOfPagesInput_out.pdf";
        }
    }
}

現在のPDFドキュメント内の新しい場所にページを移動する

  1. ソースPDFファイルを使用してDocumentクラスのオブジェクトを作成します。
  2. PageCollectionコレクションからページを取得します。
  3. 新しい場所(例えば、最後)にページをAddメソッドを使用して追加します。
  4. 前の場所でDeleteメソッドを使用してページを削除します。
  5. Saveメソッドを使用して出力PDFを保存します。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void MovingAPageInNewLocationInTheCurrentPdfDocument()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "MovingAPageInNewLocationInTheCurrentPdfDocumentInput.pdf"))
    {
        var page = document.Pages[2];
        document.Pages.Add(page);
        document.Pages.Delete(2);
        // Save PDF document
        document.Save(dataDir + "MovingAPageInNewLocationInTheCurrentPdfDocument_out.pdf");
    }
}