In diesem Artikel wird beschrieben, wie man mit der Java REST API SmartArt zu PowerPoint hinzufügt. Sie lernen, PowerPoint SmartArt mit Java Low Code API mithilfe des Java-basierten Cloud SDK automatisch einzufügen. Es werden verschiedene Klassen und Enumeratoren besprochen, um verschiedene Arten von SmartArt in der PowerPoint-Präsentationsfolie zu erstellen.
Voraussetzung
Herunterladen Aspose.Slides Cloud SDK for Java for inserting SmartArt in slides
Richten Sie ein Java-Projekt mit dem oben genannten SDK ein, um eine SmartArt-Grafik zu erstellen
Schritte zum Hinzufügen von PowerPoint Presentation SmartArt mit Java REST API
- Legen Sie die Anmeldeinformationen im SlidesApi-Objekt für die Arbeit mit SmartArt fest
- Laden Sie die Quellpräsentation in den Cloud-Speicher hoch, um intelligente Grafiken einzufügen
- Erstellen Sie Grafikdaten, indem Sie die gewünschten Eigenschaften im SmartArt-Objekt festlegen
- Fügen Sie das SmartArt mit der Methode CreateShape() ein
- Laden Sie die aktualisierte Präsentationsdatei herunter, nachdem Sie SmartArt hinzugefügt haben
In diesen Schritten wird erläutert, wie Sie mit SmartArt für PowerPoint mit Java REST API arbeiten. Erstellen Sie das SlidesApi-Klassenobjekt, laden Sie die Quellpräsentation hoch und erstellen Sie das SmartArt-Objekt mit den angegebenen Einstellungen, einschließlich Position, Größe, Layout, Schnellstil und Farbstil. Abschließend werden der jeweiligen Folie mithilfe der Methode CreateShape() mehrere Knoten für die SmartArt hinzugefügt.
Code zum Hinzufügen intelligenter PowerPoint-Formen mit Java Java-basierter 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.ArrayList; | |
import java.util.List; | |
public class Example_AddSmartArtShapeInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddSmartArtShapeInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addCustomShapeInSlide() 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); | |
// Configure SmartArt properties | |
SmartArt smartArt = new SmartArt(); | |
smartArt.setX(50.0); // Horizontal position of the SmartArt | |
smartArt.setY(50.0); // Horizontal position of the SmartArt | |
smartArt.setWidth(250.0); // Width of the SmartArt | |
smartArt.setHeight(250.0); // Height of the SmartArt | |
smartArt.setLayout(SmartArt.LayoutEnum.BENDINGPICTURESEMITRANSPARENTTEXT); // SmartArt layout | |
smartArt.setQuickStyle(SmartArt.QuickStyleEnum.SIMPLEFILL); // Quick style | |
smartArt.setColorStyle(SmartArt.ColorStyleEnum.COLOREDFILLACCENT1); // Color style | |
List<SmartArtNode> nodes = new ArrayList<SmartArtNode>(); | |
SmartArtNode node1 = new SmartArtNode(); | |
node1.setText("Planning"); | |
nodes.add(node1); | |
SmartArtNode node2 = new SmartArtNode(); | |
node2.setText("Design"); | |
nodes.add(node2); | |
SmartArtNode node3 = new SmartArtNode(); | |
node3.setText("Implementation"); | |
nodes.add(node3); | |
smartArt.setNodes(nodes); | |
// Add SmartArt to the first slide | |
ShapeBase addedSmartArt = presentationApi.createShape(fileName, 1, smartArt, null, null, null, storageFolderName, null, null ); | |
System.out.println("SmartArt added to the presentation successfully."); | |
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 SmartArt 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 Code zeigt, wie man eine Smart Art-Grafik mit Java REST Interface in eine Folie einfügt. Verwenden Sie LayoutEnum, um die gewünschte SmartArt-Form aus einer großen Liste von Werten auszuwählen, darunter AccentProcess, AccentedPicture, ArrowRibbon, BasicPyramid und BasicProcess usw. Ebenso verfügen die schnellen Stil- und Farbstil-Enumeratoren über eine Vielzahl von Optionen zum Anpassen der SmartArt.
In diesem Artikel haben wir gelernt, wie man SmartArt in einer Präsentationsfolie erstellt. Informationen zum Hinzufügen benutzerdefinierter Formen in einer Präsentation finden Sie im Artikel zu Erstellen Sie benutzerdefinierte Formen in PowerPoint mit der Java REST API.