c#使用Aspose实现Word域套打

前言

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

建立域

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

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

使用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);

替换后效果以下spa

代码示例code

相关文章
相关标签/搜索