使用Aspose组件将WORD、PDF、PPT转为图片

  1. 参考资料:http://www.studyofnet.com/news/1268.html



  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using OMCS.Engine.WhiteBoard;  
  6. using ESBasic;  
  7. using System.Drawing.Imaging;  
  8. using System.IO;  
  9. using System.Drawing;  
  10. using Schematrix;  
  11.   
  12. namespace OMCS.Demos.WhiteBoardTest  
  13. {  
  14.     /**//*   
  15.      *  
  16.      * 将pdf、ppf、word转换给图片的组件有很多,这里仅使用Aspose组件(试用版)作为示例。 
  17.      *  
  18.      *  
  19.      */  
  20.   
  21.     图片转换器工厂 -> 将被注入到OMCS的多媒体管理器IMultimediaManager的ImageConverterFactory属性#region 图片转换器工厂 -> 将被注入到OMCS的多媒体管理器IMultimediaManager的ImageConverterFactory属性  
  22.     /**//// <summary>  
  23.     /// 图片转换器工厂。  
  24.     /// </summary>  
  25.     public class ImageConverterFactory : IImageConverterFactory  
  26.     {  
  27.         public IImageConverter CreateImageConverter(string extendName)  
  28.         {  
  29.             if (extendName == ".doc" || extendName == ".docx")  
  30.             {  
  31.                 return new Word2ImageConverter();  
  32.             }  
  33.   
  34.             if (extendName == ".pdf")  
  35.             {  
  36.                 return new Pdf2ImageConverter();  
  37.             }  
  38.   
  39.             if (extendName == ".ppt" || extendName == ".pptx")  
  40.             {  
  41.                 return new Ppt2ImageConverter();  
  42.             }  
  43.   
  44.             if (extendName == ".rar")  
  45.             {  
  46.                 return new Rar2ImageConverter();   
  47.             }  
  48.   
  49.             return null;  
  50.         }  
  51.   
  52.         public bool Support(string extendName)  
  53.         {  
  54.             return extendName == ".doc" || extendName == ".docx" || extendName == ".pdf" || extendName == ".ppt" || extendName == ".pptx" || extendName == ".rar";  
  55.         }  
  56.     }   
  57.     #endregion  
  58.   
  59.     将word文档转换为图片#region 将word文档转换为图片  
  60.     public class Word2ImageConverter : IImageConverter  
  61.     {  
  62.         private bool cancelled = false;  
  63.         public event CbGeneric<intint> ProgressChanged;  
  64.         public event CbGeneric ConvertSucceed;  
  65.         public event CbGeneric<string> ConvertFailed;  
  66.   
  67.         public void Cancel()  
  68.         {  
  69.             if (this.cancelled)  
  70.             {  
  71.                 return;  
  72.             }  
  73.   
  74.             this.cancelled = true;  
  75.         }  
  76.   
  77.         public void ConvertToImage(string originFilePath, string imageOutputDirPath)  
  78.         {  
  79.             this.cancelled = false;  
  80.             ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, null, 200);  
  81.         }  
  82.   
  83.         /**//// <summary>  
  84.         /// 将Word文档转换为图片的方法        
  85.         /// </summary>  
  86.         /// <param name="wordInputPath">Word文件路径</param>  
  87.         /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为Word所在路径</param>        
  88.         /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>  
  89.         /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param>  
  90.         /// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>  
  91.         /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>  
  92.         private void ConvertToImage(string wordInputPath, string imageOutputDirPath, int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution)  
  93.         {  
  94.             try  
  95.             {  
  96.                 Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);  
  97.   
  98.                 if (doc == null)  
  99.                 {  
  100.                     throw new Exception("Word文件无效或者Word文件被加密!");  
  101.                 }  
  102.   
  103.                 if (imageOutputDirPath.Trim().Length == 0)  
  104.                 {  
  105.                     imageOutputDirPath = Path.GetDirectoryName(wordInputPath);  
  106.                 }  
  107.   
  108.                 if (!Directory.Exists(imageOutputDirPath))  
  109.                 {  
  110.                     Directory.CreateDirectory(imageOutputDirPath);  
  111.                 }                 
  112.   
  113.                 if (startPageNum <= 0)  
  114.                 {  
  115.                     startPageNum = 1;  
  116.                 }  
  117.   
  118.                 if (endPageNum > doc.PageCount || endPageNum <= 0)  
  119.                 {  
  120.                     endPageNum = doc.PageCount;  
  121.                 }  
  122.   
  123.                 if (startPageNum > endPageNum)  
  124.                 {  
  125.                     int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;  
  126.                 }  
  127.   
  128.                 if (imageFormat == null)  
  129.                 {  
  130.                     imageFormat = ImageFormat.Png;  
  131.                 }  
  132.   
  133.                 if (resolution <= 0)  
  134.                 {  
  135.                     resolution = 128;  
  136.                 }  
  137.   
  138.                 string imageName = Path.GetFileNameWithoutExtension(wordInputPath);  
  139.                 Aspose.Words.Saving.ImageSaveOptions imageSaveOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png);  
  140.                 imageSaveOptions.Resolution = resolution;  
  141.                 for (int i = startPageNum; i <= endPageNum; i++)  
  142.                 {  
  143.                     if (this.cancelled)  
  144.                     {  
  145.                         break;  
  146.                     }  
  147.   
  148.                     MemoryStream stream = new MemoryStream();  
  149.                     imageSaveOptions.PageIndex = i - 1;  
  150.                     string imgPath = Path.Combine(imageOutputDirPath, imageName) + "_" + i.ToString("000") + "." + imageFormat.ToString();  
  151.                     doc.Save(stream, imageSaveOptions);  
  152.                     Image img = Image.FromStream(stream);  
  153.                     Bitmap bm = ESBasic.Helpers.ImageHelper.Zoom(img, 0.6f);  
  154.                     bm.Save(imgPath, imageFormat);  
  155.                     img.Dispose();  
  156.                     stream.Dispose();  
  157.                     bm.Dispose();  
  158.   
  159.                     System.Threading.Thread.Sleep(200);  
  160.                     if (this.ProgressChanged != null)  
  161.                     {  
  162.                         this.ProgressChanged(i - 1, endPageNum);  
  163.                     }  
  164.                 }  
  165.   
  166.                 if (this.cancelled)  
  167.                 {  
  168.                     return;  
  169.                 }  
  170.   
  171.                 if (this.ConvertSucceed != null)  
  172.                 {  
  173.                     this.ConvertSucceed();  
  174.                 }  
  175.             }  
  176.             catch (Exception ex)  
  177.             {  
  178.                 if (this.ConvertFailed != null)  
  179.                 {  
  180.                     this.ConvertFailed(ex.Message);  
  181.                 }  
  182.             }  
  183.         }  
  184.     }   
  185.     #endregion  
  186.   
  187.     将pdf文档转换为图片#region 将pdf文档转换为图片  
  188.     public class Pdf2ImageConverter : IImageConverter  
  189.     {  
  190.         private bool cancelled = false;  
  191.         public event CbGeneric<intint> ProgressChanged;  
  192.         public event CbGeneric ConvertSucceed;  
  193.         public event CbGeneric<string> ConvertFailed;  
  194.   
  195.         public void Cancel()  
  196.         {  
  197.             if (this.cancelled)  
  198.             {  
  199.                 return;  
  200.             }  
  201.   
  202.             this.cancelled = true;  
  203.         }  
  204.   
  205.         public void ConvertToImage(string originFilePath, string imageOutputDirPath)  
  206.         {  
  207.             this.cancelled = false;  
  208.             ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, 200);  
  209.         }  
  210.   
  211.         /**//// <summary>  
  212.         /// 将pdf文档转换为图片的方法        
  213.         /// </summary>  
  214.         /// <param name="originFilePath">pdf文件路径</param>  
  215.         /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为pdf所在路径</param>         
  216.         /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>  
  217.         /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>         
  218.         /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>  
  219.         private void ConvertToImage(string originFilePath, string imageOutputDirPath, int startPageNum, int endPageNum, int resolution)  
  220.         {  
  221.             try  
  222.             {  
  223.                 Aspose.Pdf.Document doc = new Aspose.Pdf.Document(originFilePath);  
  224.   
  225.                 if (doc == null)  
  226.                 {  
  227.                     throw new Exception("pdf文件无效或者pdf文件被加密!");  
  228.                 }  
  229.   
  230.                 if (imageOutputDirPath.Trim().Length == 0)  
  231.                 {  
  232.                     imageOutputDirPath = Path.GetDirectoryName(originFilePath);  
  233.                 }  
  234.   
  235.                 if (!Directory.Exists(imageOutputDirPath))  
  236.                 {  
  237.                     Directory.CreateDirectory(imageOutputDirPath);  
  238.                 }  
  239.   
  240.                 if (startPageNum <= 0)  
  241.                 {  
  242.                     startPageNum = 1;  
  243.                 }  
  244.   
  245.                 if (endPageNum > doc.Pages.Count || endPageNum <= 0)  
  246.                 {  
  247.                     endPageNum = doc.Pages.Count;  
  248.                 }  
  249.   
  250.                 if (startPageNum > endPageNum)  
  251.                 {  
  252.                     int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;  
  253.                 }  
  254.   
  255.                 if (resolution <= 0)  
  256.                 {  
  257.                     resolution = 128;  
  258.                 }  
  259.   
  260.                 string imageNamePrefix = Path.GetFileNameWithoutExtension(originFilePath);  
  261.                 for (int i = startPageNum; i <= endPageNum; i++)  
  262.                 {  
  263.                     if (this.cancelled)  
  264.                     {  
  265.                         break;  
  266.                     }  
  267.   
  268.                     MemoryStream stream = new MemoryStream();                     
  269.                     string imgPath = Path.Combine(imageOutputDirPath, imageNamePrefix) + "_" + i.ToString("000") + ".jpg";  
  270.                     Aspose.Pdf.Devices.Resolution reso = new Aspose.Pdf.Devices.Resolution(resolution);  
  271.                     Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(reso, 100);  
  272.                     jpegDevice.Process(doc.Pages[i], stream);  
  273.   
  274.                     Image img = Image.FromStream(stream);  
  275.                     Bitmap bm = ESBasic.Helpers.ImageHelper.Zoom(img, 0.6f);  
  276.                     bm.Save(imgPath, ImageFormat.Jpeg);  
  277.                     img.Dispose();  
  278.                     stream.Dispose();  
  279.                     bm.Dispose();  
  280.   
  281.                     System.Threading.Thread.Sleep(200);  
  282.                     if (this.ProgressChanged != null)  
  283.                     {  
  284.                         this.ProgressChanged(i - 1, endPageNum);  
  285.                     }  
  286.                 }  
  287.   
  288.                 if (this.cancelled)  
  289.                 {  
  290.                     return;  
  291.                 }  
  292.   
  293.                 if (this.ConvertSucceed != null)  
  294.                 {  
  295.                     this.ConvertSucceed();  
  296.                 }  
  297.             }  
  298.             catch (Exception ex)  
  299.             {  
  300.                 if (this.ConvertFailed != null)  
  301.                 {  
  302.                     this.ConvertFailed(ex.Message);  
  303.                 }  
  304.             }  
  305.         }  
  306.     }   
  307.     #endregion  
  308.   
  309.     将ppt文档转换为图片#region 将ppt文档转换为图片  
  310.     public class Ppt2ImageConverter : IImageConverter  
  311.     {  
  312.         private Pdf2ImageConverter pdf2ImageConverter;       
  313.         public event CbGeneric<intint> ProgressChanged;  
  314.         public event CbGeneric ConvertSucceed;  
  315.         public event CbGeneric<string> ConvertFailed;  
  316.   
  317.         public void Cancel()  
  318.         {  
  319.             if (this.pdf2ImageConverter != null)  
  320.             {  
  321.                 this.pdf2ImageConverter.Cancel();  
  322.             }  
  323.         }  
  324.   
  325.         public void ConvertToImage(string originFilePath, string imageOutputDirPath)  
  326.         {  
  327.             ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, 200);  
  328.         }  
  329.   
  330.         /**//// <summary>  
  331.         /// 将pdf文档转换为图片的方法        
  332.         /// </summary>  
  333.         /// <param name="originFilePath">ppt文件路径</param>  
  334.         /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为pdf所在路径</param>         
  335.         /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>  
  336.         /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>         
  337.         /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>  
  338.         private void ConvertToImage(string originFilePath, string imageOutputDirPath, int startPageNum, int endPageNum, int resolution)  
  339.         {  
  340.             try  
  341.             {  
  342.                 Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(originFilePath);  
  343.   
  344.                 if (doc == null)  
  345.                 {  
  346.                     throw new Exception("ppt文件无效或者ppt文件被加密!");  
  347.                 }  
  348.   
  349.                 if (imageOutputDirPath.Trim().Length == 0)  
  350.                 {  
  351.                     imageOutputDirPath = Path.GetDirectoryName(originFilePath);  
  352.                 }  
  353.   
  354.                 if (!Directory.Exists(imageOutputDirPath))  
  355.                 {  
  356.                     Directory.CreateDirectory(imageOutputDirPath);  
  357.                 }  
  358.   
  359.                 if (startPageNum <= 0)  
  360.                 {  
  361.                     startPageNum = 1;  
  362.                 }  
  363.   
  364.                 if (endPageNum > doc.Slides.Count || endPageNum <= 0)  
  365.                 {  
  366.                     endPageNum = doc.Slides.Count;  
  367.                 }  
  368.   
  369.                 if (startPageNum > endPageNum)  
  370.                 {  
  371.                     int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;  
  372.                 }  
  373.   
  374.                 if (resolution <= 0)  
  375.                 {  
  376.                     resolution = 128;  
  377.                 }  
  378.   
  379.                 //先将ppt转换为pdf临时文件  
  380.                 string tmpPdfPath = originFilePath + ".pdf";  
  381.                 doc.Save(tmpPdfPath ,Aspose.Slides.Export.SaveFormat.Pdf);  
  382.   
  383.                 //再将pdf转换为图片  
  384.                 Pdf2ImageConverter converter = new Pdf2ImageConverter();  
  385.                 converter.ConvertFailed += new CbGeneric<string>(converter_ConvertFailed);  
  386.                 converter.ConvertSucceed += new CbGeneric(converter_ConvertSucceed);  
  387.                 converter.ProgressChanged += new CbGeneric<intint>(converter_ProgressChanged);  
  388.                 converter.ConvertToImage(tmpPdfPath, imageOutputDirPath);  
  389.   
  390.                 //删除pdf临时文件  
  391.                 File.Delete(tmpPdfPath);  
  392.   
  393.                 if (this.ConvertSucceed != null)  
  394.                 {  
  395.                     this.ConvertSucceed();  
  396.                 }  
  397.             }  
  398.             catch (Exception ex)  
  399.             {  
  400.                 if (this.ConvertFailed != null)  
  401.                 {  
  402.                     this.ConvertFailed(ex.Message);  
  403.                 }  
  404.             }  
  405.   
  406.             this.pdf2ImageConverter = null;  
  407.         }  
  408.   
  409.         void converter_ProgressChanged(int done, int total)  
  410.         {  
  411.             if (this.ProgressChanged != null)  
  412.             {  
  413.                 this.ProgressChanged(done, total);  
  414.             }  
  415.         }  
  416.   
  417.         void converter_ConvertSucceed()  
  418.         {  
  419.             if (this.ConvertSucceed != null)  
  420.             {  
  421.                 this.ConvertSucceed();  
  422.             }  
  423.         }  
  424.   
  425.         void converter_ConvertFailed(string msg)  
  426.         {  
  427.             if (this.ConvertFailed != null)  
  428.             {  
  429.                 this.ConvertFailed(msg);  
  430.             }  
  431.         }  
  432.     }  
  433.     #endregion  
  434.   
  435.     将图片压缩包解压。(如果课件本身就是多张图片,那么可以将它们压缩成一个rar,作为一个课件)#region 将图片压缩包解压。(如果课件本身就是多张图片,那么可以将它们压缩成一个rar,作为一个课件)  
  436.     /**//// <summary>  
  437.     /// 将图片压缩包解压。(如果课件本身就是多张图片,那么可以将它们压缩成一个rar,作为一个课件)  
  438.     /// </summary>  
  439.     public class Rar2ImageConverter : IImageConverter  
  440.     {  
  441.         private bool cancelled = false;  
  442.         public event CbGeneric<string> ConvertFailed;  
  443.         public event CbGeneric<intint> ProgressChanged;  
  444.         public event CbGeneric ConvertSucceed;  
  445.   
  446.         public void Cancel()  
  447.         {  
  448.             this.cancelled = true;  
  449.         }  
  450.   
  451.   
  452.         public void ConvertToImage(string rarPath, string imageOutputDirPath)  
  453.         {  
  454.             try  
  455.             {  
  456.                 Unrar tmp = new Unrar(rarPath);  
  457.                 tmp.Open(Unrar.OpenMode.List);  
  458.                 string[] files = tmp.ListFiles();  
  459.                 tmp.Close();  
  460.   
  461.                 int total = files.Length;  
  462.                 int done = 0;  
  463.   
  464.                 Unrar unrar = new Unrar(rarPath);  
  465.                 unrar.Open(Unrar.OpenMode.Extract);  
  466.                 unrar.DestinationPath = imageOutputDirPath;  
  467.   
  468.                 while (unrar.ReadHeader() && !cancelled)  
  469.                 {  
  470.                     if (unrar.CurrentFile.IsDirectory)  
  471.                     {  
  472.                         unrar.Skip();  
  473.                     }  
  474.                     else  
  475.                     {  
  476.                         unrar.Extract();  
  477.                         ++done;  
  478.   
  479.                         if (this.ProgressChanged != null)  
  480.                         {  
  481.                             this.ProgressChanged(done, total);  
  482.                         }  
  483.                     }  
  484.                 }  
  485.                 unrar.Close();  
  486.   
  487.                 if (this.ConvertSucceed != null)  
  488.                 {  
  489.                     this.ConvertSucceed();  
  490.                 }  
  491.   
  492.             }  
  493.             catch (Exception ex)  
  494.             {  
  495.                 if (this.ConvertFailed != null)  
  496.                 {  
  497.                     this.ConvertFailed(ex.Message);  
  498.                 }  
  499.             }  
  500.         }  
  501.   
  502.           
  503.     }   
  504.     #endregion  
  505. }  
展开阅读全文

没有更多推荐了,返回首页