在 C++ 中将 PowerPoint 幻灯片转换为 PNG

关于 PowerPoint 到 PNG 转换

PNG(可移植网络图形)格式虽然没有 JPEG(联合图像专家组)那么流行,但它依然非常流行。

使用场景: 当您拥有复杂的图像且对尺寸没有要求时,PNG 是比 JPEG 更好的图像格式。

将 PowerPoint 转换为 PNG

按以下步骤操作:

  1. 实例化 Presentation 类。
  2. Presentation::get_Slides() 集合中获取 ISlide 接口下的幻灯片对象。
  3. 使用 ISlide::GetImage() 方法获取每张幻灯片的缩略图。
  4. 使用 IImage::Save(String, ImageFormatPtr 方法将幻灯片缩略图保存为 PNG 格式。

以下 C++ 代码演示了如何将 PowerPoint 演示文稿转换为 PNG:

auto pres = System::MakeObject<Presentation>(u"pres.pptx");
    
for (int32_t index = 0; index < pres->get_Slides()->get_Count(); index++)
{
    auto slide = pres->get_Slides()->idx_get(index);
    auto fileName = String::Format(u"slide_{0}.png", index);
    slide->GetImage()->Save(fileName, ImageFormat::Png);
}

使用自定义尺寸将 PowerPoint 转换为 PNG

如果您希望获得特定比例的 PNG 文件,可以设置 desiredXdesiredY 的值,这些值决定生成的缩略图尺寸。

下面的 C++ 代码演示了上述操作:

auto pres = System::MakeObject<Presentation>(u"pres.pptx");

float scaleX = 2.f;
float scaleY = 2.f;
for (int32_t index = 0; index < pres->get_Slides()->get_Count(); index++)
{
    auto slide = pres->get_Slides()->idx_get(index);
    auto fileName = String::Format(u"slide_{0}.png", index);
    slide->GetImage(scaleX, scaleY)->Save(fileName, ImageFormat::Png);
}

使用自定义大小将 PowerPoint 转换为 PNG

如果您希望获取特定尺寸的 PNG 文件,可以为 ImageSize 传入您偏好的 widthheight 参数。

以下代码展示了在指定图像尺寸的情况下,将 PowerPoint 转换为 PNG 的方法:

auto pres = System::MakeObject<Presentation>(u"pres.pptx");
    
Size size(960, 720);
for (int32_t index = 0; index < pres->get_Slides()->get_Count(); index++)
{
    auto slide = pres->get_Slides()->idx_get(index);
    auto fileName = String::Format(u"slide_{0}.png", index);
    slide->GetImage(size)->Save(fileName, ImageFormat::Png);
}

常见问题

如何仅导出特定形状(例如图表或图片)而不是整张幻灯片?
Aspose.Slides 支持 为单个形状生成缩略图;您可以将形状渲染为 PNG 图像。

服务器上是否支持并行转换?
是的,但请 不要共享 单个演示文稿实例于多个线程。每个线程或进程应使用单独的实例。

导出为 PNG 时试用版有哪些限制?
评估模式会在输出图像上添加水印,并在应用许可证前强制执行 其他限制