Bu makale, Java REST API’li bir sunum tasarım şablonunu kullanarak sunumun nasıl oluşturulacağı konusunda kılavuzluk etmektedir. Java tabanlı bir SDK kullanarak Java RESTful Hizmeti ile slayt şablonunu kullanarak bir PowerPoint sunumu oluşturmayı öğreneceksiniz. Ayrıca şablonları doldurmak için XML komut dosyasının oluşturulmasını da açıklayacaktır.
Önkoşul
İndirmek Aspose.Slides Cloud SDK for Java to create a presentation from a template
Bir tasarım şablonundan sunum oluşturmak için yukarıdaki SDK ile Java projesini kurun
Java REST Arayüzlü Şablonları Kullanarak Sunum Oluşturma Adımları
- İstemci Kimliği ve gizli anahtarla bir SlidesApi nesnesi oluşturarak ortamı ayarlayın
- Giriş şablonu dosya adını ve çıktı sunumu adını tanımlayın
- UploadFile() yöntemini kullanarak şablonu Bulut depolama alanına yükleyin
- Şablonu doldurmak için XML dosyasını oluşturun veya yükleyin
- XML verilerini kullanmak ve bir sunum oluşturmak için CreatePresentationFromTemplate() yöntemini çağırın
- Yeni oluşturulan sunum dosyasını Could depolama alanından indirin
Bu adımlar Java tabanlı API içeren bir şablon kullanarak sunum oluşturma sürecini özetlemektedir. Şablon dosyasını Bulut depolama alanına yükleyin, şablon için XML komut dosyasını oluşturun veya yükleyin ve çıktı sunumu adını, yüklenen şablon dosyasını ve XML verilerini sağlayarak CreatePresentationFromTemplate() öğesini çağırın. Son olarak, yeni oluşturulan sunumu Bulut depolama alanından indirerek çıktı dosyasını kaydedin.
Java Düşük Kod API’si ile Şablon Kullanarak Sunum Oluşturma Kodu
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardCopyOption; | |
public class Example_CreatePresentationFromDesignTemplate { | |
protected static SlidesApi presentationApi; | |
public Example_CreatePresentationFromDesignTemplate() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void CreatePresentationFromDesignTemplate() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String templateFileName = "TemplatePres.pptx"; | |
String outputFileName = "GeneratedPres.pptx"; | |
String storageFolderName = "TempTests"; | |
String inputData = """ | |
<staff><person> | |
<staffName>Alice Smith</staffName> | |
<address><line1>25 Maple Avenue</line1><line2>New York</line2></address> | |
<phone>+789 654321</phone> | |
<bio>Hello, I'm Alice and this is my resume</bio> | |
<domains> | |
<domain><experience>Python</experience><grade>Expert</grade></domain> | |
<domain><experience>JavaScript</experience><grade>Intermediate</grade></domain> | |
<domain><experience>Ruby</experience><grade>Beginner</grade></domain> | |
</domains> | |
</person></staff> | |
"""; | |
var response = presentationApi.createPresentationFromTemplate( | |
outputFileName, templateFileName, inputData, null, null, false, | |
null, storageFolderName, null); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+outputFileName, null, null); | |
// Copy the downloaded presentation with inserted HTML text to the local directory | |
copyFile(presentationFile, new File(localPath, outputFileName)); | |
System.out.println("Presentation created from Design Template is copied to: " + localPath + outputFileName); | |
} | |
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 kod, Java REST API’si ile tasarım şablonunu kullanarak bir sunumun nasıl oluşturulacağını göstermiştir. Şablon parola korumalıysa işlev çağrısında parolayı ayarlayın. Ayrıca gerekirse çıktı sunum dosyası için şifre de ayarlayabilirsiniz.
Bu makale bize şablondan sunum oluşturmayı öğretti. Belge özelliklerini görüntülemek için Java REST API ile PowerPoint’te belge özelliklerini görüntüleme makalesine bakın.