c#使用Aspose实现Word域套打

前言

在项目开发过程中我们经常会使用word书签替换功能导出一些模板类的文档,可能很多人都不知道word还有一个域替换的功能。可以实现和书签同样的效果,对比word书签,word域还可以用于一些对于格式要求严格的文档导出(将域建立在模板图片上),本文使用c#基于Aspose.Word实现word域套打功能

创建域

以word2013为例,插入-文本-文档部件-域,选择MergeField

下图示例就是插入域完毕的文档

使用Aspose.Word

注册Aspose.Words 去水印

        string licenseFile = System.IO.Path.Combine(ModelPath, @"Model\Aspose.Words.lic");
            if (File.Exists(licenseFile))
            {
                Aspose.Words.License license = new Aspose.Words.License();
                license.SetLicense(licenseFile);
            }

域替换

            Document doc = new Document(System.IO.Path.Combine(ModelPath, TempWordName));
            DocumentBuilder builder = new DocumentBuilder(doc);

            if (titleField != null && valueField != null)
                //文档域的字段赋值
                doc.MailMerge.Execute(titleField, valueField);

            doc.Save(SavePath + ExportWordName);

替换后效果如下

代码示例

posted @ 2020-11-01 16:32  code2roc  阅读(913)  评论(1编辑  收藏  举报