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)
{
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]);
}
Document srcDoc = new Document("TestInsertChartColumn.docx");
builder.MoveToBookmark("合同正文");
builder.InsertDocument(srcDoc, ImportFormatMode.KeepDifferentStyles);
doc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "合同正文.doc"));
doc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "合同正文.pdf"), Aspose.Words.SaveFormat.Pdf);
}
static void InsertChart()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 432, 252);
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);
}
}
執行結果如圖:
Aspose.Words.dll 17.6.0.0 破解版本下載