在这个简单的主题中,我们将向您演示如何使用 Java 将 SVG 转换为 Presentation。该应用程序可以在 Windows、Linux 或 macOS 内的任何 Java 配置环境中使用,以无缝地将 SVG 转换为 Java 中的 PPTX,而不依赖任何第三方软件。
在 Java 中将 SVG 导出到 PPTX 的步骤
- 配置您的应用程序以从存储库管理器添加 Aspose.Slides for Java
- 实例化 Presentation 类的实例以创建默认演示文稿
- 加载演示幻灯片集合中的第一张幻灯片
- 打开并读取 SVG 文件内容并将其插入到演示图像集合中
- 在添加了 SVG 图像的幻灯片中插入相框形状
- 将具有 SVG 图像的演示文稿保存在磁盘上
通过以上步骤Java saving SVG as PPT presentation 可以很容易的进行。该过程是通过使用 Presentation 类实例创建新演示文稿并访问幻灯片集合中的第一张幻灯片来启动的。随后,源 SVG 文件内容被读取并添加到演示图像集合中的 IPPImage 类的实例中。然后将添加的图像插入相框形状内,然后将演示文稿以 PPTX 格式保存在磁盘上。
在 Java 中将 SVG 转换为 PPTX 的代码
import com.aspose.slides.IPPImage; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.ISvgImage; | |
import com.aspose.slides.License; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
import com.aspose.slides.SvgImage; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
public class InsertSVG { | |
public static void main(String[] args) throws Exception{ | |
String filesPath = "/Documents/KnowledgeBase/TestData/"; | |
License svgImportlicense = new License(); | |
svgImportlicense.setLicense(filesPath + "Conholdate.Total.Product.Family.lic"); | |
//Generate a default presentation to insert an SVG image | |
Presentation SvgPresentation = new Presentation(); | |
//Access the first slide of the newly created presentation | |
ISlide slide = SvgPresentation.getSlides().get_Item(0); | |
//Load the SVG file content and add it to the presentation image collection | |
String svgContent = new String(Files.readAllBytes(Paths.get(filesPath + "410.svg"))); | |
ISvgImage svgImage = new SvgImage(svgContent); | |
IPPImage ppSVGImage = SvgPresentation.getImages().addImage(svgImage); | |
//Insert the SVG inside a picture frame shape | |
slide.getShapes().addPictureFrame(ShapeType.Rectangle, 0, 0, ppSVGImage.getWidth(), ppSVGImage.getHeight(), ppSVGImage); | |
//Save the presentation with the SVG image | |
SvgPresentation.save(filesPath + "PresWithSVG.pptx", SaveFormat.Pptx); | |
} | |
} |
通过在 Java SVG 到 PowerPoint 中使用上述代码,可以使用简单的 API 接口轻松执行演示文稿转换。您可以创建新的演示文稿或加载现有的演示文稿,也可以将 SVG 图像插入任何幻灯片内的任何所需位置,同时为图像设置任何自定义大小。源 SVG 图像也可以作为内存流从网络或数据库等来源加载。
本主题涉及如何使用 Java* 在演示文稿中插入 SVG*。如果您有兴趣了解如何在 PowerPoint 演示文稿中添加表格,请参阅 如何使用Java在幻灯片中插入表格 上的文章。