このガイドに従って、Java REST API を使用して PowerPoint に画像を追加する 方法を学習します。Java ベースの Cloud SDK を使用して、Java Low Code API を使用して PowerPoint に画像を追加する方法 を学習します。この記事では、スライドに追加する前に画像をカスタマイズするためのさまざまなプロパティについて説明します。
前提条件
ダウンロード Aspose.Slides Cloud SDK for Java for inserting images into the slides
上記のSDKを使用してJavaプロジェクトをセットアップし、スライドに画像を挿入します。
Java ベースの API を使用して PowerPoint に画像を追加する手順
- SlidesApi オブジェクトをインスタンス化してスライドに画像を挿入します
- 画像を追加する対象のプレゼンテーションファイルをアップロードします
- 必要な形式で画像データを準備する
- スライドに配置するためのPictureFrameオブジェクトを作成する
- 特定のスライドに画像を挿入するには、CreateShape メソッドを呼び出します。
- 画像を追加した後ファイルをダウンロードする
これらの手順は、Java REST インターフェイスを使用して PowerPoint に画像を追加する方法をまとめたものです。ソース プレゼンテーションをクラウド ストレージにアップロードし、Convert 名前空間の ToBase64String() メソッドを使用して画像を準備し、この画像を使用して PictureFrame オブジェクトを作成します。最後に、CreateShape() メソッドを呼び出して、指定されたスライドに画像を追加し、更新されたプレゼンテーションをダウンロードします。
Java REST API を使用して PowerPoint スライドに画像を追加するコード
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardCopyOption; | |
import java.util.Base64; | |
public class Example_AddPictureInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddPictureInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addPictureInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String imageFileName = "ShapeImage.png"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
PictureFrame imageFrame = new PictureFrame(); | |
imageFrame.setX(50.0); | |
imageFrame.setY(50.0); | |
imageFrame.setWidth(350.0); | |
imageFrame.setHeight(250.0); | |
PictureFill pictureFill1 = new PictureFill(); | |
pictureFill1.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH); | |
pictureFill1.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName))); | |
imageFrame.setPictureFillFormat(pictureFill); | |
// Add the image to the third slide of the presentation. | |
ShapeBase shapeResponse = presentationApi.createShape(fileName, 3, imageFrame, null, null, | |
null,storageFolderName, null, null); | |
// Output the URI of the newly added image shape. | |
System.out.println("Image added at: "+ shapeResponse.getSelfUri().getHref()); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new image shape to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide with image shape is copied to: " + localPath + fileName); | |
} | |
public static byte[] readFileToByteArray(String filePath) throws IOException { | |
Path path = new File(filePath).toPath(); | |
return Files.readAllBytes(path); | |
} | |
private void copyFile(File sourceFile, File targetFile) throws IOException { | |
if (sourceFile == null || !sourceFile.exists()) { | |
throw new IOException("Source file does not exist: " + sourceFile); | |
} | |
// Ensure the target directory exists | |
Path targetPath = targetFile.toPath(); | |
Files.createDirectories(targetPath.getParent()); | |
// Copy the file | |
Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); | |
} | |
} |
このサンプル コードは、Java RESTful サービスを使用して PowerPoint に画像を配置する方法 を示しています。PictureFrame オブジェクトの X プロパティと Y プロパティを設定することで画像の開始位置を定義し、PictureFill クラス オブジェクトを使用して画像の塗りつぶしモードを定義できます。PictureFrame プロパティは、元のサイズに関係なく、スライド上の画像のサイズを定義します。
この記事では、画像を追加する方法を説明しました。プレゼンテーションにメモを追加するには、Java REST API を使用して PowerPoint スライドにメモを追加する の記事を参照してください。