PPT转化成Image、PPTX、XPS、EMF

最近工作经常用到演示文稿,接触到了一款不错的免费软件—Free Spire.Presentation。使用之后发现这款软件非常轻巧,功能还挺齐全。这款软件的转化功能也是非常不错的,平时遇到的各种转换难题,用短短几行代码就能搞定。现在我跟大家分享一下我的使用心得。

 

有兴趣的朋友可以从E-iceblue官网下载FreeSpire.Presentation使用。下载完成后,请将bin文件夹的.DLL添加作为Visual Studio的引用。

 

将PPT文件转化成Image文件

//Create a presentation document.
Presentation presentation = new Presentation();
//Load the PPT file from disk.
presentation.LoadFromFile("sample.pptx");
// Save the slide to Image.
Image image = presentation.Slides[i].SaveAsImage();
//Save image to file.
String fileName = String.Format("result-img-{0}.png", i);
image.Save(“ToImage”, System.Drawing.Imaging.ImageFormat.Png);
//Launch and view the image.
System.Diagnostics.Process.Start(“ToImage”);

将PPT文件转化成PPTX文件

//Create a presentation document.
Presentation presentation = new Presentation();
//Load the PPT file from disk.
presentation.LoadFromFile("sample.ppt");
//Save the PPT document to PPTX file format.
presentation.SaveToFile("ToPPTX.pptx", FileFormat.Pptx2010);
//Launch and view the resulted PPTX file.
System.Diagnostics.Process.Start("ToPPTX.pptx");

将PPT文件转化成XPS文件

//Save to the XPS file.
ppt.SaveToFile("sample.xps", FileFormat.XPS);

将PPS文件转化成PPTX文件

//Save the PPS document to PPTX file format.
presentation.SaveToFile("ToPPTX.pptx", FileFormat.Pptx2010);

将PPT文件转化成EMF文件

//Save the presentation slide to EMF image.
presentation.Slides[2].SaveAsEMF("result.emf");

PS:我们在之前文章里面曾经谈过将PPT文件转化成PDF文件,在这里就不多作介绍了。


