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 /// 
 10 /// Aspose转换帮助类
 11 /// 
 12 public partial class zhangbupang_AsposeConverterHelper
 13 {
 14     /// 
 15     /// 将PPT文档转换为PDF的方法
 16     /// 
 17     /// 转换后图片的总数
 18     /// 要转换的ppt文件(物理路径)
 19     /// 保存到指定目录(物理路径)
 20     /// 已进行过转换的文件是否要重新转换
 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     /// 
 60     /// 将PPT文档转换为图片的方法(先转为pdf,再转为图片)
 61     /// 
 62     /// 转换后图片的总数
 63     /// 要转换的ppt文件(物理路径)
 64     /// 保存到指定目录(物理路径)
 65     /// 已进行过转换的文件是否要重新转换
 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     /// 
122     /// 将PPTX文档转换为PDF的方法(先转为pdf,再转为图片)
123     /// 
124     /// 转换后图片的总数
125     /// 要转换的pptx文件(物理路径)
126     /// 保存到指定目录(物理路径)
127     /// 已进行过转换的文件是否要重新转换
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     /// 
167     /// 将PPTX文档转换为图片的方法(先转为pdf,再转为图片)
168     /// 
169     /// 转换后图片的总数
170     /// 要转换的pptx文件(物理路径)
171     /// 保存到指定目录(物理路径)
172     /// 已进行过转换的文件是否要重新转换
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
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参数