C# Asp.Net 实现PPT/PDF转成图片(不依赖office)
C# Asp.Net 实现PPT/PDF转成图片(不依赖office)
/// <summary> /// 将PPT转换为图片 /// </summary> /// <param name="pptPath"></param> /// <param name="imgPath"></param> public static List<string> UploadPptImage(HttpRequestBase request) { var imgUrls = new List<string>(); var file = request.Files["ppt_file"]; if (string.IsNullOrEmpty(file.FileName)) { return new List<string>(); } var path = AppDomain.CurrentDomain.BaseDirectory + "imagesfromppt/" + file.FileName; var savepath = AppDomain.CurrentDomain.BaseDirectory + "ppt/"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } if (!System.IO.Directory.Exists(savepath)) { System.IO.Directory.CreateDirectory(savepath); } var filepath = Path.Combine(savepath, file.FileName); file.SaveAs(filepath); Microsoft.Office.Interop.PowerPoint.Application application = null; Presentation persentation = null; var imagPathList = new List<string>(); try { application = new Microsoft.Office.Interop.PowerPoint.Application(); persentation = application.Presentations.Open(filepath); //persentation.Slides[1].Export(path + "\\page" + 1 + ".jpg", "JPG", 800, 600); for (var k = 1; k <= persentation.Slides.Count; k++) { var imgPath = path + "\\page" + k + ".jpg"; imagPathList.Add(imgPath); persentation.Slides[k].Export(imgPath, "JPG", 800, 600); } imagPathList.ForEach(p => { using (MemoryStream ms = new MemoryStream()) { using (var fs = new FileStream(p, FileMode.Open)) { byte[] buffer = new byte[1024]; int result = 0; while ((result = fs.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, result); } } ms.Seek(0, SeekOrigin.Begin); var fileData = ms.ToArray(); string url = ;//此处为云存储,不重要,上传后获得url imgUrls.Add(url); } }); } catch (Exception ex) { //记录异常 } finally { if (persentation != null) { persentation.Close(); persentation = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); imagPathList.ForEach(p => { if (File.Exists(p)) { File.Delete(p); } }); if (File.Exists(filepath)) { File.Delete(filepath); } if (Directory.Exists(path)) { Directory.Delete(path); } } return imgUrls; }
/// <summary> /// 将PPT转换为图片 /// </summary> /// <param name="pptPath"></param> /// <param name="imgPath"></param> public static List<string> UploadPptImage(HttpRequestBase request) { var imgUrls = new List<string>(); var file = request.Files["ppt_file"]; if (string.IsNullOrEmpty(file.FileName)) { return new List<string>(); } Presentation ppt = new Presentation(); ppt.LoadFromStream(file.InputStream, FileFormat.Auto); var slidescount = ppt.Slides.Count; try { if (slidescount > 0) { for (int i = 0; i < slidescount; i++) { ppt.Slides[i].SaveAsImage();//把ppt转换成emf格式图片 Image image = ppt.Slides[i].SaveAsImage(); using (var ms = new MemoryStream()) { image.Save(ms, ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); var fileData = ms.ToArray(); string url = ;//云存储,返回url imgUrls.Add(url); } } } } catch (Exception ex) { //记录异常 } return imgUrls; }
1、O2S.Components.PDFRender4NET.dll
2、Acrobat.dll
3. PDFLibNet.dll
4. Ghostscript
/// <summary> /// 将PPT转换为图片 /// </summary> /// <param name="pptPath"></param> /// <param name="imgPath"></param> public static List<string> UploadPptImage(HttpRequestBase request) { var imgUrls = new List<string>(); var file = request.Files["ppt_file"]; if (string.IsNullOrEmpty(file.FileName)) { return new List<string>(); } Presentation ppt = new Presentation(file.InputStream); using (var mspdf = new MemoryStream()) { var pageCount = ppt.Slides.Count; ppt.Save(mspdf, Aspose.Slides.Export.SaveFormat.Pdf); using (var pdf = PdfDocument.Load(mspdf)) { try { var pagesizes = pdf.PageSizes; for (int i = 0; i < pdf.PageCount; i++) { Size size = new Size(); size.Height = (int)pagesizes[(i)].Height; size.Width = (int)pagesizes[(i)].Width; using (var image = pdf.Render(i, size.Width, size.Height, PdfRenderFlags.Annotations)) { using (var ms = new MemoryStream()) { image.Save(ms, ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); var fileData = ms.ToArray(); string url = ;//云存储,返回url imgUrls.Add(url); } } } } catch (Exception ex) { //异常记录 } } } return imgUrls; }
速度还可以,没有水印,不需要存储到服务器,基本都是流操作,完美!!!