ใช้รูปภาพเป็นพื้นหลังใน PowerPoint ด้วย Java REST API

ปฏิบัติตามบทความนี้เพื่อ ใช้รูปภาพเป็นพื้นหลังใน PowerPoint ด้วย Java REST API คุณจะได้เรียนรู้วิธีเปลี่ยน การออกแบบพื้นหลังใน PowerPoint ด้วย Java REST Interface โดยใช้ Cloud SDK ที่ใช้ Java มีการอธิบายตัวเลือกต่างๆ เพื่อปรับแต่งรูปภาพพื้นหลังในสไลด์

ข้อกำหนดเบื้องต้น

ขั้นตอนการตั้งค่าพื้นหลัง PowerPoint ด้วย Java Low Code API

  1. สร้างวัตถุ SlidesApi เพื่อตั้งค่าพื้นหลังของสไลด์
  2. อัปโหลดงานนำเสนอ PowerPoint ต้นฉบับไปยังที่จัดเก็บข้อมูลบนคลาวด์ด้วยชื่อที่ไม่ซ้ำใคร
  3. อ่านข้อมูลไฟล์ภาพลงในอาร์เรย์ไบต์และแปลงเป็นสตริงฐาน 64
  4. สร้างวัตถุ SlideBackground และตั้งค่ารูปแบบการเติมสำหรับการตั้งค่าพารามิเตอร์ภาพพื้นหลัง
  5. เรียกใช้เมธอด SetBackground() เพื่อตั้งค่าพื้นหลังสไลด์ PowerPoint
  6. ดาวน์โหลดงานนำเสนอ PowerPoint ที่อัปเดตหลังจากตั้งค่าพื้นหลัง

ขั้นตอนเหล่านี้อธิบายวิธีการตั้งค่า พื้นหลังสำหรับการนำเสนอ PowerPoint ด้วย API ที่ใช้ Java สร้างอ็อบเจ็กต์ SlidesApi อัปโหลดการนำเสนอไปยังที่เก็บข้อมูลบนคลาวด์ อ่านข้อมูลภาพ แปลงเป็นสตริงฐาน 64 และใช้ในอ็อบเจ็กต์ SlideBackground เพื่อตั้งค่า FillFormat สุดท้าย เรียกใช้เมธอด SetBackground() เพื่อเพิ่มภาพเป็นพื้นหลังและดาวน์โหลดไฟล์เอาต์พุตลงในดิสก์

โค้ดสำหรับเพิ่มพื้นหลัง PPT ด้วย Java Low Code API

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.PictureFill;
import com.aspose.slides.model.SlideBackground;
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_AddPresentationBackgroundImage {
protected static SlidesApi presentationApi;
public Example_AddPresentationBackgroundImage() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void addBackgroundImage() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String imageFileName = "Background.png";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
SlideBackground pictureBackground = new SlideBackground();
PictureFill pictureFill = new PictureFill();//For customization of the background image
pictureFill.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName)));
pictureFill.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH);
pictureBackground.setFillFormat(pictureFill);
// Set slide background image
SlideBackground currentBackground = presentationApi.setBackground(fileName, 1, pictureBackground, null, storageFolderName, null);
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);
// Copy the downloaded presentation with new background image to the local directory
copyFile(presentationFile, new File(localPath, fileName));
System.out.println("Presentation slide background image set 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);
}
}

โค้ดนี้สาธิตวิธีตั้งค่า พื้นหลังสำหรับสไลด์การนำเสนอด้วย Java Low Code API คุณสามารถตั้งค่าคุณสมบัติต่างๆ ของรูปภาพได้ เช่น โหมดเติมภาพ แสงเรืองรอง เงาภายใน เงาภายนอก ขอบนุ่ม และการสะท้อน ให้ระบุรหัสผ่านสำหรับการนำเสนอ PowerPoint ที่อัปโหลดหากได้รับการป้องกัน

บทความนี้สอนให้เราตั้งค่ารูปภาพพื้นหลังสำหรับ PPT ด้วย Java REST Interface หากคุณต้องการคัดลอกสไลด์ภายในงานนำเสนอหรือไปยังงานนำเสนออื่น โปรดอ่านบทความ คัดลอกสไลด์ PowerPoint ด้วย Java REST API

 ไทย