Thực hiện theo hướng dẫn này để tìm hiểu cách thêm hình ảnh vào PowerPoint bằng Java REST API. Bạn sẽ học cách thêm hình ảnh vào PowerPoint bằng Java Low Code API bằng cách sử dụng Cloud SDK dựa trên Java. Bài viết này thảo luận về các thuộc tính khác nhau để tùy chỉnh hình ảnh trước khi thêm vào slide.
Điều kiện tiên quyết
Tải về Aspose.Slides Cloud SDK for Java for inserting images into the slides
Thiết lập dự án Java với SDK ở trên để chèn hình ảnh vào slide
Các bước để thêm hình ảnh vào PowerPoint bằng API dựa trên Java
- Khởi tạo đối tượng SlidesApi để chèn hình ảnh vào slide
- Tải lên tệp trình bày mục tiêu nơi hình ảnh sẽ được thêm vào
- Chuẩn bị dữ liệu hình ảnh theo định dạng yêu cầu
- Tạo đối tượng PictureFrame để đặt vào slide
- Gọi phương thức CreateShape để chèn hình ảnh vào một slide cụ thể
- Tải xuống tệp sau khi thêm hình ảnh vào đó
Các bước này tóm tắt cách thêm hình ảnh vào PowerPoint bằng Giao diện REST Java. Tải bản trình bày nguồn lên bộ lưu trữ Đám mây, chuẩn bị hình ảnh bằng phương thức ToBase64String() trong không gian tên Convert và sử dụng hình ảnh này để tạo đối tượng PictureFrame. Cuối cùng, gọi phương thức CreateShape() để thêm hình ảnh vào slide đã chỉ định và tải xuống bản trình bày đã cập nhật.
Mã để thêm hình ảnh vào trang chiếu PowerPoint bằng Java REST API
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; | |
import java.util.Base64; | |
public class Example_AddPictureInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddPictureInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addPictureInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String imageFileName = "ShapeImage.png"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
PictureFrame imageFrame = new PictureFrame(); | |
imageFrame.setX(50.0); | |
imageFrame.setY(50.0); | |
imageFrame.setWidth(350.0); | |
imageFrame.setHeight(250.0); | |
PictureFill pictureFill1 = new PictureFill(); | |
pictureFill1.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH); | |
pictureFill1.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName))); | |
imageFrame.setPictureFillFormat(pictureFill); | |
// Add the image to the third slide of the presentation. | |
ShapeBase shapeResponse = presentationApi.createShape(fileName, 3, imageFrame, null, null, | |
null,storageFolderName, null, null); | |
// Output the URI of the newly added image shape. | |
System.out.println("Image added at: "+ shapeResponse.getSelfUri().getHref()); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new image shape to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide with image shape 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); | |
} | |
} |
Mã mẫu này trình bày cách đưa hình ảnh vào PowerPoint bằng Java RESTful Service. Bạn có thể xác định vị trí bắt đầu của hình ảnh bằng cách thiết lập các thuộc tính X và Y trong đối tượng PictureFrame và xác định chế độ tô hình ảnh bằng cách sử dụng đối tượng lớp PictureFill. Thuộc tính PictureFrame xác định kích thước của hình ảnh trên slide bất kể kích thước ban đầu.
Bài viết này hướng dẫn chúng ta cách thêm hình ảnh. Để thêm ghi chú vào bài thuyết trình, hãy tham khảo bài viết trên Thêm ghi chú vào slide powerpoint bằng Java REST API.