Folgen Sie dieser Anleitung, um zu erfahren, wie Sie mit Java REST API ein Bild zu PowerPoint hinzufügen. Sie erfahren, wie Sie mit Java Low Code API unter Verwendung eines Java-basierten Cloud SDK ein Bild zu PowerPoint hinzufügen. In diesem Artikel werden verschiedene Eigenschaften erläutert, mit denen Sie das Bild anpassen können, bevor Sie es zur Folie hinzufügen.
Voraussetzung
Herunterladen Aspose.Slides Cloud SDK for Java for inserting images into the slides
Richten Sie ein Java-Projekt mit dem oben genannten SDK ein, um ein Bild in eine Folie einzufügen
Schritte zum Hinzufügen eines Bilds in PowerPoint mit einer Java-basierten API
- Instanziieren Sie das SlidesApi-Objekt, um ein Bild in eine Folie einzufügen
- Laden Sie die Zielpräsentationsdatei hoch, in die das Bild eingefügt werden soll
- Bereiten Sie die Bilddaten im erforderlichen Format vor
- Erstellen Sie das PictureFrame-Objekt zum Platzieren in einer Folie
- Rufen Sie die Methode CreateShape auf, um das Bild in eine bestimmte Folie einzufügen
- Laden Sie die Datei herunter, nachdem Sie ein Bild hinzugefügt haben
Diese Schritte fassen zusammen, wie Sie mit der Java REST-Schnittstelle ein Bild zu PowerPoint hinzufügen. Laden Sie die Quellpräsentation in den Cloud-Speicher hoch, bereiten Sie das Bild mit der Methode ToBase64String() im Convert-Namespace vor und verwenden Sie dieses Bild, um ein PictureFrame-Objekt zu erstellen. Rufen Sie abschließend die Methode CreateShape() auf, um das Bild zur angegebenen Folie hinzuzufügen und die aktualisierte Präsentation herunterzuladen.
Code zum Hinzufügen von Bildern zu PowerPoint-Folien mit 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); | |
} | |
} |
Dieser Beispielcode zeigt, wie man mit Java RESTful Service ein Bild in PowerPoint einfügt. Sie können die Startposition des Bildes definieren, indem Sie die X- und Y-Eigenschaften im PictureFrame-Objekt festlegen, und den Bildfüllmodus mithilfe des PictureFill-Klassenobjekts definieren. Die PictureFrame-Eigenschaft definiert die Größe des Bildes auf der Folie unabhängig von der Originalgröße.
In diesem Artikel haben wir gelernt, wie man Bilder hinzufügt. Informationen zum Hinzufügen von Notizen zu einer Präsentation finden Sie im Artikel zu Notizen zu Powerpoint-Folien hinzufügen mit Java REST API.