/// <summary> /// 转换文件 /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <param name="d"></param> /// <returns></returns> public static bool Convert(string source, string target, AsposeConvertDelegate d) { logger.Info("Convert - source=" + source + ", type=" + FileTypeUtil.GetFileType(source)); if (FileTypeUtil.IsWordFile(source)) { return(WordUtil.ConverToImage(source, target, 300, d)); } if (FileTypeUtil.IsExcelFile(source)) { return(ExcelUtil.ConverToImage(source, target, 300, d)); } if (FileTypeUtil.IsPowerPointFile(source)) { return(PowerpointUtil.ConverToImage(source, target, 1.5F, d)); } if (FileTypeUtil.IsVisioFile(source)) { return(VisioUtil.ConverToImage(source, target, 100, d)); } if (FileTypeUtil.IsPdfFile(source)) { return(PDFUtil.ConverToImage(source, target, 100, d)); } throw new IOException("文件格式当前不支持!"); }
/// <summary> /// Word转为图片 /// </summary> /// <param name="source">word文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="resolution">分辨率</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } Diagram diagram = new Diagram(source); total = diagram.Pages.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", resolution=" + resolution + ", pageCount=" + total); ImageSaveOptions options = new ImageSaveOptions(SaveFileFormat.PNG); options.Resolution = resolution; for (page = 0; page < total; page++) { options.PageIndex = page; using (MemoryStream stream = new MemoryStream()) { diagram.Save(stream, options); Bitmap bitmap = new Bitmap(stream); path = target + "\\" + (page + 1) + "_.png"; bitmap.Save(path, ImageFormat.Png); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return true; }
/// <summary> /// Word转为图片 /// </summary> /// <param name="source">word文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="resolution">分辨率</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d=null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } LoadOptions loadOptions = new LoadOptions(); loadOptions.LoadFormat = LoadFormat.Auto; Document doc = new Document(source, loadOptions); total = doc.PageCount; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", resolution=" + resolution + ", pageCount=" + total); for (page = 0; page < total; page++) { ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png); options.PrettyFormat = false; options.Resolution = resolution; options.PageIndex = page; options.PageCount = 1; path = target + "\\" + (page + 1) + ".png"; doc.Save(path, options); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return true; }
/// <summary> /// OneNote转为图片 - 未开发完 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, float scale, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } Document doc = new Document(source); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", scale=" + scale + ", pageCount=" + total); ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png); opts.PageIndex = 1; doc.Save(target + "\\" + (page + 1) + "_.png", opts); //for (page = 0; page < total; page++) //{ // if (d != null) // { // second = (DateTime.Now - startTime).TotalSeconds; // percent = 0.2 + page * 0.8 / total; // message = "正在转换第" + (page + 1) + "/" + total + "页!"; // d.Invoke(percent, page, total, second, message); // } //} return(true); }
/// <summary> /// PPT转为图片 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, float scale=1.5F, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } LoadOptions loadOptions = new LoadOptions(); loadOptions.LoadFormat = LoadFormat.Auto; Presentation pre = new Presentation(source, loadOptions); total = pre.Slides.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", scale=" + scale + ", pageCount=" + total); for (page = 0; page < total; page++) { Slide slide = (Slide)pre.Slides[page]; Bitmap bitmap = slide.GetThumbnail(scale, scale); path = target + "\\" + (page + 1) + "_.png"; bitmap.Save(path, ImageFormat.Png); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return true; }
public static bool ConverToPdf(List <String> files, string target, AsposeConvertDelegate d = null) { logger.Info("ConverToPdf - files=" + files.Count + " , target=" + target); double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (File.Exists(target)) { FileInfo fi = new FileInfo(target); fi.Delete(); logger.Debug("ConverToPdf - delete - fileName=" + fi.FullName); } Document document = new Document(); total = files.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始添加文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } for (page = 0; page < total; page++) { path = files[page]; Page pdfPage = document.Pages.Insert(page + 1); BitmapImage bitemap = new BitmapImage(); bitemap.BeginInit(); bitemap.StreamSource = new FileStream(path, FileMode.Open); bitemap.EndInit(); logger.Debug("ConverToPdf - PixelWidth=" + bitemap.PixelWidth + ", PixelHeight=" + bitemap.PixelHeight + ", path=" + path); Aspose.Pdf.Rectangle rec = new Aspose.Pdf.Rectangle(0, 0, bitemap.PixelWidth, bitemap.PixelHeight); pdfPage.SetPageSize(bitemap.PixelWidth, bitemap.PixelHeight); pdfPage.AddImage(bitemap.StreamSource, rec, bitemap.PixelWidth, bitemap.PixelHeight, true); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在添加第" + (page + 1) + "/" + total + "个文件!"; d.Invoke(percent, (page + 1), total, second, path, message); } } document.Save(target); return(true); }
/// <summary> /// OneNote转为图片 - 未开发完 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, float scale, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } Document doc = new Document(source); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", scale=" + scale + ", pageCount=" + total); ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png); opts.PageIndex = 1; doc.Save(target + "\\" + (page + 1) + "_.png", opts); //for (page = 0; page < total; page++) //{ // if (d != null) // { // second = (DateTime.Now - startTime).TotalSeconds; // percent = 0.2 + page * 0.8 / total; // message = "正在转换第" + (page + 1) + "/" + total + "页!"; // d.Invoke(percent, page, total, second, message); // } //} return true; }
/// <summary> /// 转换文件 /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <param name="d"></param> /// <returns></returns> public static bool Convert(string source, string target, AsposeConvertDelegate d) { logger.Info("Convert - source=" + source + ", type=" + FileTypeUtil.GetFileType(source)); if (FileTypeUtil.IsWordFile(source)) { return WordUtil.ConverToImage(source, target, 300, d); } if (FileTypeUtil.IsExcelFile(source)) { return ExcelUtil.ConverToImage(source, target, 300, d); } if (FileTypeUtil.IsPowerPointFile(source)) { return PowerpointUtil.ConverToImage(source, target, 1.5F, d); } if (FileTypeUtil.IsVisioFile(source)) { return VisioUtil.ConverToImage(source, target, 100, d); } if(FileTypeUtil.IsPdfFile(source)){ return PDFUtil.ConverToImage(source, target, 100, d); } throw new IOException("文件格式当前不支持!"); }
/// <summary> /// Word转为图片 /// </summary> /// <param name="source">word文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="resolution">分辨率</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } Diagram diagram = new Diagram(source); total = diagram.Pages.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", resolution=" + resolution + ", pageCount=" + total); ImageSaveOptions options = new ImageSaveOptions(SaveFileFormat.PNG); options.Resolution = resolution; for (page = 0; page < total; page++) { options.PageIndex = page; using (MemoryStream stream = new MemoryStream()) { diagram.Save(stream, options); Bitmap bitmap = new Bitmap(stream); path = target + "\\" + (page + 1) + "_.png"; bitmap.Save(path, ImageFormat.Png); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return(true); }
/// <summary> /// Word转为图片 /// </summary> /// <param name="source">word文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="resolution">分辨率</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } LoadOptions loadOptions = new LoadOptions(); loadOptions.LoadFormat = LoadFormat.Auto; Document doc = new Document(source, loadOptions); total = doc.PageCount; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", resolution=" + resolution + ", pageCount=" + total); for (page = 0; page < total; page++) { ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png); options.PrettyFormat = false; options.Resolution = resolution; options.PageIndex = page; options.PageCount = 1; path = target + "\\" + (page + 1) + ".png"; doc.Save(path, options); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return(true); }
public static bool ConverToPdf(string source, string target, string fileType, AsposeConvertDelegate d = null) { if (!Directory.Exists(source)) { throw new DirectoryNotFoundException(); } DirectoryInfo di = new DirectoryInfo(source); List <String> files = new List <string>(); foreach (FileInfo f in di.GetFiles()) { if (FileUtil.GetFileType(f.FullName).Equals(fileType)) { files.Add(f.FullName); } } if (files.Count != 0) { return(ConverToPdf(files, target, d)); } else { return(false); } }
/// <summary> /// Excel转为图片 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } Document document = new Document(source); total = document.Pages.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", resolution=" + resolution + ", pageCount=" + total); for (page = 0; page < total; page++) { path = target + "\\" + (page + 1) + ".png"; using (FileStream imageStream = new FileStream(path, FileMode.Create)) { PngDevice pngDevice = new PngDevice(new Resolution(resolution)); pngDevice.Process(document.Pages[page + 1], imageStream); imageStream.Close(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return(true); }
/// <summary> /// Excel转为图片 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } LoadOptions loadOptions = new LoadOptions(LoadFormat.Auto); Workbook workbook = new Workbook(source, loadOptions); total = workbook.Worksheets.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", pageCount=" + total); for (page = 0; page < total; page++) { Worksheet sheet = workbook.Worksheets[page]; ImageOrPrintOptions op = new ImageOrPrintOptions(); op.ImageFormat = ImageFormat.Png; op.HorizontalResolution = resolution; op.VerticalResolution = resolution; SheetRender sr = new SheetRender(sheet, op); for (int j = 0; j < sr.PageCount; j++) { Bitmap bitmap = sr.ToImage(j); path = target + "\\" + (page + 1) + "_" + (j + 1) + ".png"; bitmap.Save(path); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } } return(true); }
/// <summary> /// PPT转为图片 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, float scale = 1.5F, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } LoadOptions loadOptions = new LoadOptions(); loadOptions.LoadFormat = LoadFormat.Auto; Presentation pre = new Presentation(source, loadOptions); total = pre.Slides.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", scale=" + scale + ", pageCount=" + total); for (page = 0; page < total; page++) { Slide slide = (Slide)pre.Slides[page]; Bitmap bitmap = slide.GetThumbnail(scale, scale); path = target + "\\" + (page + 1) + "_.png"; bitmap.Save(path, ImageFormat.Png); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } return(true); }
/// <summary> /// Excel转为图片 /// </summary> /// <param name="source">源文件路径</param> /// <param name="target">图片保存的文件夹路径</param> /// <param name="dpi">dpi</param> /// <param name="format">图片格式</param> public static bool ConverToImage(string source, string target, int resolution = 300, AsposeConvertDelegate d = null) { double percent = 0.0; int page = 0; int total = 0; double second = 0; string path = ""; string message = ""; DateTime startTime = DateTime.Now; if (!FileUtil.CreateDirectory(target)) { throw new DirectoryNotFoundException(); } if (!File.Exists(source)) { throw new FileNotFoundException(); } if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.1; message = "正在解析文件!"; d.Invoke(percent, page, total, second, path, message); } LoadOptions loadOptions = new LoadOptions(LoadFormat.Auto); Workbook workbook = new Workbook(source, loadOptions); total = workbook.Worksheets.Count; if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2; message = "开始转换文件,共" + total + "页!"; d.Invoke(percent, page, total, second, path, message); } logger.Info("ConverToImage - source=" + source + ", target=" + target + ", pageCount=" + total); for (page = 0; page < total; page++) { Worksheet sheet = workbook.Worksheets[page]; ImageOrPrintOptions op = new ImageOrPrintOptions(); op.ImageFormat = ImageFormat.Png; op.HorizontalResolution = resolution; op.VerticalResolution = resolution; SheetRender sr = new SheetRender(sheet, op); for (int j = 0; j < sr.PageCount; j++ ) { Bitmap bitmap = sr.ToImage(j); path = target + "\\" + (page + 1) + "_" + (j + 1) + ".png"; bitmap.Save(path); if (d != null) { second = (DateTime.Now - startTime).TotalSeconds; percent = 0.2 + (page + 1) * 0.8 / total; message = "正在转换第" + (page + 1) + "/" + total + "页!"; d.Invoke(percent, (page + 1), total, second, path, message); } } } return true; }