この記事に従って、Java REST API を使用して PowerPoint でカスタム シェイプを作成します。 Java RESTful サービスを使用して PowerPoint 用のカスタム図形を自動的に作成および追加する方法を学習します。形状の作成、パラメータの設定、特定のスライドへの追加に関するすべての詳細を共有します。
前提条件
ダウンロード Aspose.Slides Cloud SDK for Java for inserting shapes in slides
上記SDKを使用してJavaプロジェクトをセットアップし、形状を作成します
Java REST API を使用して PPT 形状を追加する手順
- SlidesApi オブジェクトを作成し、そこに図形を追加するためのソース プレゼンテーションをアップロードします。
- 変更するスライドの種類がマスター スライドであることを指定します
- 特定のプロパティを持つ新しい形状オブジェクトを作成する
- CreateSpecialSlideShape() メソッドを使用して、指定したスライドに新しい図形を追加します
- 新しい形状を含む更新されたプレゼンテーション ファイルをダウンロードします。
これらの手順では、Java REST API* を使用して *プレゼンテーション シェイプを生成する方法を説明します。 SlidesApi オブジェクトを作成し、プレゼンテーションをクラウド ストレージにアップロードし、変更するスライドの種類を指定して、必要なパラメーターを使用して Shape オブジェクトをインスタンス化します。マスター スライドに図形を追加し、新しい図形を含む更新されたプレゼンテーションをダウンロードします。
Java REST インターフェイスを使用して PPT の形状を追加するコード
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; | |
public class Example_CreateCustomShapeInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_CreateCustomShapeInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addCustomShapeInSlide() 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); | |
// Specify that the slide type to modify is a Master Slide | |
SpecialSlideType slideType = SpecialSlideType.MASTERSLIDE; | |
// Create a new shape object with specific properties | |
Shape dto = new Shape(); | |
dto.setX(100.0); | |
dto.setY(100.0); | |
dto.setWidth(500.0); | |
dto.setHeight(200.0); | |
dto.setShapeType(GeometryShape.ShapeTypeEnum.RECTANGLE); | |
dto.setText("New shape"); | |
// Add the new shape to the specified slide (master slide at index 1) and retrieve the created shape | |
Shape shape = (Shape)presentationApi.createSpecialSlideShape(fileName, 1, slideType, dto, null, null, | |
null,storageFolderName, null, null ); | |
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 サービス を使用してスライドのシェイプを処理する方法を示します。列挙子 GeometryShape.ShapeTypeEnum を使用して、Lines、Triangle、Rectangle、Diamond などの任意のタイプの図形を追加できます。このオプションは、LayoutSlide や NotesSlide などの他の特殊なタイプのスライドを選択するためにも使用できます。
この記事では、スライド上に図形を描く方法を学びました。画像を追加するには、記事 Java REST API を使用して PowerPoint に画像を追加する を参照してください。