ปฏิบัติตามคำแนะนำนี้เพื่อเรียนรู้วิธี เพิ่มรูปภาพลงใน PowerPoint ด้วย Java REST API คุณจะเรียนรู้วิธี เพิ่มรูปภาพลงใน PowerPoint ด้วย Java Low Code API โดยใช้ Cloud SDK ที่ใช้ Java บทความนี้จะอธิบายคุณสมบัติต่างๆ เพื่อปรับแต่งรูปภาพก่อนจะเพิ่มลงในสไลด์
ข้อกำหนดเบื้องต้น
ดาวน์โหลด Aspose.Slides Cloud SDK for Java for inserting images into the slides
ตั้งค่าโปรเจ็กต์ Java ด้วย SDK ข้างต้นเพื่อแทรกภาพลงในสไลด์
ขั้นตอนการเพิ่มรูปภาพใน PowerPoint โดยใช้ API ที่ใช้ Java
- สร้างอินสแตนซ์ของวัตถุ SlidesApi เพื่อแทรกภาพลงในสไลด์
- อัพโหลดไฟล์นำเสนอเป้าหมายที่จะเพิ่มรูปภาพ
- เตรียมข้อมูลภาพตามรูปแบบที่ต้องการ
- สร้างวัตถุ PictureFrame เพื่อวางในสไลด์
- เรียกใช้เมธอด CreateShape เพื่อแทรกภาพลงในสไลด์ที่ระบุ
- ดาวน์โหลดไฟล์หลังจากเพิ่มรูปภาพแล้ว
ขั้นตอนเหล่านี้สรุปวิธีเพิ่มรูปภาพลงใน PowerPoint ด้วย Java REST Interface อัปโหลดงานนำเสนอต้นฉบับไปยังที่จัดเก็บข้อมูลบนคลาวด์ เตรียมรูปภาพโดยใช้เมธอด ToBase64String() ในเนมสเปซ Convert และใช้รูปภาพนี้เพื่อสร้างอ็อบเจ็กต์ PictureFrame สุดท้าย เรียกใช้เมธอด CreateShape() เพื่อเพิ่มรูปภาพลงในสไลด์ที่ระบุและดาวน์โหลดงานนำเสนอที่อัปเดต
โค้ดสำหรับเพิ่มรูปภาพลงในสไลด์ PowerPoint ด้วย 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); | |
} | |
} |
โค้ดตัวอย่างนี้สาธิตวิธีใส่รูปภาพลงใน PowerPoint โดยใช้ Java RESTful Service คุณสามารถกำหนดตำแหน่งเริ่มต้นของรูปภาพได้โดยตั้งค่าคุณสมบัติ X และ Y ในอ็อบเจ็กต์ PictureFrame และกำหนดโหมดการเติมรูปภาพโดยใช้คลาส PictureFill คุณสมบัติ PictureFrame จะกำหนดขนาดของรูปภาพบนสไลด์โดยไม่คำนึงถึงขนาดเดิม
บทความนี้สอนให้เราเพิ่มรูปภาพ หากต้องการเพิ่มหมายเหตุในงานนำเสนอ โปรดอ่านบทความที่ เพิ่มบันทึกลงในสไลด์ PowerPoint ด้วย Java REST API