ทำตามบทความนี้เพื่อ แปลง PDF เป็นงานนำเสนอ PowerPoint ด้วย Java REST API คุณจะได้เรียนรู้กระบวนการ การแทรก PDF ลงใน PowerPoint ด้วย Java Low Code API โดยใช้ Cloud SDK ที่ใช้ Java ต้องใช้การเรียก API สองครั้งเพื่อแปลง PDF ที่สมบูรณ์ให้เป็นงานนำเสนอ
ข้อกำหนดเบื้องต้น
Download Aspose.Slides Cloud SDK for Java for changing a PDF to a PPTX
Setup Java project with the above SDK to add a PDF as a slide in a presentation
Steps for Converting PDF to PowerPoint Presentation with Java-based API
- Instantiate the SlidesApi object using the client ID and secret for converting PDF to a PPTX
- Load the source PDF file into a memory stream for transformation to a PowerPoint presentation
- Invoke the ImportFromPdf() method by setting the output presentation name and source PDF stream
- Download the presentation from the Cloud with PDF pages as slides in it
These steps explain how to convert a PDF to PowerPoint presentation with Java REST API. Initialize the SlidesApi object to work with this feature, load the source PDF file into a memory stream, and call the ImportFromPdf() method by providing the output presentation name and the stream containing the PDF contents. You may download the resultant presentation file using the output presentation name given above.
Code for Importing a PDF into PowerPoint with Java REST API
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); | |
} | |
} |
รหัสนี้ได้สาธิตวิธีการเปลี่ยน PDF เป็น PowerPoint ด้วย Java RESTful Service คุณสามารถตั้งค่าตัวเลือกสำหรับการตรวจหาตารางในไฟล์ PDF ต้นฉบับและแสดงผลในการนำเสนอเอาท์พุตได้ นอกจากนี้ยังมีตัวเลือกสำหรับการตั้งรหัสผ่านสำหรับการเปิดไฟล์ PDF ต้นฉบับหากมีการป้องกัน
บทความนี้ได้สอนเราเกี่ยวกับกระบวนการบันทึก PDF เป็นการนำเสนอด้วย Java REST Interface หากต้องการเพิ่มไฮเปอร์ลิงก์ในงานนำเสนอ โปรดดูบทความเกี่ยวกับ เพิ่มไฮเปอร์ลิงก์ไปยัง PowerPoint ด้วย Java REST API