Aspose.Words操作word生成PDF文档
Aspose.Words操作word生成PDF文档
using Aspose.Words; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TempAspose.Words.Function { public class WordToPdf { private string _TempPath; //模版路径 private Aspose.Words.Document doc = null; public WordToPdf(string TempPath) { this._TempPath = TempPath; doc = new Aspose.Words.Document(TempPath); //new一个对象 } /// <summary> /// 在书签处插入值 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool InsertValue(string key, string value) { Bookmark bookmark=doc.Range.Bookmarks[key]; //判断是否存在此标签 if (bookmark != null) { bookmark.Text = value; return true; } else { return false; } } /// <summary> /// 在书签处插入图片 /// </summary> /// <param name="key">书签</param> /// <param name="img">图片</param> /// <param name="width">宽</param> /// <param name="height">高</param> /// <returns></returns> public bool InsertImage(string key, string img,double width,double height) { DocumentBuilder dBuilder = new DocumentBuilder(doc); bool pic = dBuilder.MoveToBookmark(key); //判断书签是否存在 if (pic) { dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center; dBuilder.InsertImage(img, width, height); return true; } else { return false; } } #region 插入表格,未测试 public bool InsertDataSet() { try { System.Data.DataSet dataSet = new System.Data.DataSet(); doc.MailMerge.ExecuteWithRegions(dataSet); return true; } catch (Exception ex) { return false; } } public bool InsertTable(string key,System.Data.DataTable dt) { try { DocumentBuilder dBuilder = new DocumentBuilder(doc); bool table = dBuilder.MoveToBookmark(key); dBuilder.StartTable();//开始画Table dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //添加Word表格 dBuilder.InsertCell(); dBuilder.CellFormat.Width = 600.0; dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(115); dBuilder.CellFormat.Borders.LineStyle = LineStyle.None; dBuilder.StartTable();//开始画Table dBuilder.RowFormat.Height = 20.2; dBuilder.InsertCell(); dBuilder.CellFormat.Borders.LineStyle = LineStyle.Single; dBuilder.Font.Size = 10.5; dBuilder.Bold = false; dBuilder.Write("评卷人"); dBuilder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center; dBuilder.CellFormat.Width = 50.0; dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50); dBuilder.RowFormat.Height = 20.0; dBuilder.InsertCell(); dBuilder.CellFormat.Borders.LineStyle = LineStyle.Single; dBuilder.Font.Size = 10.5; dBuilder.Bold = false; dBuilder.Write("得分"); dBuilder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center; dBuilder.CellFormat.Width = 50.0; dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50); dBuilder.EndRow(); dBuilder.RowFormat.Height = 25.0; dBuilder.InsertCell(); dBuilder.RowFormat.Height = 25.0; dBuilder.InsertCell(); dBuilder.EndRow(); dBuilder.EndTable(); dBuilder.InsertCell(); dBuilder.CellFormat.Width = 300.0; dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto; dBuilder.CellFormat.Borders.LineStyle = LineStyle.None; //dBuilder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 //dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left; //dBuilder.Font.Size = 11; //dBuilder.Bold = true; //dBuilder.Write(handText); //dBuilder.EndRow(); //dBuilder.RowFormat.Height = 28; dBuilder.EndTable(); return true; } catch (Exception ex) { return false; } } #endregion /// <summary> /// 保存文件 /// </summary> /// <param name="path">文件路径</param> /// <param name="foemat">文件格式,枚举</param> /// <returns></returns> public bool SaveFile(string path, SaveFormat foemat) { try { doc.Save(path, foemat); return true; } catch (Exception ex) { return false; } } } }