แทนที่ฟิลด์ด้วยข้อความคงที่

มักจำเป็นต้องแทนที่ฟิลด์เมื่อคุณต้องการบันทึกเอกสารของคุณเป็นสำเนาคงที่ เช่น เมื่อส่งเป็นไฟล์แนบในอีเมล การแปลงช่องต่างๆ เช่น DATE หรือ TIME เป็นข้อความคงที่จะทำให้เอกสารแสดงวันที่เดียวกันกับวันที่ส่งได้ นอกจากนี้ ในบางสถานการณ์ คุณอาจต้องลบเขตข้อมูล IF แบบมีเงื่อนไขออกจากเอกสารของคุณ และแทนที่ด้วยผลลัพธ์ข้อความล่าสุดแทน ตัวอย่างเช่น การแปลงผลลัพธ์ของช่อง IF เป็นข้อความคงที่ ดังนั้นจะไม่เปลี่ยนค่าแบบไดนามิกอีกต่อไปเมื่อมีการอัปเดตช่องในเอกสาร

แผนภาพด้านล่างแสดงวิธีจัดเก็บช่อง IF ในเอกสาร

  • ข้อความล้อมรอบด้วยโหนดฟิลด์พิเศษ – FieldStart และ FieldEnd
  • โหนด FieldSeparator แยกข้อความภายในฟิลด์ออกเป็นโค้ดฟิลด์และผลลัพธ์ของฟิลด์
  • รหัสฟิลด์จะกำหนดลักษณะการทำงานทั่วไปของฟิลด์ ในขณะที่ผลลัพธ์ของฟิลด์จะคงผลลัพธ์ล่าสุดไว้เมื่อมีการอัปเดตฟิลด์นี้โดยใช้ Microsoft Word หรือ Aspose.Words
  • ผลลัพธ์ของฟิลด์คือสิ่งที่เก็บไว้ในฟิลด์และแสดงในเอกสารเมื่อดู

update-remove-a-field-aspose-words

โครงสร้างยังแสดงด้านล่างในรูปแบบลำดับชั้นโดยใช้ โครงการสาธิต “DocumentExplorer”.

update-remove-a-field-aspose-words-2

ช่องที่ไม่สามารถแทนที่ด้วยข้อความได้

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

ตัวอย่างเช่น การพยายามแปลงช่อง PAGE ในส่วนหัวหรือส่วนท้ายเป็นข้อความคงที่จะทำให้ค่าเดียวกันนี้ปรากฏบนทุกหน้า เนื่องจากส่วนหัวและส่วนท้ายซ้ำกันในหลายหน้า และเมื่อยังคงเป็นฟิลด์ จึงมีการจัดการโดยเฉพาะเพื่อให้แสดงผลลัพธ์ที่ถูกต้องสำหรับแต่ละหน้า

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Various fields.docx");
doc.UnlinkFields();

แปลงประเภทฟิลด์บางประเภทในส่วนของเอกสารเฉพาะ

เนื่องจากวิธี ConvertFieldsToStaticText ยอมรับพารามิเตอร์สองตัว ได้แก่ คุณสมบัติ CompositeNode และการแจงนับ FieldType คุณจึงสามารถส่งผ่านโหนดคอมโพสิตไปยังวิธีนี้ได้ ซึ่งช่วยให้สามารถแปลงฟิลด์เป็นข้อความคงที่เฉพาะในส่วนของเอกสารเท่านั้น

ตัวอย่างเช่น คุณสามารถส่งวัตถุ Document และแปลงช่องประเภทที่ระบุจากทั้งเอกสารเป็นข้อความคงที่ หรือคุณสามารถส่งวัตถุ Body ของส่วนและแปลงเฉพาะช่องที่พบในเนื้อหานั้นได้

การแจงนับ FieldType ที่ส่งผ่านไปยังวิธี ConvertFieldsToStaticText ระบุประเภทของฟิลด์ที่ควรแปลงเป็นข้อความคงที่ ประเภทฟิลด์อื่นๆ ที่พบในเอกสารจะยังคงไม่เปลี่ยนแปลง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเลือกฟิลด์ประเภทเฉพาะ – targetFieldType ในโหนดเฉพาะ – compositeNode แล้วแปลงเป็นข้อความคงที่:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class FieldsHelper
{
/// <summary>
/// Converts any fields of the specified type found in the descendants of the node into static text.
/// </summary>
/// <param name="compositeNode">The node in which all descendants of the specified FieldType will be converted to static text.</param>
/// <param name="targetFieldType">The FieldType of the field to convert to static text.</param>
public static void ConvertFieldsToStaticText(CompositeNode compositeNode, FieldType targetFieldType)
{
compositeNode.Range.Fields.Cast<Field>().Where(f => f.Type == targetFieldType).ToList().ForEach(f => f.Unlink());
}
}

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
doc.Range.Fields.Where(f => f.Type == FieldType.FieldIf).ToList().ForEach(f => f.Unlink());
// Save the document with fields transformed to disk
doc.Save(ArtifactsDir + "WorkingWithFields.ConvertFieldsInDocument.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Linked fields.docx");
// Pass the appropriate parameters to convert PAGE fields encountered to text only in the body of the first section.
doc.FirstSection.Body.Range.Fields.Where(f => f.Type == FieldType.FieldPage).ToList().ForEach(f => f.Unlink());
doc.Save(ArtifactsDir + "WorkingWithFields.ConvertFieldsInBody.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields to text that are encountered only in the last
// paragraph of the document.
doc.FirstSection.Body.LastParagraph.Range.Fields.Where(f => f.Type == FieldType.FieldIf).ToList()
.ForEach(f => f.Unlink());
doc.Save(ArtifactsDir + "WorkingWithFields.TestFile.docx");