使用Aspose.Words元件進行word文件書籤替換,文件、圖表插入,轉pdf等
阿新 • • 發佈:2019-02-10
Program.cs
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace AposeWordDemo
{
class Program
{
static void Main(string[] args)
{
//InsertChart(); //只需執行一次,生成圖表docx
FillFileBookMark();
}
static void FillFileBookMark()
{
Document doc = new Document("template/測試合同.docx");
var dic = new Dictionary<string, string>();
dic.Add("合同編號", "NT20180306");
dic.Add("專案名稱", "通州區數字化經濟系統和智慧合約系統相關功能拓展升級專案" );
dic.Add("委託方", "南通市通州區城市工商局");
dic.Add("受託方", "南通市量子次元科技有限公司");
dic.Add("簽訂時間", DateTime.Now.ToShortDateString());
dic.Add("簽訂地點", "南通市");
dic.Add("有效期限", "兩年");
DocumentBuilder builder = new DocumentBuilder(doc);
//書籤替換
foreach (var key in dic.Keys)
{
builder.MoveToBookmark(key);
builder.Write(dic[key]);
}
//在對應書籤位置插入word文件
Document srcDoc = new Document("TestInsertChartColumn.docx");
builder.MoveToBookmark("合同正文");
builder.InsertDocument(srcDoc, ImportFormatMode.KeepDifferentStyles);
doc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "合同正文.doc"));
//轉換為pdf文件
doc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "合同正文.pdf"), Aspose.Words.SaveFormat.Pdf);
}
/// <summary>
/// 插入圖表
/// </summary>
static void InsertChart()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//新增包含預設資料的圖表。 您可以指定不同的圖表型別和大小
Shape shape = builder.InsertChart(ChartType.Line, 432, 252);
//Shape的Chart屬性包含所有與圖表相關的選項
Chart chart = shape.Chart;
// 獲取圖表系列集
ChartSeriesCollection seriesColl = chart.Series;
//刪除預設生成的系列
seriesColl.Clear();
//建立類別名稱陣列,在此示例中我們有兩個類別
string[] categories = new string[] { "AW Category 1", "AW Category 1" };
//新增新系列。 請注意,資料陣列不能為空,並且陣列的大小必須相同。
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2 });
seriesColl.Add("AW Series 2", categories, new double[] { 3, 4 });
seriesColl.Add("AW Series 3", categories, new double[] { 5, 6 });
seriesColl.Add("AW Series 4", categories, new double[] { 7, 8 });
seriesColl.Add("AW Series 5", categories, new double[] { 9, 10 });
doc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestInsertChartColumn.docx"));
}
}
}
模板
執行結果
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="BtnGenerate" runat="server" Text="生成Doc" OnClick="BtnGenerate_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnGenerate_Click(object sender, EventArgs e)
{
string tmppath = Server.MapPath("~/TestWord.doc");
Document doc = new Document(tmppath); //載入模板
if (doc.Range.Bookmarks["Rhythmk"] != null)
{
Bookmark mark = doc.Range.Bookmarks["Rhythmk"];
mark.Text = "張三公司";
}
doc.Save(Server.MapPath("~/demo.doc"),SaveFormat.Doc); //儲存為doc,並開啟
}
}