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

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

แผนภาพด้านล่างแสดงให้เห็นว่าฟิลด์IFถูกเก็บไว้ในเอกสาร:

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

update-remove-a-field-aspose-words

โครงสร้างยังสามารถมองเห็นได้ด้านล่างในรูปแบบลำดับชั้นโดยใช้โครงการสาธิต**“DocumentExplorer”**ซึ่งจัดส่งพร้อมกับตัวติดตั้ง**Aspose.Words**.

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-Java
Document doc = new Document(getMyDir() + "Linked 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-Java
private static class FieldsHelper extends DocumentVisitor {
/**
* Converts any fields of the specified type found in the descendants of the node into static text.
*
* @param compositeNode The node in which all descendants of the specified FieldType will be converted to static text.
* @param targetFieldType The FieldType of the field to convert to static text.
*/
public static void convertFieldsToStaticText(CompositeNode compositeNode, int targetFieldType) throws Exception {
for (Field field : compositeNode.getRange().getFields())
{
if (field.getType() == targetFieldType)
{
field.unlink();
}
}
}
}

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(getMyDir() + "Linked fields.docx");
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to text.
for (Field field : doc.getRange().getFields()) {
if (field.getType() == FieldType.FIELD_IF) {
field.unlink();
}
}
doc.save(getArtifactsDir() + "WorkingWithFields.ConvertFieldsInDocument.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(getMyDir() + "Linked fields.docx");
// Pass the appropriate parameters to convert PAGE fields encountered to text only in the body of the first section.
for (Field field : doc.getRange().getFields()) {
if (field.getType() == FieldType.FIELD_PAGE) {
field.unlink();
}
}
doc.save(getArtifactsDir() + "WorkingWithFields.ConvertFieldsInBody.docx");

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(getMyDir() + "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.
for (Field field : doc.getFirstSection().getBody().getLastParagraph().getRange().getFields()) {
if (field.getType() == FieldType.FIELD_IF) {
field.unlink();
}
}
doc.save(getArtifactsDir() + "WorkingWithFields.TestFile.docx");