既存のPDFファイルの画像を置き換える

Contents
[ ]

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

ImagesコレクションのReplaceメソッドを使用すると、既存のPDFファイル内の画像を置き換えることができます。

Imagesコレクションは、ページのResourcesコレクション内にあります。画像を置き換えるには:

  1. Documentオブジェクトを使用してPDFファイルを開きます。
  2. 特定の画像を置き換え、DocumentオブジェクトのSaveメソッドを使用して更新されたPDFファイルを保存します。

次のコードスニペットは、PDFファイル内の画像を置き換える方法を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "ReplaceImage.pdf"))
    {
        // Replace a particular image in the document
        using (var imageStream = new FileStream(dataDir + "NewImage.jpg", FileMode.Open))
        {
            document.Pages[1].Resources.Images.Replace(1, imageStream);
        }

        // Save PDF document
        document.Save(dataDir + "ReplaceImage_out.pdf");
    }
}