516 lines
12 KiB
C#
516 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing.Imaging;
|
||
using System.IO;
|
||
using System.Drawing;
|
||
using Image = System.Drawing.Image;
|
||
|
||
namespace APT.Utility
|
||
{
|
||
|
||
public class ConvertImageFile
|
||
{
|
||
public string FileName { get; set; }
|
||
|
||
public string FileType { get; set; }
|
||
|
||
public byte[] FileData { get; set; }
|
||
}
|
||
|
||
public class ConvertImageFileParam
|
||
{
|
||
public ConvertImageFileParam()
|
||
{
|
||
Files = new List<ConvertImageFile>();
|
||
}
|
||
|
||
public string FileName { get; set; }
|
||
public byte[] FileData { get; set; }
|
||
|
||
public List<ConvertImageFile> Files { get; set; }
|
||
}
|
||
|
||
|
||
public interface IAsposeConvert
|
||
{
|
||
bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/*
|
||
*
|
||
* 将pdf、ppf、word转换给图片的组件有很多,这里仅使用Aspose组件(试用版)作为示例。
|
||
*
|
||
* Aspose官网:www.aspose.com, 请支持和购买正版Aspose组件。
|
||
*
|
||
*/
|
||
|
||
#region 图片转换器工厂 -> 将被注入到OMCS的多媒体管理器IMultimediaManager的ImageConverterFactory属性
|
||
|
||
/// <summary>
|
||
/// 图片转换器工厂。
|
||
/// </summary>
|
||
public class ImageConverterFactory
|
||
{
|
||
public IAsposeConvert CreateImageConverter(string fileName)
|
||
{
|
||
var pos = fileName.LastIndexOf(".");
|
||
var extendName = fileName.Substring(pos);
|
||
if (extendName == ".doc" || extendName == ".docx")
|
||
{
|
||
return new Word2ImageConverter();
|
||
}
|
||
//小树增加xls文件类型上传20170329
|
||
if (extendName == ".xls" || extendName == ".xlsx")
|
||
{
|
||
return new Xls2ImageConverter();
|
||
|
||
}
|
||
if (extendName == ".pdf")
|
||
{
|
||
return new Pdf2ImageConverter();
|
||
}
|
||
|
||
if (extendName == ".ppt" || extendName == ".pptx")
|
||
{
|
||
return new Ppt2ImageConverter();
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public bool Support(string extendName)
|
||
{
|
||
//return extendName == ".doc" || extendName == ".docx" || extendName == ".pdf" || extendName == ".ppt" ||
|
||
// extendName == ".pptx" || extendName == ".xls" || extendName == ".xlsx";
|
||
return false;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 将word文档转换为图片
|
||
|
||
public class Word2ImageConverter : IAsposeConvert
|
||
{
|
||
private bool cancelled = false;
|
||
|
||
public void Cancel()
|
||
{
|
||
if (this.cancelled)
|
||
{
|
||
return;
|
||
}
|
||
|
||
this.cancelled = true;
|
||
}
|
||
|
||
public bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list)
|
||
{
|
||
this.cancelled = false;
|
||
return WordConvertToImage(inFileName, inStream, list, 0, 0, null, 200);
|
||
}
|
||
|
||
|
||
|
||
public bool WordConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list,
|
||
int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution)
|
||
{
|
||
try
|
||
{
|
||
//Aspose.Words.Document doc = new Aspose.Words.Document(inStream);
|
||
|
||
//if (doc == null)
|
||
//{
|
||
// return false;
|
||
// //throw new Exception("Word文件无效或者Word文件被加密!");
|
||
//}
|
||
|
||
|
||
//if (startPageNum <= 0)
|
||
//{
|
||
// startPageNum = 1;
|
||
//}
|
||
|
||
//if (endPageNum > doc.PageCount || endPageNum <= 0)
|
||
//{
|
||
// endPageNum = doc.PageCount;
|
||
//}
|
||
|
||
//if (startPageNum > endPageNum)
|
||
//{
|
||
// int tempPageNum = startPageNum;
|
||
// startPageNum = endPageNum;
|
||
// endPageNum = startPageNum;
|
||
//}
|
||
|
||
//if (imageFormat == null)
|
||
//{
|
||
// imageFormat = ImageFormat.Png;
|
||
//}
|
||
|
||
//if (resolution <= 0)
|
||
//{
|
||
// resolution = 128;
|
||
//}
|
||
|
||
//string imageName = Path.GetFileNameWithoutExtension(inFileName);
|
||
//Aspose.Words.Saving.ImageSaveOptions imageSaveOptions =
|
||
// new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png);
|
||
//imageSaveOptions.Resolution = resolution;
|
||
//for (int i = startPageNum; i <= endPageNum; i++)
|
||
//{
|
||
// if (this.cancelled)
|
||
// {
|
||
// break;
|
||
// }
|
||
|
||
// MemoryStream stream = new MemoryStream();
|
||
// imageSaveOptions.PageIndex = i - 1;
|
||
// string imgFileName = imageName + "_" + i.ToString("000") + "." + imageFormat.ToString();
|
||
// doc.Save(stream, imageSaveOptions);
|
||
// Image img = Image.FromStream(stream);
|
||
// Bitmap bm = ImageHelper.Zoom(img, 0.6f);
|
||
|
||
// MemoryStream imgStream = new MemoryStream();
|
||
// bm.Save(imgStream, imageFormat);
|
||
|
||
// imgStream.Position = 0;//重置为开始位置
|
||
// byte[] bytes = new byte[imgStream.Length];
|
||
// imgStream.Read(bytes, 0, bytes.Length);
|
||
|
||
// ConvertImageFile tmpFile = new ConvertImageFile();
|
||
// tmpFile.FileName = imgFileName;
|
||
// tmpFile.FileType = "image/" + imageFormat.ToString().ToLower();
|
||
// tmpFile.FileData = bytes;
|
||
// list.Add(tmpFile);
|
||
// img.Dispose();
|
||
// stream.Dispose();
|
||
// bm.Dispose();
|
||
// imgStream.Dispose();
|
||
// System.Threading.Thread.Sleep(200);
|
||
//}
|
||
//if (this.cancelled)
|
||
//{
|
||
// return false;
|
||
//}
|
||
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("Word转换失败,详情:" + ex.ToString());
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 将pdf文档转换为图片
|
||
|
||
public class Pdf2ImageConverter : IAsposeConvert
|
||
{
|
||
private bool cancelled = false;
|
||
|
||
public void Cancel()
|
||
{
|
||
if (this.cancelled)
|
||
{
|
||
return;
|
||
}
|
||
|
||
this.cancelled = true;
|
||
}
|
||
|
||
public bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list)
|
||
{
|
||
this.cancelled = false;
|
||
return ConvertToImage(inFileName, inStream, list, 0, 0, 200);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 将pdf文档转换为图片的方法
|
||
/// </summary>
|
||
/// <param name="originFilePath">pdf文件路径</param>
|
||
/// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为pdf所在路径</param>
|
||
/// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
|
||
/// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>
|
||
/// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
|
||
private bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list,
|
||
int startPageNum, int endPageNum, int resolution)
|
||
{
|
||
try
|
||
{
|
||
//Aspose.Pdf.Document doc = new Aspose.Pdf.Document(inStream);
|
||
|
||
//if (doc == null)
|
||
//{
|
||
// return false;
|
||
// //throw new Exception("pdf文件无效或者pdf文件被加密!");
|
||
//}
|
||
|
||
|
||
|
||
//if (startPageNum <= 0)
|
||
//{
|
||
// startPageNum = 1;
|
||
//}
|
||
|
||
//if (endPageNum > doc.Pages.Count || endPageNum <= 0)
|
||
//{
|
||
// endPageNum = doc.Pages.Count;
|
||
//}
|
||
|
||
//if (startPageNum > endPageNum)
|
||
//{
|
||
// int tempPageNum = startPageNum;
|
||
// startPageNum = endPageNum;
|
||
// endPageNum = startPageNum;
|
||
//}
|
||
|
||
//if (resolution <= 0)
|
||
//{
|
||
// resolution = 128;
|
||
//}
|
||
|
||
//string imageName = Path.GetFileNameWithoutExtension(inFileName);
|
||
//for (int i = startPageNum; i <= endPageNum; i++)
|
||
//{
|
||
// if (this.cancelled)
|
||
// {
|
||
// break;
|
||
// }
|
||
// MemoryStream stream = new MemoryStream();
|
||
// string imgFileName = imageName + "_" + i.ToString("000") + ".jpg";
|
||
// Aspose.Pdf.Devices.Resolution reso = new Aspose.Pdf.Devices.Resolution(resolution);
|
||
// Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(reso, 100);
|
||
// jpegDevice.Process(doc.Pages[i], stream);
|
||
|
||
// Image img = Image.FromStream(stream);
|
||
// Bitmap bm = ImageHelper.Zoom(img, 0.6f);
|
||
// MemoryStream imgStream = new MemoryStream();
|
||
// bm.Save(imgStream, ImageFormat.Jpeg);
|
||
// imgStream.Position = 0;//重置为开始位置
|
||
// byte[] bytes = new byte[imgStream.Length];
|
||
// imgStream.Read(bytes, 0, bytes.Length);
|
||
|
||
|
||
// ConvertImageFile tmpFile = new ConvertImageFile();
|
||
// tmpFile.FileName = imgFileName;
|
||
// tmpFile.FileType = "image/" + ImageFormat.Jpeg.ToString().ToLower();
|
||
// tmpFile.FileData = bytes;
|
||
// list.Add(tmpFile);
|
||
|
||
// img.Dispose();
|
||
// stream.Dispose();
|
||
// bm.Dispose();
|
||
// imgStream.Dispose();
|
||
|
||
// System.Threading.Thread.Sleep(200);
|
||
|
||
//}
|
||
//if (this.cancelled)
|
||
//{
|
||
// return false;
|
||
//}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("PDF转换失败,详情:" + ex.ToString());
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 将ppt文档转换为图片
|
||
|
||
public class Ppt2ImageConverter : IAsposeConvert
|
||
{
|
||
private Pdf2ImageConverter pdf2ImageConverter;
|
||
|
||
public void Cancel()
|
||
{
|
||
if (this.pdf2ImageConverter != null)
|
||
{
|
||
this.pdf2ImageConverter.Cancel();
|
||
}
|
||
}
|
||
|
||
public bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list)
|
||
{
|
||
return ConvertToImage(inFileName, inStream, list, 0, 0, 200);
|
||
}
|
||
|
||
|
||
private bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list,
|
||
int startPageNum, int endPageNum, int resolution)
|
||
{
|
||
try
|
||
{
|
||
//Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(inStream);
|
||
|
||
//if (doc == null)
|
||
//{
|
||
// return false;
|
||
// //throw new Exception("ppt文件无效或者ppt文件被加密!");
|
||
//}
|
||
|
||
|
||
//if (startPageNum <= 0)
|
||
//{
|
||
// startPageNum = 1;
|
||
//}
|
||
|
||
//if (endPageNum > doc.Slides.Count || endPageNum <= 0)
|
||
//{
|
||
// endPageNum = doc.Slides.Count;
|
||
//}
|
||
|
||
//if (startPageNum > endPageNum)
|
||
//{
|
||
// int tempPageNum = startPageNum;
|
||
// startPageNum = endPageNum;
|
||
// endPageNum = startPageNum;
|
||
//}
|
||
|
||
//if (resolution <= 0)
|
||
//{
|
||
// resolution = 128;
|
||
//}
|
||
//string imageName = Path.GetFileNameWithoutExtension(inFileName);
|
||
////先将ppt转换为pdf临时文件
|
||
//MemoryStream pdfStream = new MemoryStream();
|
||
//doc.Save(pdfStream, Aspose.Slides.Export.SaveFormat.Pdf);
|
||
|
||
////再将pdf转换为图片
|
||
//var converter = new Pdf2ImageConverter();
|
||
//converter.ConvertToImage(imageName + ".pdf", pdfStream, list);
|
||
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("PPT转换失败,详情:" + ex.ToString());
|
||
}
|
||
this.pdf2ImageConverter = null;
|
||
return true;
|
||
}
|
||
|
||
private void converter_ProgressChanged(int done, int total)
|
||
{
|
||
|
||
}
|
||
|
||
private void converter_ConvertSucceed()
|
||
{
|
||
|
||
}
|
||
|
||
private void converter_ConvertFailed(string msg)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
//小树增加Excel文档格式
|
||
#region 将Excel文档转换为图片
|
||
|
||
public class Xls2ImageConverter : IAsposeConvert
|
||
{
|
||
private Pdf2ImageConverter pdf2ImageConverter;
|
||
|
||
public void Cancel()
|
||
{
|
||
if (this.pdf2ImageConverter != null)
|
||
{
|
||
this.pdf2ImageConverter.Cancel();
|
||
}
|
||
}
|
||
|
||
public bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list)
|
||
{
|
||
return ConvertToImage(inFileName, inStream, list, 0, 0, 200);
|
||
}
|
||
|
||
|
||
private bool ConvertToImage(string inFileName, Stream inStream, List<ConvertImageFile> list,
|
||
int startPageNum, int endPageNum, int resolution)
|
||
{
|
||
try
|
||
{
|
||
//Workbook wb = new Workbook(inStream);
|
||
//if (wb == null) return false;
|
||
//MemoryStream pdfStream = new MemoryStream();
|
||
//wb.Save(pdfStream, Aspose.Cells.SaveFormat.Pdf);
|
||
|
||
|
||
////先将excel转换为pdf临时文件
|
||
|
||
//string imageName = Path.GetFileNameWithoutExtension(inFileName);
|
||
////再将pdf转换为图片
|
||
//var converter = new Pdf2ImageConverter();
|
||
//converter.ConvertToImage(imageName + ".pdf", pdfStream, list);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception("Excel转换失败,详情:" + ex.ToString());
|
||
}
|
||
this.pdf2ImageConverter = null;
|
||
return true;
|
||
}
|
||
|
||
private void converter_ProgressChanged(int done, int total)
|
||
{
|
||
}
|
||
|
||
private void converter_ConvertSucceed()
|
||
{
|
||
}
|
||
|
||
private void converter_ConvertFailed(string msg)
|
||
{
|
||
|
||
}
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region
|
||
|
||
public static class ImageHelper
|
||
{
|
||
public static Bitmap Zoom(Image origin, float zoomCoef)
|
||
{
|
||
//IL_0015: Unknown result type (might be due to invalid IL or missing references)
|
||
//IL_001a: Expected O, but got Unknown
|
||
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
|
||
//IL_0021: Expected O, but got Unknown
|
||
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
|
||
Bitmap val = new Bitmap((int)((float)origin.Width * zoomCoef), (int)((float)origin.Height * zoomCoef));
|
||
Graphics val2 = Graphics.FromImage(val);
|
||
try
|
||
{
|
||
val2.DrawImage(origin, new Rectangle(0, 0, val.Width, val.Height));
|
||
}
|
||
finally
|
||
{
|
||
((IDisposable)val2)?.Dispose();
|
||
}
|
||
return val;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
}
|