قم بإنشاء أشكال مخصصة في PowerPoint باستخدام Java REST API

اتبع هذه المقالة لإنشاء أشكال مخصصة في PowerPoint باستخدام Java REST API. سوف تتعلم كيفية إنشاء وإضافة أشكال مخصصة لبرنامج PowerPoint باستخدام Java RESTful Service تلقائيًا. فهو يشارك جميع التفاصيل لإنشاء شكل وتعيين معلماته وإضافتها إلى شريحة معينة.

الشرط الأساسي

خطوات إضافة شكل PPT باستخدام Java REST API

  1. قم بإنشاء كائن SlidesApi وقم بتحميل العرض التقديمي المصدر لإضافة الأشكال إليه
  2. حدد أن نوع الشريحة المراد تعديلها هو شريحة رئيسية
  3. قم بإنشاء كائن شكل جديد بخصائص محددة
  4. أضف الشكل الجديد إلى الشريحة المحددة باستخدام طريقة CreateSpecialSlideShape().
  5. قم بتنزيل ملف العرض التقديمي المحدث بالشكل الجديد بداخله

تشرح هذه الخطوات كيفية إنشاء أشكال العرض التقديمي باستخدام Java REST API. قم بإنشاء كائن SlidesApi، وقم بتحميل العرض التقديمي إلى وحدة التخزين السحابية، وحدد نوع الشريحة المراد تعديلها، وقم بإنشاء كائن الشكل باستخدام المعلمات المطلوبة. أضف الشكل إلى الشريحة الرئيسية وقم بتنزيل العرض التقديمي المحدث بالشكل الجديد بداخله.

كود لإضافة شكل لـ PPT مع واجهة Java REST

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;
public class Example_CreateCustomShapeInPresentation {
protected static SlidesApi presentationApi;
public Example_CreateCustomShapeInPresentation() {
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);
// Specify that the slide type to modify is a Master Slide
SpecialSlideType slideType = SpecialSlideType.MASTERSLIDE;
// Create a new shape object with specific properties
Shape dto = new Shape();
dto.setX(100.0);
dto.setY(100.0);
dto.setWidth(500.0);
dto.setHeight(200.0);
dto.setShapeType(GeometryShape.ShapeTypeEnum.RECTANGLE);
dto.setText("New shape");
// Add the new shape to the specified slide (master slide at index 1) and retrieve the created shape
Shape shape = (Shape)presentationApi.createSpecialSlideShape(fileName, 1, slideType, dto, null, null,
null,storageFolderName, null, null );
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);
}
}

يوضح هذا الرمز كيفية التعامل مع أشكال الشرائح باستخدام Java RESTful Service. يمكنك إضافة أي نوع من الأشكال باستخدام العداد GeometryShape.ShapeTypeEnum بما في ذلك الخطوط والمثلث والمستطيل والماس وما إلى ذلك. ويتوفر الخيار أيضًا لتحديد أنواع خاصة أخرى من الشرائح مثل LayoutSlide وNotesSlide.

لقد علمتنا هذه المقالة كيفية رسم الأشكال على الشريحة. لإضافة الصور راجع المقالة إضافة صورة إلى PowerPoint باستخدام Java REST API.

 عربي