在这个简单的主题中,我们将向您演示如何在不依赖 PowerPoint 的情况下使用 C#** 将 SVG 转换为 Presentation。好消息是您不需要第三方软件或 Interop 库即可在 C# 中将 SVG 转换为 PPTX。该应用程序可用于 Windows、Linux 或 macOS 内的任何 .NET 配置环境,前提是示例代码可以在所有 .NET 平台上无缝运行。
在 C# 中将 SVG 导出到 PPTX 的步骤
- 配置您的应用程序以添加来自 NuGet.org 的 Aspose.Slides for .NET 包
- 创建 Presentation 类的实例以创建默认演示文稿
- 访问演示幻灯片集合中的第一张默认幻灯片
- 将 SVG 文件内容作为字符串读取并将其插入到演示图像集合中
- 在已添加 SVG 图像的所选幻灯片中添加相框形状
- 将带有 SVG 图像的演示文稿保存在磁盘上
通过在 C# 中使用上述步骤将 SVG 保存为 PPT 演示文稿可以轻松完成。该过程首先创建 Presentation 类的实例并访问幻灯片集合中的默认第一张幻灯片。然后将 SVG 文件内容作为字符串从磁盘中读取并添加到演示图像集合中的 IPPImage 中。最后,使用添加的 SVG 在幻灯片内部添加一个相框形状,并将演示文稿保存在磁盘上。
在 C# 中将 SVG 转换为 PPTX 的代码
using Aspose.Slides; | |
namespace TestSlides | |
{ | |
public class InsertSVG | |
{ | |
public static void AddSvgToSlide() | |
{ | |
string filesPath = @"/Documents/KnowledgeBase/TestData/"; | |
License license = new License(); | |
license.SetLicense(filesPath + "Conholdate.Total.Product.Family.lic"); | |
//Create a new presentation to insert an SVG image | |
Presentation SvgPresentation = new Presentation(); | |
//Access the first default slide of the presentation | |
ISlide slide = SvgPresentation.Slides[0]; | |
//Load the SVG file content and add that to the presentation image collection | |
var svgContent = System.IO.File.ReadAllText(filesPath + "410.svg"); | |
ISvgImage svgImage = new SvgImage(svgContent); | |
IPPImage ppSVGImage = SvgPresentation.Images.AddImage(svgImage); | |
//Insert the SVG inside a picture frame shape | |
slide.Shapes.AddPictureFrame(ShapeType.Rectangle, 0, 0, ppSVGImage.Width, ppSVGImage.Height, ppSVGImage); | |
//Save the presentation with an SVG image | |
SvgPresentation.Save(filesPath + "PresWithSVG.pptx", Aspose.Slides.Export.SaveFormat.Pptx); | |
} | |
} | |
} |
本教程解释了如何使用 C# 在演示文稿中插入 SVG。如果您想了解如何在 PowerPoint 演示文稿中添加表格,请参阅 如何使用 C# 在 PowerPoint 中创建表格 上的文章。