需求:ppt上传后转成图片
网上搜索了很多方法,若是要实现ppt直接转图片,需要系统安装Microsoft Office PowerPoint;
考虑到真实情况,不会在服务器安装office,于是搜索了很多办法,只能曲线救国,先将ppt转pdf,再将pdf转图片
目前时长大多在用的第三方的ppt转pdf有:
1:Aspose.Slides 收费,2页免费,其他的会有水印
2:Spire.Presentation 收费 10页免费 限制10页的转换,而超过10页有试用版
以上2种均不满足,于是了解到了【LibreOffice】
LibreOffice和office类似,是一个安装程序,需要先安装,后来以命令的方式运行ppt转pdf;
LibreOffice是免费且开源的,只需要下载支持服务器系统版本进行安装即可
LibreOffice
1:中文官网:【https://zh-cn.libreoffice.org/】
2:下载网址:【https://mirrors.cloud.tencent.com/libreoffice/libreoffice/stable/7.4.5/】
3:下载适用版本,进行安装
实例:
可以使用 OpenOffice / LibreOffice 来将 PPT 转换为 PDF。
OpenOffice 和 LibreOffice 都是免费的办公软件套件,它们支持将 Office文档转换为多种格式,包括将 PPT 转换为 PDF。
以下是使用 LibreOffice 的 API 将 PPT 转换为 PDF 的示例代码:
usingSystem.Diagnostics;
// 构造转换命令
stringofficePath = @"C:\Program Files\LibreOffice\program\soffice.exe"; //LibreOffice 的安装目录
stringinputFile = @"C:\Path\To\Your\Presentation.pptx"; // 需要转换的 PPT 文件
stringoutputFile = @"C:\Path\To\Your\Presentation.pdf"; // 转换后的PDF 文件
stringconversionCommand = $"--headless --convert-to pdf\"{inputFile}\" --outdir\"{Path.GetDirectoryName(outputFile)}\"";
// 启动LibreOffice 并执行转换
ProcessStartInfo psi= new ProcessStartInfo(officePath, conversionCommand)
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true
};
Process process =new Process() { StartInfo = psi };
process.Start();
process.WaitForExit();
上述代码中,将会启动 LibreOffice并执行转换命令,最终将 Presentation.pptx文件转换为 Presentation.pdf。
需要说明的是,上述代码需要安装 LibreOffice,否则可能会出现无法启动LibreOffice 的问题。此外,使用 OpenOffice / LibreOffice 进行 PPT 到 PDF 的转换可能比 COM API 或OpenXML SDK 更耗时,所以建议使用异步方式进行转换并对用户进行进度提示。
测试指令:
LibreOffice默认安装路径【C:\ProgramFiles\LibreOffice\program\】
cmd 进入到以上路径,然后将下面的语句改成自己的文件名称
--headless--convert-to pdf "C:\Program Files\LibreOffice\program\input.pptx" --outdir "D:\ProgramFiles\LibreOffice\program\out.pdf"