Word控件Spire.Doc 【表单域】教程(三):如何在 C# 中创建 IF 字段

{IF { MERGEFIELD Count } > “100” “谢谢” “最低订购量为 100 件”}

第一步新建一个word文档。

Document document = new Document();

第 2 步为文档添加一个新部分。

Section section = document.AddSection();

第 3 步为该部分添加一个新段落。

Paragraph paragraph = section.AddParagraph();

第 4 步定义创建 IF 字段的方法。

CreateIfField(document, paragraph);

第 5 步定义合并数据。

string[] fieldName = {"Count"};string[] fieldValue = { "2" };

第 6 步将数据合并到 IF 字段中。

document.MailMerge.Execute(fieldName, fieldValue);

第 7 步更新文档中的所有字段。

document.IsUpdateFields = true;

第 8 步将文档保存到文件中。

document.SaveToFile("sample.docx", FileFormat.Docx);

以下 CreateIfField() 方法显示了如何创建 IF 字段,如:

{IF { MERGEFIELD Count } > “100” “Thanks” ” 最低订购量为 100 个单位 “}

static void CreateIfField(Document document, Paragraph paragraph){IfField ifField = new IfField(document);ifField.Type = FieldType.FieldIf;ifField.Code = "IF ";paragraph.Items.Add(ifField);paragraph.AppendField("Count",FieldType.FieldMergeField);paragraph.AppendText(" > ");paragraph.AppendText(""100"" "");paragraph.AppendText(""""Thanks"" "");paragraph.AppendText(""""The minimum order is 100 units"""");IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);(end as FieldMark).Type = FieldMarkType.FieldEnd;paragraph.Items.Add(end);ifField.End = end as FieldMark;}

检查有效截图如下:

Word控件Spire.Doc 【表单域】教程(三):如何在 C# 中创建 IF 字段

完整代码:

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Interface;namespace CreatIF{class Program{static void Main(string[] args){Document document = new Document();Section section = document.AddSection();Paragraph paragraph = section.AddParagraph();CreateIfField(document

                                                        

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年3月20日
下一篇 2022年3月20日

相关推荐

发表回复

登录后才能评论