Aspose 类库 PPT转PDF PDF转JPEG
1 using Aspose.Pdf; 2 using Aspose.Pdf.Devices; 3 using Aspose.Slides; 4 using System; 5 using System.Drawing.Imaging; 6 using System.IO; 7 using System.Web; 8 9 /// <summary> 10 /// Aspose转换帮助类 11 /// </summary> 12 public partial class zhangbupang_AsposeConverterHelper 13 { 14 /// <summary> 15 /// 将PPT文档转换为PDF的方法 16 /// </summary> 17 /// <param name="total">转换后图片的总数</param> 18 /// <param name="convertFile">要转换的ppt文件(物理路径)</param> 19 /// <param name="dirPath">保存到指定目录(物理路径)</param> 20 /// <param name="ignoreExist">已进行过转换的文件是否要重新转换</param> 21 public static bool PPT2PdfConverter(out int total, string convertFile, string dirPath = null, bool ignoreExist = false) 22 { 23 total = 0; 24 try 25 { 26 Presentation ppt = new Presentation(convertFile); 27 28 string dirName = string.Format("{0}.image", convertFile.Substring(convertFile.LastIndexOf('\\') + 1)); //转换后的图片存放目录 29 dirPath = dirPath ?? HttpContext.Current.Server.MapPath(string.Format("/ConvertFiles/ppt/{0}/", dirName)); 30 31 if (!Directory.Exists(dirPath)) 32 { 33 Directory.CreateDirectory(dirPath); 34 } 35 else 36 { 37 if (ignoreExist) 38 { 39 Directory.Delete(dirPath, true); 40 Directory.CreateDirectory(dirPath); 41 } 42 else 43 { 44 return true; 45 } 46 } 47 48 ppt.Save(string.Format("{0}\\{1}.{2}", dirPath, 1, "pdf"), Aspose.Slides.Export.SaveFormat.Pdf); 49 total++; 50 51 return true; 52 } 53 catch 54 { 55 return false; 56 } 57 } 58 59 /// <summary> 60 /// 将PPT文档转换为图片的方法(先转为pdf,再转为图片) 61 /// </summary> 62 /// <param name="total">转换后图片的总数</param> 63 /// <param name="convertFile">要转换的ppt文件(物理路径)</param> 64 /// <param name="dirPath">保存到指定目录(物理路径)</param> 65 /// <param name="ignoreExist">已进行过转换的文件是否要重新转换</param> 66 public static bool PPT2ImageConverter(out int total, string convertFile, string dirPath, bool ignoreExist = false) 67 { 68 total = 0; 69 try 70 { 71 Presentation ppt = new Presentation(convertFile); 72 dirPath = dirPath + "image/"; 73 //string dirName = string.Format("{0}.image", convertFile.Substring(convertFile.LastIndexOf('\\') + 1)); //转换后的图片存放目录 74 //dirPath = dirPath ?? HttpContext.Current.Server.MapPath(string.Format("/Views/KPIManage/upload/{0}/", dirName)); 75 76 if (!Directory.Exists(dirPath)) 77 { 78 Directory.CreateDirectory(dirPath); 79 } 80 else 81 { 82 if (ignoreExist) 83 { 84 Directory.Delete(dirPath, true); 85 Directory.CreateDirectory(dirPath); 86 } 87 else 88 { 89 return true; 90 } 91 } 92 93 string targetFile = string.Format("{0}\\{1}.{2}", dirPath, 1, "pdf"); 94 //保存为pdf 95 ppt.Save(targetFile, Aspose.Slides.Export.SaveFormat.Pdf); 96 //根据pdf生成图片,然后删除pdf 97 Document pdfDocument = new Document(targetFile); 98 for (var i = 0; i < pdfDocument.Pages.Count; i++) 99 { 100 using (FileStream imageStream = new FileStream(string.Format("{0}\\{1}.png", dirPath, (i + 1)), FileMode.Create)) 101 { 102 Resolution resolution = new Resolution(100); 103 JpegDevice jpegDevice = new JpegDevice(resolution, 70); 104 //convert a particular page and save the image to stream 105 jpegDevice.Process(pdfDocument.Pages[i + 1], imageStream); 106 //close stream 107 imageStream.Close(); 108 total++; 109 } 110 } 111 File.Delete(targetFile); 112 return true; 113 } 114 catch 115 { 116 Directory.Delete(dirPath, true); 117 return false; 118 } 119 } 120 121 /// <summary> 122 /// 将PPTX文档转换为PDF的方法(先转为pdf,再转为图片) 123 /// </summary> 124 /// <param name="total">转换后图片的总数</param> 125 /// <param name="convertFile">要转换的pptx文件(物理路径)</param> 126 /// <param name="dirPath">保存到指定目录(物理路径)</param> 127 /// <param name="ignoreExist">已进行过转换的文件是否要重新转换</param> 128 public static bool PPTX2PDFConverter(out int total, string convertFile, string dirPath = null, bool ignoreExist = false) 129 { 130 total = 0; 131 try 132 { 133 Aspose.Slides.Pptx.PresentationEx ppt = new Aspose.Slides.Pptx.PresentationEx(convertFile); 134 135 string dirName = string.Format("{0}.image", convertFile.Substring(convertFile.LastIndexOf('\\') + 1)); //转换后的图片存放目录 136 dirPath = dirPath ?? HttpContext.Current.Server.MapPath(string.Format("/ConvertFiles/ppt/{0}/", dirName)); 137 138 if (!Directory.Exists(dirPath)) 139 { 140 Directory.CreateDirectory(dirPath); 141 } 142 else 143 { 144 if (ignoreExist) 145 { 146 Directory.Delete(dirPath, true); 147 Directory.CreateDirectory(dirPath); 148 } 149 else 150 { 151 return true; 152 } 153 } 154 155 ppt.Save(string.Format("{0}\\{1}.{2}", dirPath, 1, "pdf"), Aspose.Slides.Export.SaveFormat.Pdf); 156 total++; 157 158 return true; 159 } 160 catch 161 { 162 return false; 163 } 164 } 165 166 /// <summary> 167 /// 将PPTX文档转换为图片的方法(先转为pdf,再转为图片) 168 /// </summary> 169 /// <param name="total">转换后图片的总数</param> 170 /// <param name="convertFile">要转换的pptx文件(物理路径)</param> 171 /// <param name="dirPath">保存到指定目录(物理路径)</param> 172 /// <param name="ignoreExist">已进行过转换的文件是否要重新转换</param> 173 public static bool PPTX2ImageConverter(out int total, string convertFile, string dirPath, bool ignoreExist = false) 174 { 175 total = 0; 176 try 177 { 178 Aspose.Slides.Pptx.PresentationEx ppt = new Aspose.Slides.Pptx.PresentationEx(convertFile); 179 dirPath = dirPath + "image/"; 180 //string dirName = string.Format("{0}.image", convertFile.Substring(convertFile.LastIndexOf('\\') + 1)); //转换后的图片存放目录 181 //dirPath = dirPath ?? HttpContext.Current.Server.MapPath(string.Format("/ConvertFiles/ppt/{0}/", dirName)); 182 183 if (!Directory.Exists(dirPath)) 184 { 185 Directory.CreateDirectory(dirPath); 186 } 187 else 188 { 189 if (ignoreExist) 190 { 191 Directory.Delete(dirPath, true); 192 Directory.CreateDirectory(dirPath); 193 } 194 else 195 { 196 return true; 197 } 198 } 199 200 string targetFile = string.Format("{0}\\{1}.{2}", dirPath, 1, "pdf"); 201 //保存为pdf 202 203 ppt.Save(targetFile, Aspose.Slides.Export.SaveFormat.Pdf); 204 //根据pdf生成图片,然后删除pdf 205 Document pdfDocument = new Document(targetFile); 206 for (var i = 0; i < pdfDocument.Pages.Count; i++) 207 { 208 using (FileStream imageStream = new FileStream(string.Format("{0}\\{1}.png", dirPath, (i + 1)), FileMode.Create)) 209 { 210 //分辨率 resolution 最大1024,有两个参数,x和y,最高都是1024 211 //JpegDevice中的Quality [0-100] 最大100 212 Resolution resolution = new Resolution(960); 213 JpegDevice jpegDevice = new JpegDevice(resolution, 100); 214 //convert a particular page and save the image to stream 215 jpegDevice.Process(pdfDocument.Pages[i + 1], imageStream); 216 //close stream 217 imageStream.Close(); 218 total++; 219 } 220 } 221 File.Delete(targetFile); 222 return true; 223 } 224 catch 225 { 226 Directory.Delete(dirPath, true); 227 return false; 228 } 229 } 230 231 }
以上是自己的源码,不能转载
以下可以参考:java使用Aspose.pdf实现pdf转图片_glj6287的博客-CSDN博客_aspose pdf转图片
看到out变量的时候不太懂什么意思,查了一下,发现一个宝藏解释:【C# 教程系列第 5 篇】什么是 c# 中的 out 参数?_“Allen Su”的博客-CSDN博客_c# out参数
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示