إضافة صورة إلى PowerPoint باستخدام Java REST API

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

متطلب أساسي

خطوات إضافة صورة في PowerPoint باستخدام واجهة برمجة التطبيقات المستندة إلى Java

  1. قم بإنشاء كائن SlidesApi لإدراج صورة في شريحة
  2. قم بتحميل ملف العرض التقديمي المستهدف حيث سيتم إضافة الصورة
  3. قم بإعداد بيانات الصورة بالتنسيق المطلوب
  4. إنشاء كائن PictureFrame لوضعه في الشريحة
  5. اتصل بطريقة CreateShape لإدراج الصورة في شريحة معينة
  6. قم بتنزيل الملف بعد إضافة صورة إليه

تلخص هذه الخطوات كيفية إضافة صورة إلى PowerPoint باستخدام واجهة Java REST. قم بتحميل العرض التقديمي المصدر إلى وحدة التخزين السحابية، وقم بإعداد الصورة باستخدام طريقة ToBase64String() في مساحة اسم Convert، واستخدم هذه الصورة لإنشاء كائن PictureFrame. أخيرًا، اتصل بطريقة CreateShape() لإضافة الصورة إلى الشريحة المحددة وتنزيل العرض التقديمي المحدث.

كود إضافة الصور إلى شرائح PowerPoint باستخدام 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);
}
}

يوضح هذا الكود النموذجي كيفية وضع صورة على PowerPoint باستخدام خدمة Java RESTful. يمكنك تحديد موضع بداية الصورة عن طريق تعيين خصائص X وY في كائن PictureFrame، وتحديد وضع ملء الصورة باستخدام كائن فئة PictureFill. تحدد خاصية PictureFrame حجم الصورة على الشريحة بغض النظر عن الحجم الأصلي.

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

 عربي