Chia nhỏ Slide bằng Java REST API

Hãy làm theo bài viết này để chia slide bằng Java REST API. Bạn sẽ học cách tự động chia PPTX bằng Java dựa trên API bằng cách sử dụng Java-based Cloud SDK. Nó chia sẻ tất cả các thuộc tính cần thiết để tùy chỉnh quy trình chuyển đổi.

Điều kiện tiên quyết

Các bước để tách PPT bằng Java Low Code API

  1. Tạo một đối tượng của lớp SlidesApi bằng cách thiết lập ID và bí mật của máy khách
  2. Tải bản trình bày nguồn lên bộ nhớ đám mây bằng cách đặt tên duy nhất
  3. Gọi phương thức Split() bằng cách sử dụng tên tệp đã tải lên, định dạng hình ảnh đầu ra, trang chiếu bắt đầu và số trang chiếu cuối cùng
  4. Phân tích tất cả các slide trong đối tượng phản hồi
  5. Tải xuống từng hình ảnh slide bằng cách sử dụng tên hình ảnh trong chuỗi Href
  6. Lưu hình ảnh đã tải xuống trên đĩa

Các bước này giải thích cách phát triển công cụ chia tách PowerPoint với Giao diện REST Java. Tạo đối tượng SlidesApi, tải tệp trình bày nguồn lên và gọi phương thức Split() bằng cách cung cấp thông tin bắt buộc. Phân tích phản hồi API và lưu tất cả hình ảnh đã tạo vào từng slide trên đĩa cục bộ.

Mã cho PowerPoint Splitter với Java RESTful Service

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_SplitPresentationSlides {
protected static SlidesApi presentationApi;
public Example_SplitPresentationSlides() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void SplitPresentation() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "TestPresentation.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(storageFolderName+localPath + fileName),null);
ExportOptions options = new ExportOptions();
// Split the 2nd and 3rd slides and save them to PNG format.
SplitDocumentResult response = presentationApi.split(fileName, options, SlideExportFormat.PNG,720, 540,2, 3,
storageFolderName, null, storageFolderName, null, null);
for (ResourceUri slide : response.getSlides())
{
System.out.println(slide.getHref());
String imageName = slide.getHref().substring(slide.getHref().lastIndexOf('/') + 1);
File imageFile = presentationApi.downloadFile(storageFolderName + imageName, null, null);
// Copy the downloaded presentation to the local directory
copyFile(imageFile, new File(localPath, imageName));
}
System.out.println("Presentation splitted to image copied to: " + localPath);
}
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);
}
}

Mã này trình bày cách phát triển PowerPoint splitter trực tuyến với Java REST Interface. Phương thức Split() yêu cầu tên tệp đã tải lên, định dạng hình ảnh đầu ra sử dụng trình liệt kê SlideExportFormat, từ index và đến index để xác định phạm vi slide. Href của mỗi mục trong bộ sưu tập slide chứa tham chiếu đến hình ảnh trực tuyến do API tạo ra và có thể được sử dụng để tải xuống hình ảnh.

Bài viết này hướng dẫn chúng ta cách phát triển PPTX splitter với Java RESTful Service. Nếu bạn muốn hợp nhất các bài thuyết trình, hãy tham khảo bài viết trên Hợp nhất các bài thuyết trình với Java REST API.

 Tiếng Việt