プレゼンテーション画像コレクション内の画像を置き換える
Contents
[
Hide
]
Aspose.Slides for PHP via Javaは、スライドのシェイプ内の画像を置き換えることを可能にします。この記事では、異なるアプローチを使用してプレゼンテーション画像コレクションに追加された画像を置き換える方法を説明します。
プレゼンテーション画像コレクション内の画像の置き換え
Aspose.Slides for PHP via Javaは、プレゼンテーション画像コレクション内の画像を置き換えるためのシンプルなAPIメソッドを提供しています。以下の手順に従ってください:
- Presentationクラスを使用して、画像が含まれているプレゼンテーションファイルをロードします。
- バイト配列でファイルから画像をロードします。
- ターゲット画像をバイト配列内の新しい画像で置き換えます。
- 2番目のアプローチでは、Imageオブジェクトに画像をロードし、ロードされた画像でターゲット画像を置き換えます。
- 3番目のアプローチでは、プレゼンテーション画像コレクションにすでに追加された画像で画像を置き換えます。
- 修正されたプレゼンテーションをPPTXファイルとして書き込みます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
//Instantiate the presentation | |
Presentation presentation = new Presentation("presentation.pptx"); | |
//The first method | |
File file = new File("image1.png"); | |
byte[]data = new byte[(int) file.length()]; | |
InputStream inputStream = null; | |
try | |
{ | |
inputStream = new FileInputStream(file); | |
inputStream.read(data); | |
} | |
finally | |
{ | |
inputStream.close(); | |
} | |
IPPImage oldImage = presentation.getImages().get_Item(0); | |
oldImage.replaceImage(data); | |
//The second method | |
BufferedImage newImage = ImageIO.read(new File("image0.jpeg")); | |
IPPImage imageToReplace= presentation.getImages().addImage(newImage); | |
oldImage = presentation.getImages().get_Item(1); | |
oldImage.replaceImage(imageToReplace); | |
//The third method | |
oldImage = presentation.getImages().get_Item(2); | |
oldImage.replaceImage(presentation.getImages().get_Item(3)); | |
//Save the presentation | |
presentation.save("presentation_out.pptx", SaveFormat.Pptx); | |
} catch (Exception e) { | |
} | |