[英]Replacing an image using Apache POI is not working
我想使用 Apache POI 替換 PPT 幻燈片中的現有圖像。 但要實現這一點有一些困難。 任何人都可以建議如何解決這個問題,因為我對此很陌生而且我找不到任何可以幫助我的文章?
Aspose.Slides for Java可以輕松地用 PowerPoint 演示文稿中的圖像替換另一個圖像。 以下代碼示例向您展示了如何執行此操作:
// Load a presentation file.
var presentation = new Presentation("input.pptx");
// Add an image to presentation resources.
var imageData = Files.readAllBytes(Paths.get("image.png"));
var newImage = presentation.getImages().addImage(imageData);
// Let's the first shape on the first slide is a picture frame.
var firstSlide = presentation.getSlides().get_Item(0);
var pictureFrame = (IPictureFrame) firstSlide.getShapes().get_Item(0);
// Replace an image with the new one.
pictureFrame.getPictureFormat().getPicture().setImage(newImage);
// Save the presentation.
presentation.save("output.pptx", SaveFormat.Pptx);
presentation.dispose();
或者,您可以使用Aspose.Slides Cloud SDK for Java ,它提供基於 REST 的 API 來管理演示文稿。 下面的代碼示例向您展示了如何使用 Aspose.Slides Cloud 更新演示文稿中的圖像:
var slidesApi = new SlidesApi("my_client_id", "my_client_secret");
var fileName = "example.pptx";
var slideIndex = 1;
var shapeIndex = 1;
// Get image data as a Base64 string.
var imageData = Files.readAllBytes(Paths.get("image.png"));
var imageBase64String = Base64.getEncoder().encodeToString(imageData);
// Get a picture frame.
var pictureFrame = (PictureFrame)slidesApi.getShape(fileName, slideIndex, shapeIndex, null, null, null);
// Update the image data.
pictureFrame.setPictureFillFormat(new PictureFill());
pictureFrame.getPictureFillFormat().setBase64Data(imageBase64String);
// Update the picture frame.
slidesApi.updateShape(fileName, slideIndex, shapeIndex, pictureFrame, null, null, null);
我在 Aspose 擔任支持開發人員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.