แทรกฟิลด์

มีหลายวิธีในการแทรกฟิลด์ลงในเอกสาร:

ในบทความนี้ เราจะดูรายละเอียดเพิ่มเติมแต่ละวิธีและวิเคราะห์วิธีแทรกฟิลด์บางฟิลด์โดยใช้ตัวเลือกเหล่านี้

การแทรกฟิลด์ลงในเอกสารโดยใช้ DocumentBuilder

ใน Aspose.Words วิธีการ InsertField ใช้เพื่อแทรกฟิลด์ใหม่ลงในเอกสาร พารามิเตอร์แรกยอมรับโค้ดฟิลด์แบบเต็มของฟิลด์ที่จะแทรก พารามิเตอร์ตัวที่สองเป็นทางเลือก และอนุญาตให้ตั้งค่าผลลัพธ์ของฟิลด์ด้วยตนเองได้ หากไม่ได้ระบุ ฟิลด์นี้จะได้รับการอัปเดตโดยอัตโนมัติ คุณสามารถส่งค่าว่างหรือค่าว่างให้กับพารามิเตอร์นี้เพื่อแทรกฟิลด์ที่มีค่าฟิลด์ว่างได้ หากคุณไม่แน่ใจเกี่ยวกับไวยากรณ์ของโค้ดฟิลด์ ให้สร้างฟิลด์ใน Microsoft Word ก่อน แล้วสลับเพื่อดูโค้ดของฟิลด์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกเขตข้อมูลผสานลงในเอกสารโดยใช้ DocumentBuilder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField(@"MERGEFIELD MyFieldName \* MERGEFORMAT");
doc.Save(ArtifactsDir + "WorkingWithFields.InsertField.docx");

เทคนิคเดียวกันนี้ใช้ในการแทรกฟิลด์ที่ซ้อนกันภายในฟิลด์อื่น

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกฟิลด์ที่ซ้อนกันภายในฟิลด์อื่นโดยใช้ DocumentBuilder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < 5; i++)
builder.InsertBreak(BreakType.PageBreak);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// We want to insert a field like this:
// { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" }
Field field = builder.InsertField(@"IF ");
builder.MoveTo(field.Separator);
builder.InsertField("PAGE");
builder.Write(" <> ");
builder.InsertField("NUMPAGES");
builder.Write(" \"See Next Page\" \"Last Page\" ");
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertNestedFields.docx");

ระบุสถานที่ในระดับฟิลด์

ตัวระบุภาษาเป็นตัวย่อตัวเลขมาตรฐานสากลสำหรับภาษาในประเทศหรือภูมิภาคทางภูมิศาสตร์ ด้วย Aspose.Words คุณสามารถระบุ Locale ในระดับฟิลด์ได้โดยใช้คุณสมบัติ LocaleId ซึ่งรับหรือตั้งค่า ID ตำแหน่งที่ตั้งของฟิลด์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้ตัวเลือกนี้:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
DocumentBuilder builder = new DocumentBuilder();
Field field = builder.InsertField(FieldType.FieldDate, true);
field.LocaleId = 1049;
builder.Document.Save(ArtifactsDir + "WorkingWithFields.SpecifylocaleAtFieldlevel.docx");

แทรกฟิลด์ที่ไม่ได้พิมพ์/ว่างเปล่า

หากคุณต้องการแทรกช่องที่ไม่ได้พิมพ์/ว่างเปล่า ({}) เช่นเดียวกับที่ Microsoft Word อนุญาต คุณสามารถใช้วิธี InsertField กับพารามิเตอร์ FieldType.FieldNone ได้ หากต้องการแทรกฟิลด์ลงในเอกสาร Word คุณสามารถกดคีย์ผสม “Ctrl + F9”

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกฟิลด์ว่างลงในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldUnknown field = (FieldUnknown) builder.InsertField(FieldType.FieldNone, false);
doc.Save(ArtifactsDir + "WorkingWithFields.InsertFieldNone.docx");

การแทรกฟิลด์ลงในเอกสารโดยใช้ FieldBuilder

อีกวิธีหนึ่งในการแทรกฟิลด์ใน Aspose.Words คือคลาส FieldBuilder มีอินเทอร์เฟซที่คล่องแคล่วเพื่อระบุสวิตช์ฟิลด์และค่าอาร์กิวเมนต์เป็นข้อความ โหนด หรือแม้แต่ฟิลด์ที่ซ้อนกัน

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกฟิลด์ลงในเอกสารโดยใช้ FieldBuilder:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Prepare IF field with two nested MERGEFIELD fields: { IF "left expression" = "right expression" "Firstname: { MERGEFIELD firstname }" "Lastname: { MERGEFIELD lastname }"}
FieldBuilder fieldBuilder = new FieldBuilder(FieldType.FieldIf)
.AddArgument("left expression")
.AddArgument("=")
.AddArgument("right expression")
.AddArgument(
new FieldArgumentBuilder()
.AddText("Firstname: ")
.AddField(new FieldBuilder(FieldType.FieldMergeField).AddArgument("firstname")))
.AddArgument(
new FieldArgumentBuilder()
.AddText("Lastname: ")
.AddField(new FieldBuilder(FieldType.FieldMergeField).AddArgument("lastname")));
// Insert IF field in exact location
Field field = fieldBuilder.BuildAndInsert(doc.FirstSection.Body.FirstParagraph);
field.Update();
doc.Save(ArtifactsDir + "Field.InsertFieldUsingFieldBuilder.docx");

การแทรกฟิลด์โดยใช้ DOM

คุณยังสามารถแทรกฟิลด์ประเภทต่างๆ โดยใช้ Aspose.Words Document Object Model (DOM) ได้อีกด้วย ในส่วนนี้เราจะดูตัวอย่างบางส่วน

การแทรกเขตข้อมูลผสานลงในเอกสารโดยใช้ DOM

ฟิลด์ MERGEFIELD ในเอกสาร Word สามารถแสดงได้ด้วยคลาส FieldMergeField คุณสามารถใช้คลาส FieldMergeField เพื่อดำเนินการต่อไปนี้:

  • ระบุชื่อเขตข้อมูลผสาน
  • ระบุการจัดรูปแบบของช่องผสาน
  • ระบุข้อความที่อยู่ระหว่างตัวคั่นฟิลด์และส่วนท้ายของฟิลด์ผสาน
  • ระบุข้อความที่จะแทรกหลังช่องผสานหากช่องไม่ว่างเปล่า
  • ระบุข้อความที่จะแทรกก่อนช่องผสานหากช่องไม่ว่างเปล่า

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ MERGE โดยใช้ DOM ลงในย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
builder.MoveTo(para);
// We want to insert a merge field like this:
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" }
FieldMergeField field = (FieldMergeField) builder.InsertField(FieldType.FieldMergeField, false);
// { " MERGEFIELD Test1" }
field.FieldName = "Test1";
// { " MERGEFIELD Test1 \\b Test2" }
field.TextBefore = "Test2";
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 }
field.TextAfter = "Test3";
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m" }
field.IsMapped = true;
// { " MERGEFIELD Test1 \\b Test2 \\f Test3 \\m \\v" }
field.IsVerticalFormatting = true;
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertMergeFieldUsingDOM.docx");

การแทรกฟิลด์ Mail Merge ADDRESSBLOCK ลงในเอกสารโดยใช้ DOM

ช่อง ADDRESSBLOCK ใช้เพื่อแทรกบล็อกที่อยู่ Mail Merge ในเอกสาร Word ฟิลด์ ADDRESSBLOCK ในเอกสาร Word สามารถแสดงด้วยคลาส FieldAddressBlock คุณสามารถใช้คลาส FieldAddressBlock เพื่อดำเนินการต่อไปนี้:

  • ระบุว่าจะรวมชื่อประเทศ/ภูมิภาคในช่องหรือไม่
  • ระบุว่าจะจัดรูปแบบที่อยู่ตามประเทศ/ภูมิภาคของผู้รับตามที่กำหนดโดย POST*CODE (Universal Postal Union 2006)
  • ระบุชื่อประเทศ/ภูมิภาคที่ยกเว้น
  • ระบุรูปแบบชื่อและที่อยู่
  • ระบุรหัสภาษาที่ใช้ในการจัดรูปแบบที่อยู่

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ Mail Merge ADDRESSBLOCK โดยใช้ DOM ให้กับย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
builder.MoveTo(para);
// We want to insert a mail merge address block like this:
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 \\f Test3 \\l \"Test 4\" }
FieldAddressBlock field = (FieldAddressBlock) builder.InsertField(FieldType.FieldAddressBlock, false);
// { ADDRESSBLOCK \\c 1" }
field.IncludeCountryOrRegionName = "1";
// { ADDRESSBLOCK \\c 1 \\d" }
field.FormatAddressOnCountryOrRegion = true;
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 }
field.ExcludedCountryOrRegionName = "Test2";
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 \\f Test3 }
field.NameAndAddressFormat = "Test3";
// { ADDRESSBLOCK \\c 1 \\d \\e Test2 \\f Test3 \\l \"Test 4\" }
field.LanguageId = "Test 4";
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertMailMergeAddressBlockFieldUsingDOM.docx");

การแทรกฟิลด์ ADVANCE ลงในเอกสารโดยไม่ต้องใช้ DocumentBuilder

ช่อง ADVANCE ใช้เพื่อชดเชยข้อความที่ตามมาภายในบรรทัดทางซ้าย ขวา ขึ้นหรือลง ฟิลด์ ADVANCE ในเอกสาร Word สามารถแสดงได้ด้วยคลาส FieldAdvance คุณสามารถใช้คลาส FieldAdvance เพื่อดำเนินการต่อไปนี้:

  • ระบุจำนวนจุดที่ควรย้ายข้อความที่ตามหลังฟิลด์ในแนวตั้งจากขอบด้านบนของหน้า
  • ระบุจำนวนจุดที่ควรย้ายข้อความที่ตามหลังฟิลด์ในแนวนอนจากขอบด้านซ้ายของคอลัมน์ กรอบ หรือกล่องข้อความ
  • ระบุจำนวนจุดที่ข้อความที่อยู่หลังฟิลด์ควรเลื่อนไปทางซ้าย ขวา ขึ้นหรือลง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ ADVANCE โดยใช้ DOM ลงในย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
// We want to insert an Advance field like this:
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 }
FieldAdvance field = (FieldAdvance) para.AppendField(FieldType.FieldAdvance, false);
// { ADVANCE \\d 10 " }
field.DownOffset = "10";
// { ADVANCE \\d 10 \\l 10 }
field.LeftOffset = "10";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 }
field.RightOffset = "-3.3";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 }
field.UpOffset = "0";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 }
field.HorizontalPosition = "100";
// { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 }
field.VerticalPosition = "100";
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertAdvanceFieldWithOutDocumentBuilder.docx");

การแทรกฟิลด์ ASK ลงในเอกสารโดยไม่ต้องใช้ DocumentBuilder

ช่อง ASK ใช้เพื่อแจ้งให้ผู้ใช้ป้อนข้อความเพื่อกำหนดให้กับบุ๊กมาร์กในเอกสาร Word ฟิลด์ ASK ในเอกสาร Word สามารถแสดงโดยคลาส FieldAsk คุณสามารถใช้คลาส FieldAsk เพื่อดำเนินการต่อไปนี้:

  • ระบุชื่อบุ๊คมาร์ค
  • ระบุการตอบสนองของผู้ใช้เริ่มต้น (ค่าเริ่มต้นที่มีอยู่ในหน้าต่างแจ้ง)
  • ระบุว่าควรได้รับการตอบสนองของผู้ใช้หนึ่งครั้งต่อการดำเนินการ Mail Merge หรือไม่
  • ระบุข้อความพร้อมท์ (ชื่อของหน้าต่างพร้อมท์)

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ ASK โดยใช้ DOM ลงในย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
// We want to insert an Ask field like this:
// { ASK \"Test 1\" Test2 \\d Test3 \\o }
FieldAsk field = (FieldAsk) para.AppendField(FieldType.FieldAsk, false);
// { ASK \"Test 1\" " }
field.BookmarkName = "Test 1";
// { ASK \"Test 1\" Test2 }
field.PromptText = "Test2";
// { ASK \"Test 1\" Test2 \\d Test3 }
field.DefaultResponse = "Test3";
// { ASK \"Test 1\" Test2 \\d Test3 \\o }
field.PromptOnceOnMailMerge = true;
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertASKFieldWithOutDocumentBuilder.docx");

การแทรกฟิลด์ AUTHOR ลงในเอกสารโดยไม่ต้องใช้ DocumentBuilder

ฟิลด์ AUTHOR ใช้เพื่อระบุชื่อผู้เขียนเอกสารจากคุณสมบัติ Document ช่อง AUTHOR ในเอกสาร Word สามารถแสดงได้ด้วยคลาส FieldAuthor คุณสามารถใช้คลาส FieldAuthor เพื่อดำเนินการต่อไปนี้:

  • ระบุชื่อผู้เขียนเอกสาร

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ AUTHOR โดยใช้ DOM ลงในย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = (Paragraph) doc.GetChildNodes(NodeType.Paragraph, true)[0];
// We want to insert an AUTHOR field like this:
// { AUTHOR Test1 }
FieldAuthor field = (FieldAuthor) para.AppendField(FieldType.FieldAuthor, false);
field.AuthorName = "Test1"; // { AUTHOR Test1 }
field.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertAuthorField.docx");

การแทรกฟิลด์ INCLUDETEXT ลงในเอกสารโดยไม่ต้องใช้ DocumentBuilder

ช่อง INCLUDETEXT จะแทรกข้อความและกราฟิกที่มีอยู่ในเอกสารที่มีชื่ออยู่ในโค้ดช่อง คุณสามารถแทรกเอกสารทั้งหมดหรือบางส่วนของเอกสารที่อ้างอิงโดยบุ๊กมาร์กได้ ฟิลด์นี้ในเอกสาร Word แสดงโดย INCLUDETEXT คุณสามารถใช้คลาส FieldIncludeText เพื่อดำเนินการต่อไปนี้:

  • ระบุชื่อบุ๊คมาร์คของเอกสารที่รวมไว้
  • ระบุตำแหน่งของเอกสาร

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ INCLUDETEXT โดยใช้ DOM ลงในย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = new Paragraph(doc);
// We want to insert an INCLUDETEXT field like this:
// { INCLUDETEXT "file path" }
FieldIncludeText fieldIncludeText = (FieldIncludeText) para.AppendField(FieldType.FieldIncludeText, false);
fieldIncludeText.BookmarkName = "bookmark";
fieldIncludeText.SourceFullName = MyDir + "IncludeText.docx";
doc.FirstSection.Body.AppendChild(para);
fieldIncludeText.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertIncludeFieldWithoutDocumentBuilder.docx");

การแทรกฟิลด์ TOA ลงในเอกสารโดยไม่ต้องใช้ DocumentBuilder

ช่อง TOA (สารบัญหน่วยงาน) จะสร้างและแทรกสารบัญหน่วยงาน ฟิลด์ TOA รวบรวมรายการที่ทำเครื่องหมายโดยฟิลด์ TA (Table of Authorities Entry) Microsoft Office Word จะแทรกฟิลด์ TOA เมื่อคุณคลิก แทรกสารบัญ ในกลุ่ม ตารางอำนาจ บนแท็บ References เมื่อคุณดูช่อง TOA ในเอกสารของคุณ ไวยากรณ์จะมีลักษณะดังนี้:

{ TOA [Switches ] }

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มฟิลด์ TOA โดยใช้ DOM ลงในย่อหน้าในเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
Paragraph para = new Paragraph(doc);
// We want to insert TA and TOA fields like this:
// { TA \c 1 \l "Value 0" }
// { TOA \c 1 }
FieldTA fieldTA = (FieldTA) para.AppendField(FieldType.FieldTOAEntry, false);
fieldTA.EntryCategory = "1";
fieldTA.LongCitation = "Value 0";
doc.FirstSection.Body.AppendChild(para);
para = new Paragraph(doc);
FieldToa fieldToa = (FieldToa) para.AppendField(FieldType.FieldTOA, false);
fieldToa.EntryCategory = "1";
doc.FirstSection.Body.AppendChild(para);
fieldToa.Update();
doc.Save(ArtifactsDir + "WorkingWithFields.InsertTOAFieldWithoutDocumentBuilder.docx");