在 C# 中創建 IF 字段
	Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。 
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
	
	
	
通常我們需要根據不同的條件向讀者顯示不同的文本和信息。在這種情況下,我們需要創建 if 字段來決定向讀者顯示哪個結果。本文重點介紹如何借助 Spire.Doc for .NET 在 C# 中創建 IF 字段。我們一起使用 IF 字段和 MERGEFIELD 字段。
	
{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;
}
	
檢查有效截圖如下:
	 
 
完整代碼:
	
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, paragraph);
string[] fieldName = { "Count" };
string[] fieldValue = { "2" };
document.MailMerge.Execute(fieldName, fieldValue);
document.IsUpdateFields = true;
document.SaveToFile("sample.docx", FileFormat.Docx);
}
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;
}
}
}
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)


 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線