通过使用Aspose您可以轻松的将您的文档转换成真正的图片格式,最好的保证您的内容将实际可见,与其他格式相比,它并不存在查看工具的安装问题。
准备工作:
1:下载Aspose组件包:http://download.csdn.net/detail/laoge/6931819
编写代码:
核心代码AsposeFileToImg,以下代码在文档页数超过100以上生成会变慢,页数越大生成越慢,在实际使用中请注意。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Reflection; using System.Threading; using System.Drawing; using System.Runtime.InteropServices; using System.Diagnostics; using Aspose.Cells; namespace PdfOrImg { /// <summary> /// 功能: /// pdf,doc,docx,ppt,pptx,xls,xlsx文件转图片 /// 在当前文件下创建一个以文件名命名的文件夹,该文件夹的保存的是该文件生成是页图片 /// </summary> public class AsposeFileToImg { private static string imageDirectoryPath = ""; public static void FileToImg(string filePath) { if (File.Exists(filePath)) { FileInfo pdfFi = new FileInfo(filePath); string filename = pdfFi.Name.Replace(pdfFi.Extension, ""); imageDirectoryPath = System.IO.Path.Combine(pdfFi.DirectoryName, filename); if (!Directory.Exists(imageDirectoryPath)) { Directory.CreateDirectory(imageDirectoryPath); } string a = Path.GetExtension(filePath).ToLower(); if (a == ".pdf") { PdfToImg(filePath); } else { if (a == ".doc" || a == ".docx") { DocToImg(filePath); } else { if (a == ".ppt") { PptToImg(filePath); } else if (a == ".pptx") { PptxToImg(filePath); } else { if (a == ".xls" || a == ".xlsx") { XlsToImg(filePath); } } } } } } private static void PdfToImg(string filePath) { string filename = Path.GetFileNameWithoutExtension(filePath).ToLower(); FileStream docStream = new FileStream(filePath, FileMode.Open); var pdf = new Aspose.Pdf.Document(docStream); for (int i = 1; i < pdf.Pages.Count + 1; i++) { try { string imgpath = imageDirectoryPath + "/" + filename + "_" + i + ".jpg"; if (!File.Exists(imgpath)) { using (FileStream imageStream = new FileStream(imgpath, FileMode.Create)) { Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(300); Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(resolution, 100); jpegDevice.Process(pdf.Pages[i], imageStream); imageStream.Close(); } } } catch (Exception) { } } } private static void DocToImg(string filePath) { string filename = Path.GetFileNameWithoutExtension(filePath).ToLower(); var words = new Aspose.Words.Document(filePath); Aspose.Words.Saving.ImageSaveOptions imageSaveOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Jpeg); imageSaveOptions.Resolution = 128; int pageindex = 0; int pagecount = words.PageCount; for (int i = 1; i < pagecount + 1; i++) { try { string imgpath = imageDirectoryPath + "/" + filename + "_" + i + ".jpg"; if (!File.Exists(imgpath)) { imageSaveOptions.PageIndex = pageindex; words.Save(imgpath, imageSaveOptions); pageindex++; } } catch (Exception) { } } } private static void PptToImg(string filePath) { string filename = Path.GetFileNameWithoutExtension(filePath).ToLower(); var ppt = new Aspose.Slides.Presentation(filePath); string imgpath = imageDirectoryPath + "/" + filename + ".pdf"; if (!File.Exists(imgpath)) { ppt.Save(imgpath, Aspose.Slides.Export.SaveFormat.Pdf); } PdfToImg(imgpath); } private static void PptxToImg(string filePath) { string filename = Path.GetFileNameWithoutExtension(filePath).ToLower(); var ppt = new Aspose.Slides.Pptx.PresentationEx(filePath); string imgpath = imageDirectoryPath + "/" + filename + ".pdf"; if (!File.Exists(imgpath)) { ppt.Save(imgpath, Aspose.Slides.Export.SaveFormat.Pdf); } PdfToImg(imgpath); } private static void XlsToImg(string filePath) { string filename = Path.GetFileNameWithoutExtension(filePath).ToLower(); Workbook book = new Workbook(filePath); //创建一个图表选项的对象 Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions(); imgOptions.ImageFormat = ImageFormat.Jpeg; int count = book.Worksheets.Count; for (int i = 0; i < count; i++) { //获取一张工作表 Worksheet sheet = book.Worksheets[i]; //创建一个纸张底色渲染对象 Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions); for (int j = 0; j < sr.PageCount; j++) { string imgpath = imageDirectoryPath + "/" + filename + "_" + i + "_" + j + "_.jpg"; if (!File.Exists(imgpath)) { sr.ToImage(j, imgpath); } } } } } }
调用代码:
using System; using System.IO; using System.Collections; namespace PdfOrImg { //Adobe Acrobat9.0 class Program { static void Main(string[] args) { Console.WriteLine("========文件转图片开始========"); try { ArrayList alist = new ArrayList(); alist.Add("temp_pdf.pdf"); alist.Add("temp_ppt.ppt"); alist.Add("temp_pptx.pptx"); alist.Add("temp_doc.doc"); alist.Add("temp_docx.docx"); alist.Add("temp_xls.xls"); alist.Add("temp_xlsx.xlsx"); for (int i = 0; i < alist.Count; i++) { try { string pdfFilePath = alist[i].ToString(); FileInfo pdfFi = new FileInfo(pdfFilePath); string filepath = pdfFi.FullName; Console.WriteLine("正在转换" + pdfFilePath + "文件..."); AsposeFileToImg.FileToImg(filepath); } catch (Exception) { } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("========文件转图片结束========"); Console.Read(); } } }
运行情况:
根据文档名称建立同名的文件夹,并在文件夹中存放文档每一页的图片,图片命名格式:文件名_页码.jpg 效果如下:
获取代码: