通過使用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 效果如下:
獲取代碼: