Java REST API を使用して PowerPoint に新しいスライドを追加する

この記事に従って、Java REST API を使用して PowerPoint に新しいスライドを追加 します。クラウド ストレージの Java ベースの SDK を使用して、Java ベースの API で PowerPoint スライドを追加する方法 を学習します。既存のプレゼンテーションの必要なインデックスにスライドを挿入する方法を説明します。

前提条件

Java REST API を使用してスライドを追加する手順

  1. スライドを追加するためのユーザーIDとシークレットを設定してSlidesApiオブジェクトを作成します。
  2. スライドを挿入するために、対象のプレゼンテーションをクラウドストレージにアップロードします。
  3. アップロードしたプレゼンテーション名と移動先のスライドインデックスを指定して、CreateSlide() メソッドを呼び出します。
  4. 新しい空のスライドを追加した後、すべてのスライドの URL を表示します。
  5. 追加のスライドを含む出力プレゼンテーションをダウンロードして保存します

これらの手順では、Java RESTful サービスを使用して PowerPoint にスライドを追加する方法について説明します。必要な情報を使用して SlidesApi オブジェクトを作成し、ソース プレゼンテーションをクラウド ストレージにアップロードし、アップロードしたファイル名とターゲット スライド インデックスを使用して CreateSlide() メソッドを呼び出します。

Java ベースの API を使用して PowerPoint にスライドを追加するコード

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.ResourceUri;
import com.aspose.slides.model.Slides;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class Example_AddPresentationSlides {
protected static SlidesApi presentationApi;
public Example_AddPresentationSlides() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void addSlide() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
// Add a presentation slide
Slides response = presentationApi.createSlide(fileName, null,1, null, storageFolderName, null);
for (ResourceUri slide : response.getSlideList())
{
System.out.println(slide.getHref());
}
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);
// Copy the downloaded presentation with new slide added to the local directory
copyFile(presentationFile, new File(localPath, fileName));
System.out.println("Presentation slide deleted 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 にスライドを追加する方法 を示しています。新しいスライドのレイアウト タイプ、インデックス、またはレイアウト スライド名を使用して、レイアウト エイリアスを設定できます。アップロードされたプレゼンテーションがパスワードで保護されている場合は、CreateSlide メソッドを呼び出すときにパスワードを指定します。

この記事では、空のスライドを挿入する手順を説明しました。スライドを削除するには、Java REST API を使用して PowerPoint スライドを削除する の記事を参照してください。

 日本語