Word to PNG 图片
using ESBasic; using System.Drawing.Imaging; using System.IO; using System.Drawing; using System; using Aspose.Words.Saving; using Aspose.Words; using Aspose.Words.Tables; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; namespace Common { public class Word2ImageConverter : ImageConverter { private bool cancelled = false; public event CbGeneric<int, int> ProgressChanged; public event CbGeneric ConvertSucceed; public event CbGeneric<string> ConvertFailed; public void Cancel() { if (this.cancelled) { return; } this.cancelled = true; } public void ConvertToImage(string originFilePath, string imageOutputDirPath) { this.cancelled = false; ConvertToImage(originFilePath, imageOutputDirPath, 0, 1, null, 200); } /// <summary> /// 将Word文档转换为图片 /// </summary> /// <param name="wordInputPath">Word文件路径</param> /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为Word所在路径</param> /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param> /// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param> /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param> private void ConvertToImage(string wordInputPath, string imageOutputDirPath, int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution) { try { Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath); if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); } if (imageOutputDirPath.Trim().Length == 0) { imageOutputDirPath = Path.GetDirectoryName(wordInputPath); } if (!Directory.Exists(imageOutputDirPath)) { Directory.CreateDirectory(imageOutputDirPath); } 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(wordInputPath); ImageSaveOptions imageSaveOptions = new ImageSaveOptions(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 imgPath = Path.Combine(imageOutputDirPath, imageName) + "." + imageFormat.ToString(); doc.Save(stream, imageSaveOptions); Image img = Image.FromStream(stream); Bitmap bm = ESBasic.Helpers.ImageHelper.Zoom(img, 0.6f); bm.Save(imgPath, imageFormat); img.Dispose(); stream.Dispose(); bm.Dispose(); System.Threading.Thread.Sleep(200); if (this.ProgressChanged != null) { this.ProgressChanged(i - 1, endPageNum); } } if (this.cancelled) { return; } if (this.ConvertSucceed != null) { this.ConvertSucceed(); } } catch (Exception ex) { if (this.ConvertFailed != null) { this.ConvertFailed(ex.Message); } } } public void Receipt(string docpath,List<WordTableData> data) { try { var doc = new Aspose.Words.Document(docpath); var nodeList = doc.GetChildNodes(NodeType.Table, true);//as Aspose.Words.Tables.Table; if (nodeList.Count > 0) { var table = nodeList[0] as Table; if (data.Any()) { data.ForEach(item => { //获取table中的某个单元格,从0开始 Cell lshCell = table.Rows[item.Row].Cells[item.Cell]; //将单元格中的第一个段落移除 lshCell.FirstParagraph.Remove(); //新建一个段落 Paragraph p = new Paragraph(doc); //设置一个string的值 string value = item.Text; p.ParagraphFormat.Style.Font.Size = 16; //设置字体大小 p.ParagraphFormat.Style.Font.Name = "楷体";//设置字体 p.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中显示 //p.ParagraphFormat //把设置的值赋给之前新建的段落 p.AppendChild(new Run(doc, value)); //将此段落加到单元格内 lshCell.AppendChild(p); lshCell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; }); } doc.Save(@"E:\wenjie\收据1.docx"); this.ConvertToImage(@"E:\wenjie\收据1.docx",""); } else { throw new Exception("None Table in WordFile"); } } catch (Exception ex) { throw; } } } public class WordTableData { public int Row { get; set; } public int Cell { get; set; } public string Text { get; set; } } }