Java REST API ile PowerPoint’e resim eklemeyi öğrenmek için bu kılavuzu izleyin. Java tabanlı bir Cloud SDK kullanarak Java Low Code API ile bir PowerPoint’e resim eklemeyi öğreneceksiniz. Bu makale, resmi slayda eklemeden önce özelleştirmek için çeşitli özellikleri ele almaktadır.
Önkoşul
İndirmek Aspose.Slides Cloud SDK for Java for inserting images into the slides
Yukarıdaki SDK ile bir slayta resim eklemek için Java projesini kurun
Java tabanlı API ile PowerPoint’e Resim Ekleme Adımları
- Bir slayda resim eklemek için SlidesApi nesnesini örneklendirin
- Resmin ekleneceği hedef sunum dosyasını yükleyin
- Görüntü verilerini gerekli formatta hazırlayın
- Bir slayda yerleştirmek için PictureFrame nesnesini oluşturun
- Resmi belirli bir slayda eklemek için CreateShape yöntemini çağırın
- Dosyaya bir resim ekledikten sonra dosyayı indirin
Bu adımlar, Java REST Arayüzü ile PowerPoint’e resim eklemeyi özetler. Kaynak sunumu Bulut depolamasına yükleyin, Convert ad alanındaki ToBase64String() yöntemini kullanarak resmi hazırlayın ve bu resmi kullanarak bir PictureFrame nesnesi oluşturun. Son olarak, belirtilen slayda resim eklemek ve güncellenmiş sunumu indirmek için CreateShape() yöntemini çağırın.
Java REST API ile PowerPoint Slaytlarına Resim Ekleme Kodu
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); | |
} | |
} |
Bu örnek kod, Java RESTful Service ile PowerPoint’e resim koymayı gösterir. PictureFrame nesnesindeki X ve Y özelliklerini ayarlayarak resmin başlangıç konumunu tanımlayabilir ve PictureFill sınıf nesnesini kullanarak resim doldurma modunu tanımlayabilirsiniz. PictureFrame özelliği, orijinal boyuttan bağımsız olarak slayttaki resmin boyutunu tanımlar.
Bu makale bize resim eklemeyi öğretti. Bir sunuma not eklemek için Java REST API ile PowerPoint slaydına notlar ekleyin‘deki makaleye bakın.