//在WORD文档中添加书签,以下代码自动保存到书签位置
protected void sbtnExportWord_Click(object sender, EventArgs e)
{
string strGoldSignPath = ConfigurationManager.AppSettings["GoldSignPath"];
string Path = System.Web.HttpContext.Current.Server.MapPath("~/");
Path += @"Template";
string TemplatePath = Path + @"\项目开工通知单.doc";
string seed = DateTime.Now.ToString("yyyyMMddHHmmss");
string FilePath = Path + @"\" + seed + ".doc";
Document doc = new Document(TemplatePath); //载入模板
// 打开Word文件进行签名
try
{
foreach (var label in new string[] { "EngineeringName", "EngineeringNumber", "EngneeringScaleID", "EngineeringPrType", "BuildCompany", "BuildPlace", "mutualitySpec", "EngEmpID", "SmartTextBox1", "SmartTextBox2", "ProjSyndicContent", "txtSystemContent", "ProductRequire", "ProgressRequire", "SmartTextBox3", "SmartTextBox4", "SmartTextBox5"/*, "stxtEdit", "stxtAuditor", "stxtSubdecanal"*/ ,"EngineeringJoinDepartments"})
{
Bookmark mark = doc.Range.Bookmarks[label];
if (mark != null && label != "EngineeringJoinDepartments")
mark.Text = (this.FindControl(label) as SmartTextBox).Text;
}
// 签名
int i = 1;
foreach (var label in new string[] { "stxtEdit", "stxtAuditor", "stxtSubdecanal" })
{
string[] strs = (this.FindControl(label) as SmartTextBox).Text.Split(' ');
string value = strs[0];
Bookmark mark = doc.Range.Bookmarks[label];
if (mark != null)
{
string signPicPath = string.Format(@"{0}{1}.png", strGoldSignPath, value);
if (File.Exists(signPicPath))
{
InsertPicByPath(doc, signPicPath, value, label, "");
}
Bookmark markd = doc.Range.Bookmarks["date" + i];
if (value != "")
markd.Text = strs[2];
}
i += 1;
}
}
catch
{
}
finally
{
//doc.Save(FilePath);
Aspose.Words.Saving.DocSaveOptions docoptions = new Aspose.Words.Saving.DocSaveOptions(SaveFormat.Doc);
doc.Save(HttpContext.Current.Response, "项目开工通知单.doc", Aspose.Words.ContentDisposition.Attachment, docoptions);
}
}
//签图片
private static void InsertPicByPath(Aspose.Words.Document doc, string Path, string EmpName, string type, string info, int bit = 0)
{
Aspose.Words.DocumentBuilder builder;
builder = new Aspose.Words.DocumentBuilder(doc);
string path = Path;
if (path != "" && File.Exists(path))
{
Aspose.Words.Drawing.Shape shape = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
shape.ImageData.SetImage(path);
shape.Width = 40;
shape.Height = 20;
CompositeNode node = shape.ParentNode;
builder.MoveToBookmark(type);
builder.InsertNode(shape);
}
else
{
//builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToBookmark(type);
builder.Write(EmpName);
}
if (bit == 0)
{
builder = new Aspose.Words.DocumentBuilder(doc);
builder.MoveToBookmark(type + "Info");
builder.Write(info);
}
}