استبدال الحقول بنص ثابت
غالبا ما يكون استبدال الحقول مطلوبا عندما ترغب في حفظ المستند كنسخة ثابتة. على سبيل المثال، عند الإرسال كمرفق في رسالة بريد إلكتروني. سيسمح تحويل الحقول مثل DATE
أو TIME
إلى نص ثابت للمستند بعرض نفس تاريخ إرساله. أيضا، في بعض الحالات، قد تحتاج إلى إزالة الحقول الشرطية IF
من المستند واستبدالها بأحدث نتيجة نصية بدلا من ذلك. على سبيل المثال، تحويل نتيجة الحقل IF
إلى نص ثابت بحيث لن يغير قيمته ديناميكيا عند تحديث الحقول في المستند.
يوضح الرسم البياني أدناه كيفية تخزين حقل IF
في مستند:
- النص محاط بعقد الحقل الخاصة - FieldStart و FieldEnd
- تفصل العقدة FieldSeparator النص داخل الحقل إلى رمز الحقل والنتيجة الميدانية
- يحدد رمز الحقل السلوك العام للحقل، بينما تحتفظ نتيجة الحقل بأحدث نتيجة عند تحديث هذا الحقل باستخدام Microsoft Word أو Aspose.Words
- نتيجة الحقل هي ما يتم تخزينه في الحقل وعرضه في المستند عند عرضه
يمكن أيضا رؤية الهيكل أدناه في شكل هرمي باستخدام المشروع التجريبي “DocumentExplorer”، الذي يأتي مع Aspose.Words المثبت.
الحقول التي لا يمكن استبدالها بالنص
استبدال حقل مع نص ثابت لا يعمل بشكل صحيح لبعض الحقول في رأس أو تذييل الصفحة.
على سبيل المثال، ستؤدي محاولة تحويل الحقل 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"); |