namespace PPT2Img { class Program { [STAThread] static void Main(string[] args) { TaskTimerRecorder taskTimerRecorder = new TaskTimerRecorder(); //确认需要执行的操作 //getinfo:获取总页数 //convert:将指定起始页码转换到指定文件夹 string strActionMode = "getinfo"; //拿到输入文件名 string strPPTFileName = "PPT路径"; //拿到输出图片路径 string strOutputFilePath = "图片输出路径"; //拿到输出图片的名词前缀(后缀就是从0计数的页码) string strPrefix = "输出图片前缀"; //需要抠掉的颜色 uint nColor = 0xffffffff; //需要输出的起始页码 int nBeginPage = 0; //需要输出的结束页码(-1为全部输出) int nEndPage = -1; //宽高 int nWidth = 1920; int nHeight = 1080; if (args.Length > 0) { strActionMode = args[0]; if (args.Length > 1) { strPPTFileName = args[1]; } if (args.Length > 2) { strOutputFilePath = args[2]; if (strActionMode == "getinfo") { Console.WriteLine("工作模式是:" + strActionMode); Console.WriteLine("PPT路径是:" + strPPTFileName); Console.WriteLine("结果输出路径是:" + strOutputFilePath); Console.WriteLine("开始执行"); ESBasic.Office.OfficeScanner scaner = new ESBasic.Office.OfficeScanner(); int nPage = scaner.GetInfo(strPPTFileName); FileStream fs = File.Create(strOutputFilePath + "pagecount.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(nPage.ToString()); sw.Close(); fs.Close(); Console.WriteLine("执行结束"); return; } else if (strActionMode == "convert") { if (args.Length > 3) { strPrefix = args[3]; if (args.Length > 4) { string strColor = args[4]; nColor = uint.Parse(strColor, System.Globalization.NumberStyles.HexNumber); if (args.Length > 5) { string strBeginPage = args[5]; nBeginPage = int.Parse(strBeginPage); if (args.Length > 6) { string strEndPage = args[6]; nEndPage = int.Parse(strEndPage); if (args.Length > 7) { string strWidth = args[7]; nWidth = int.Parse(strWidth); if (args.Length > 8) { string strHeight = args[8]; nHeight = int.Parse(strHeight); //Console.WriteLine("1:工作模式是:" + strActionMode); //Console.WriteLine("2:PPT路径是:" + strPPTFileName); //Console.WriteLine("3:图片输出路径是:" + strOutputFilePath); //Console.WriteLine("4:图片输出前缀是:" + strPrefix); //Console.WriteLine("5:需要抠掉的颜色是:" + nColor.ToString()); //Console.WriteLine("6:输出起始页码是:" + nBeginPage.ToString()); //Console.WriteLine("7:输出结束页码是:" + nEndPage.ToString()); //Console.WriteLine("8:输出宽度是" + nWidth.ToString()); //Console.WriteLine("9:输出高度是" + nHeight.ToString()); //Console.WriteLine("1:" + strActionMode); //Console.WriteLine("2:" + strPPTFileName); //Console.WriteLine("3:" + strOutputFilePath); //Console.WriteLine("4:" + strPrefix); //Console.WriteLine("5:" + nColor.ToString()); //Console.WriteLine("6:" + nBeginPage.ToString()); //Console.WriteLine("7:" + nEndPage.ToString()); //Console.WriteLine("8:" + nWidth.ToString()); //Console.WriteLine("9:" + nHeight.ToString()); ESBasic.Office.OfficeScanner scaner = new ESBasic.Office.OfficeScanner(); int nPage = scaner.Convert(strPPTFileName, strOutputFilePath, strPrefix, nColor, nBeginPage, nEndPage, nWidth, nHeight); FileStream fs = File.Create(strOutputFilePath + "pagecount.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(nPage.ToString()); sw.Close(); fs.Close(); return; } } } } } } Console.WriteLine("1:" + strActionMode); Console.WriteLine("2:" + strPPTFileName); Console.WriteLine("3:" + strOutputFilePath); Console.WriteLine("4:" + strPrefix); Console.WriteLine("5:" + nColor.ToString()); Console.WriteLine("6:" + nBeginPage.ToString()); Console.WriteLine("7:" + nEndPage.ToString()); Console.WriteLine("8:" + nEndPage.ToString()); Console.WriteLine("9:" + nEndPage.ToString()); Console.WriteLine("10"); } } } else { #if false //无参数模式,给一些参数,调试使用 strActionMode = "convert"; strPPTFileName = "C:\\Users\\dujiangtao\\Desktop\\测试2.pptx"; strOutputFilePath = "E:\\output\\"; strPrefix = "ppt"; nColor = 0x000000ff; nWidth = 1920; nHeight = 1080; nColor = uint.Parse("000000ff", System.Globalization.NumberStyles.HexNumber); nBeginPage = 0; nEndPage = -1; ESBasic.Office.OfficeScanner scaner = new ESBasic.Office.OfficeScanner(); int nPage = scaner.Convert(strPPTFileName, strOutputFilePath, strPrefix, nColor, nBeginPage, nEndPage, nWidth, nHeight); FileStream fs = File.Create(strOutputFilePath + "pagecount.txt"); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(nPage.ToString()); sw.Close(); fs.Close(); taskTimerRecorder.Stop(); Console.ReadLine(); #endif } } } }
### 方法一:使用 C# 和 Presentation 类 通过编程方式可以利用 `Presentation` 类来实现 PPT 文件到 PPTX 的转换。具体操作如下: 加载 PowerPoint 演示文稿可以通过创建一个 `Presentation` 对象并调用其 `.Save()` 方法保存为新的格式。以下是核心代码片段[^1]: ```csharp using Spire.Presentation; // 加载原始PPT文件 Presentation presentation = new Presentation(); presentation.LoadFromFile("input.ppt"); // 保存为目标PPTX格式 presentation.SaveToFile("output.pptx", FileFormat.Pptx2010); ``` 这种方法依赖于第三方库(如 Spire.Presentation),能够高效完成单个文件的转换。 --- ### 方法二:VBA 批量处理脚本 对于批量转换需求,可以直接编写 VBA 宏脚本来自动化这一过程。以下是一个完整的宏代码示例[^2]: ```vba Option Explicit Sub ppt2pptx() On Error Resume Next Dim sEveryFile As String, sSourcePath As String, sNewSavePath As String Dim CurPpt As Object ' 设置源路径 (请根据实际环境调整) sSourcePath = "C:\Users\WHJZ\Desktop\3\" sEveryFile = Dir(sSourcePath & "*.ppt") Do While sEveryFile <> "" Set CurPpt = Presentations.Open(sSourcePath & sEveryFile, msoTrue, , msoFalse) ' 构造新文件名 sNewSavePath = VBA.Strings.Replace(sSourcePath & sEveryFile, ".ppt", ".pptx") ' 保存为PPTX格式 CurPpt.SaveAs sNewSavePath, ppSaveAsDefault ' 关闭当前文档 CurPpt.Close SaveChanges:=False ' 继续下一个文件 sEveryFile = Dir Loop Set CurPpt = Nothing End Sub ``` 运行该脚本前,请确保已启用 Microsoft Excel 或 Word 中的开发工具选项卡,并将上述代码粘贴至 Visual Basic 编辑器中执行。 --- ### 方法三:在线工具和服务 除了本地化解决方案外,还有许多免费或付费的在线服务支持一键上传多个 PPT 文档并将其自动转化为现代标准下的 PPTX 版本。例如 iSpring 插件不仅提供了 HTML/PDF 输出功能,还允许用户轻松切换不同版本间的兼容模式[^3]。 注意,在线平台可能涉及隐私泄露风险,因此建议仅限于非敏感数据场景下采用此类手段。 --- ### 方法四:借助 OpenOffice/LibreOffice 及 JODConverter 实现 Java 集成 针对开发者而言,如果希望在服务器端或者跨平台上部署类似的转换逻辑,则可考虑结合 LibreOffice/OpenOffice 应用程序以及相应的桥接组件——比如 JODConverter 来达成目标[^4]。这种方式特别适合企业级应用场合,因为它完全基于开源技术栈构建而成。 基本流程概述如下: 1. 启动监听状态的服务进程; 2. 利用 API 提交待加工的任务队列; 3. 获取最终渲染完毕的结果流返回给客户端展示或存储起来供后续访问。 虽然配置稍显复杂一些,但从长远来看维护成本较低且灵活性更高。 --- ####
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值