この記事に従って、Java REST API を使用して PDF プレゼンテーションを PowerPoint に変換します。 Java ベースの Cloud SDK を使用して、Java Low Code API を使用して PDF を PowerPoint に挿入するプロセスを学習します。完全な PDF をプレゼンテーションに変換するには、いくつかの API 呼び出しが必要です。
前提条件
ダウンロード Aspose.Slides Cloud SDK for Java for changing a PDF to a PPTX
上記の SDK を使用して Java プロジェクトをセットアップし、PDF をプレゼンテーションのスライドとして追加します
Java ベースの API を使用して PDF を PowerPoint プレゼンテーションに変換する手順
- PDF を PPTX に変換するためのクライアント ID とシークレットを使用して SlidesApi オブジェクトをインスタンス化します。
- ソース PDF ファイルをメモリ ストリームにロードして、PowerPoint プレゼンテーションに変換します。
- 出力プレゼンテーション名とソース PDF ストリームを設定して、ImportFromPdf() メソッドを呼び出します
- PDF ページをスライドとして含むプレゼンテーションをクラウドからダウンロードします
これらの手順では、Java REST API* を使用して *PDF を PowerPoint プレゼンテーションに変換する方法を説明します。この機能を使用できるように SlidesApi オブジェクトを初期化し、ソース PDF ファイルをメモリ ストリームに読み込み、出力プレゼンテーション名と PDF コンテンツを含むストリームを指定して ImportFromPdf() メソッドを呼び出します。上記の出力プレゼンテーション名を使用して、結果のプレゼンテーション ファイルをダウンロードできます。
Java REST API を使用して PDF を PowerPoint にインポートするためのコード
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.Document; | |
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_ConvertPdfToPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_ConvertPdfToPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void convertPdfToPresentation() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String pdfFileName = "ShapeImage.pdf"; | |
String storageFolderName = "TempTests"; | |
Document result = presentationApi.importFromPdf(fileName, readFileToByteArray(localPath + pdfFileName), | |
null, null, storageFolderName, null); // Change PDF to PPTX | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with imported PDF to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("PDF imported to presentation and 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 サービス* を使用して *PDF を PowerPoint に変更する方法を示しています。ソース PDF 内の表を検出し、出力プレゼンテーションでレンダリングするためのオプションを設定できます。ソース PDF ファイルが保護されている場合、そのファイルを開くためのパスワードを設定するオプションも利用できます。
この記事では、Java REST インターフェイス* を使用して *PDF をプレゼンテーションとして保存するプロセスについて説明しました。プレゼンテーションにハイパーリンクを追加する方法については、Java REST API を使用して PowerPoint にハイパーリンクを追加する に関する記事を参照してください。