Hãy làm theo bài viết này để thêm slide mới vào PowerPoint bằng Java REST API. Bạn sẽ học cách thêm slide PowerPoint bằng Java dựa trên API bằng cách sử dụng Java dựa trên SDK trong Cloud storage. Bài viết sẽ hướng dẫn bạn chèn slide vào chỉ mục bắt buộc trong bản trình bày hiện có.
Điều kiện tiên quyết
Tải về Aspose.Slides Cloud SDK for Java for inserting slides
Thiết lập dự án Java với SDK ở trên để thêm slide
Các bước để thêm một slide bằng Java REST API
- Tạo đối tượng SlidesApi bằng cách thiết lập ID người dùng và bí mật để thêm slide
- Tải bài thuyết trình mục tiêu lên bộ nhớ đám mây để chèn slide
- Gọi phương thức CreateSlide() bằng cách cung cấp tên bản trình bày đã tải lên và chỉ mục trang đích
- Hiển thị URL của tất cả các slide sau khi thêm một slide trống mới
- Tải xuống và lưu bản trình bày đầu ra với một slide bổ sung
Các bước này giải thích cách thêm slide vào PowerPoint bằng Java RESTful Service. Tạo đối tượng SlidesApi với thông tin cần thiết, tải bản trình bày nguồn lên bộ nhớ đám mây và gọi phương thức CreateSlide() với tên tệp đã tải lên và chỉ mục slide mục tiêu.
Mã để thêm một slide vào PowerPoint bằng API dựa trên Java
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.ResourceUri; | |
import com.aspose.slides.model.Slides; | |
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_AddPresentationSlides { | |
protected static SlidesApi presentationApi; | |
public Example_AddPresentationSlides() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
// Add a presentation slide | |
Slides response = presentationApi.createSlide(fileName, null,1, null, storageFolderName, null); | |
for (ResourceUri slide : response.getSlideList()) | |
{ | |
System.out.println(slide.getHref()); | |
} | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new slide added to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide deleted and 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); | |
} | |
} |
Mã này minh họa cách thêm slide vào PowerPoint bằng Java Low Code API. Bạn có thể đặt bí danh bố cục bằng cách sử dụng loại bố cục, chỉ mục hoặc tên slide bố cục cho slide mới. Nếu bản trình bày đã tải lên được bảo vệ bằng mật khẩu, hãy cung cấp mật khẩu khi gọi phương thức CreateSlide.
Bài viết này hướng dẫn chúng ta cách chèn một slide trống. Để xóa một slide, hãy tham khảo bài viết trên xóa slide PowerPoint bằng Java REST API.