この記事に従って、Java REST API を使用して PowerPoint を PDF に変換 します。Java ベースの Cloud SDK を使用して、Java REST インターフェースを使用して PowerPoint を PDF に変換する 方法を学習します。変換プロセスをカスタマイズするためのさまざまなプロパティと属性について説明します。
前提条件
ダウンロード Aspose.Slides Cloud SDK for Java for converting presentations to PDF
上記のSDKを使用してJavaプロジェクトをセットアップし、PPTXをオンラインで変換します。
Java ベースの API を使用して PowerPoint ファイルを PDF に変換する手順
- PPTX を PDF に変換するために、クライアント ID とシークレットを使用して SlidesApi オブジェクトを作成します。
- 入力プレゼンテーションファイルをFileStreamオブジェクトにロードします。
- エクスポート形式をPDFとして定義し、PDFに変換するスライドのリストを作成します。
- Convert() メソッドを呼び出して、FileStream、エクスポート形式、スライド配列を指定します。
- 出力ファイルストリームを作成し、結果のストリームをPDFとして保存します。
これらの手順では、Java RESTful サービスを使用して PowerPoint プレゼンテーションを PDF に変換する方法について説明します。SlidesApi オブジェクトを作成し、プレゼンテーション ファイルをファイル ストリームに読み込み、出力ファイル形式を定義し、PDF にレンダリングするスライドのリストを作成し、これらすべてのパラメータを使用して Convert() メソッドを呼び出します。API 呼び出しからの出力ストリームを保存し、ディスクに保存します。
Java REST API を使用して PowerPoint プレゼンテーションを PDF に変換するコード
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.ExportFormat; | |
import com.aspose.slides.model.ExportOptions; | |
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.Arrays; | |
public class Example_PresentationToPdf { | |
protected static SlidesApi presentationApi; | |
public Example_PresentationToPdf() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void PresentationToPdf() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String outputPdfName = "Sample.pdf"; | |
File pdfile = presentationApi.convert(readFileToByteArray(localPath + fileName), ExportFormat.PDF,null, null, null, | |
Arrays.asList( 1, 3,4,9), new ExportOptions()); | |
// Copy the downloaded PDF to the local directory | |
copyFile(pdfile, new File(localPath, outputPdfName)); | |
System.out.println("Presentation converted to PDF and copied to: " + localPath + outputPdfName); | |
} | |
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 を PDF に保存する方法を示しています。エクスポート形式には、PPS、PPSX、PPTM、PPSM、SWF など、他の多くの形式が含まれます。ソース プレゼンテーションの元の順序に関係なく、配列内のスライドの順序を任意に設定できます。
この記事では、スライドを PDF にエクスポートする方法を説明しました。プレゼンテーションを最初から作成するには、Java REST API でプレゼンテーションを作成する の記事を参照してください。