この記事に従って、Java REST API を使用して PowerPoint で画像を背景として使用 します。Java ベースの Cloud SDK を使用して、Java REST インターフェイスで PowerPoint の背景デザインを変更 する方法を学習します。スライドの背景画像をカスタマイズするためのさまざまなオプションについて説明します。
前提条件
ダウンロード Aspose.Slides Cloud SDK for Java for setting slides background
上記のSDKを使用してJavaプロジェクトをセットアップし、画像を背景として設定します。
Java Low Code API を使用して PowerPoint の背景を設定する手順
- スライドの背景を設定するためのSlidesApiオブジェクトを作成する
- ソースのPowerPointプレゼンテーションを一意の名前でクラウドストレージにアップロードします
- 画像ファイルのデータをバイト配列に読み込み、Base64文字列に変換します。
- SlideBackgroundオブジェクトを作成し、背景画像パラメータを設定するための塗りつぶし形式を設定します。
- SetBackground() メソッドを呼び出して PowerPoint スライドの背景を設定します
- 背景を設定した後、更新されたPowerPointプレゼンテーションをダウンロードします
これらの手順では、Java ベースの API を使用して PowerPoint プレゼンテーションの背景を設定する方法 について説明します。SlidesApi オブジェクトを作成し、プレゼンテーションをクラウド ストレージにアップロードし、画像データを読み取って Base 64 文字列に変換し、SlideBackground オブジェクトで使用して FillFormat を設定します。最後に、SetBackground() メソッドを呼び出して画像を背景として追加し、出力ファイルをディスクにダウンロードします。
Java Low Code API を使用して PPT 背景を追加するコード
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.PictureFill; | |
import com.aspose.slides.model.SlideBackground; | |
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_AddPresentationBackgroundImage { | |
protected static SlidesApi presentationApi; | |
public Example_AddPresentationBackgroundImage() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addBackgroundImage() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String imageFileName = "Background.png"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
SlideBackground pictureBackground = new SlideBackground(); | |
PictureFill pictureFill = new PictureFill();//For customization of the background image | |
pictureFill.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName))); | |
pictureFill.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH); | |
pictureBackground.setFillFormat(pictureFill); | |
// Set slide background image | |
SlideBackground currentBackground = presentationApi.setBackground(fileName, 1, pictureBackground, null, storageFolderName, null); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new background image to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide background image set and 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 Low Code API を使用してプレゼンテーション スライドの背景を設定する方法を示しています。画像の塗りつぶしモード、グロー、内側の影、外側の影、ソフト エッジ、反射など、画像のさまざまなプロパティを設定できます。アップロードされた PowerPoint プレゼンテーションが保護されている場合は、パスワードを入力してください。
この記事では、Java REST インターフェイスを使用して PPT の背景画像を設定する方法を説明しました。プレゼンテーション内または別のプレゼンテーションにスライドをコピーする場合は、記事 Java REST API を使用して PowerPoint スライドをコピーする を参照してください。