العمل مع التحكم في المحتوى SDT
في Microsoft Word، يمكنك إنشاء نموذج عن طريق البدء بقالب وإضافة عناصر تحكم في المحتوى، بما في ذلك checkboxإس ومربعات النص وجامعي التاريخ والقوائم المنسدلة. في Aspose.Words، يتم استيراد علامة مستند منظم أو عنصر تحكم في المحتوى من أي مستند تم تحميله في Aspose.Words كعقدة StructuredDocumentTag. تسمح علامات المستندات المنظمة (SDT أو التحكم في المحتوى) بتضمين الدلالات المحددة من قبل العميل بالإضافة إلى سلوكها ومظهرها في مستند.
StructuredDocumentTag يمكن أن يحدث في مستند في الأماكن التالية:
- مستوى الكتلة-بين الفقرات والجداول، كطفل لجسم، HeaderFooter، تعليق، حاشية سفلية أو عقدة شكل.
- مستوى الصف-بين الصفوف في الجدول، كطفل لعقدة الجدول.
- مستوى الخلية - بين الخلايا في صف جدول، كطفل لعقدة صف.
- المستوى المضمن-بين المحتوى المضمن في الداخل، كطفل لفقرة.
- متداخلة داخل آخر StructuredDocumentTag.
إدراج عناصر تحكم المحتوى في مستند
في هذا الإصدار من Aspose.Words، يمكن إنشاء الأنواع التالية من SDT أو التحكم في المحتوى:
- Checkbox
- DropDownList
- ComboBox
- التاريخ
- BuildingBlockGallery
- المجموعة
Picture
- RichText
- PlainText
يوضح مثال التعليمات البرمجية التالية كيفية إنشاء عنصر تحكم المحتوى من النوع checkbox:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CheckBoxTypeContentControl.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag stdCheckBox = new StructuredDocumentTag(doc, SdtType.CHECKBOX, MarkupLevel.INLINE); | |
builder.insertNode(stdCheckBox); | |
doc.save(dataDir + "output.doc"); |
يوضح مثال التعليمات البرمجية التالية كيفية إنشاء عنصر تحكم المحتوى من نوع مربع النص المنسق:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(RichTextBoxContentControl.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK); | |
Paragraph para = new Paragraph(doc); | |
Run run = new Run(doc); | |
run.setText("Hello World"); | |
run.getFont().setColor(Color.MAGENTA); | |
para.getRuns().add(run); | |
sdtRichText.getChildNodes().add(para); | |
doc.getFirstSection().getBody().appendChild(sdtRichText); | |
doc.save(dataDir + "output.doc"); |
يوضح مثال التعليمات البرمجية التالية كيفية إنشاء عنصر تحكم المحتوى من نوع مربع التحرير والسرد:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ComboBoxContentControl.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.COMBO_BOX, MarkupLevel.BLOCK); | |
sdt.getListItems().add(new SdtListItem("Choose an item", "3")); | |
sdt.getListItems().add(new SdtListItem("Item 1", "1")); | |
sdt.getListItems().add(new SdtListItem("Item 2", "2")); | |
doc.getFirstSection().getBody().appendChild(sdt); | |
doc.save(dataDir + "output.doc"); |
كيفية تحديث عناصر التحكم في المحتوى
يشرح هذا القسم كيفية تحديث قيم SDT أو التحكم في المحتوى برمجيا.
يوضح مثال الكود التالي كيفية تعيين الحالة الحالية لـ checkbox:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SetCurrentStateOfCheckBox.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag SdtCheckBox = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
//StructuredDocumentTag.Checked property gets/sets current state of the Checkbox SDT | |
if (SdtCheckBox.getSdtType() == SdtType.CHECKBOX) | |
SdtCheckBox.setChecked(true); | |
doc.save(dataDir + "output.doc"); |
يوضح مثال التعليمات البرمجية التالية كيفية تعديل عناصر التحكم في المحتوى من نوع مربع نص عادي والقائمة المنسدلة والصورة:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ModifyContentControls.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx"); | |
for (Object t : doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) { | |
StructuredDocumentTag std = (StructuredDocumentTag) t; | |
if (std.getSdtType() == SdtType.PLAIN_TEXT) { | |
std.removeAllChildren(); | |
Paragraph para = (Paragraph) std.appendChild(new Paragraph(doc)); | |
Run run = new Run(doc, "new text goes here"); | |
para.appendChild(run); | |
} | |
if (std.getSdtType() == SdtType.DROP_DOWN_LIST) { | |
SdtListItem secondItem = std.getListItems().get(2); | |
std.getListItems().setSelectedValue(secondItem); | |
} | |
if (std.getSdtType() == SdtType.PICTURE) { | |
Shape shape = (Shape) std.getChild(NodeType.SHAPE, 0, true); | |
if (shape.hasImage()) { | |
shape.getImageData().setImage(dataDir + "Watermark.png"); | |
} | |
} | |
doc.save(dataDir + "output.doc"); |
ربط التحكم في المحتوى إلى أجزاء XML مخصصة
يمكنك ربط عناصر التحكم في المحتوى ببيانات XML (custom XML part) في مستندات ورد.
يوضح مثال التعليمات البرمجية التالية كيفية ربط عنصر تحكم المحتوى إلى أجزاء XML مخصصة:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(BindingContentControlwithXML.class); | |
Document doc = new Document(); | |
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d").toString(), "<root><text>Hello, World!</text></root>"); | |
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); | |
doc.getFirstSection().getBody().appendChild(sdt); | |
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", ""); | |
dataDir = dataDir + "BindSDTtoCustomXmlPart_out.doc"; | |
// Save the document to disk. | |
doc.save(dataDir); |
مسح محتويات عنصر تحكم المحتوى
يمكنك مسح محتويات عنصر تحكم المحتوى مع عرض عنصر نائب. تقوم طريقة StructuredDocumentTag.clear() بمسح محتويات علامة المستند المهيكلة هذه وعرض عنصر نائب إذا تم تعريفه. ومع ذلك، لا يمكن مسح محتويات عنصر تحكم المحتوى إذا كان يحتوي على مراجعات. إذا لم يكن عنصر تحكم المحتوى يحتوي على عنصر نائب، فسيتم إدراج خمس مسافات كما هو الحال في MS كلمة (باستثناء الأقسام المتكررة، وعناصر القسم المتكررة، والمجموعات، وخانات الاختيار، والاستشهادات). إذا تم تعيين عنصر تحكم المحتوى إلى مخصص XML، يتم مسح العقدة XML المشار إليها.
يوضح مثال الكود التالي كيفية مسح محتوى التحكم في المحتوى:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ClearContentsControl.class); | |
Document doc = new Document(dataDir + "input.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
sdt.clear(); | |
dataDir = dataDir + "ClearContentsControl_out.doc"; | |
// Save the document to disk. | |
doc.save(dataDir); |
تغيير خلفية التحكم في المحتوى وألوان الحدود
تتيح لك خاصية StructuredDocumentTag.Color
الحصول على لون التحكم في المحتوى أو تعيينه. يؤثر اللون على التحكم في المحتوى في حالتين:
- MS كلمة يسلط الضوء على خلفية عنصر تحكم المحتوى عندما يتحرك الماوس فوق عنصر تحكم المحتوى. هذا يساعد على تحديد التحكم في المحتوى. لون تسليط الضوء هو “ليونة” قليلا من Color. على سبيل المثال، MS كلمة يسلط الضوء على الخلفية مع اللون الوردي، عندما Color أحمر.
- عندما تتفاعل (التحرير، الانتقاء، إلخ) مع عنصر التحكم في المحتوى، يتم تلوين حدود عنصر التحكم في المحتوى بـ Color.
يوضح مثال التعليمات البرمجية التالية كيفية تغيير لون عنصر تحكم المحتوى:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
Document doc = new Document(dataDir + "input.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
sdt.setColor(Color.RED); | |
dataDir = dataDir + "SetContentControlColor_out.docx"; | |
// Save the document to disk. | |
doc.save(dataDir); |
كيفية تعيين نمط لتنسيق النص المكتوب في عنصر تحكم المحتوى
إذا كنت تريد تعيين نمط التحكم في المحتوى، فيمكنك استخدام خصائص StructuredDocumentTag.Style
أو StructuredDocumentTag.StyleName
. عند كتابة النص في عنصر تحكم المحتوى في مستند الإخراج، سيكون للنص المكتوب النمط"اقتباس".
يوضح مثال التعليمات البرمجية التالية كيفية تعيين نمط التحكم في المحتوى:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "input.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
Style style = doc.getStyles().getByStyleIdentifier(StyleIdentifier.QUOTE); | |
sdt.setStyle(style); | |
dataDir = dataDir + "SetContentControlStyle_out.docx"; | |
doc.save(dataDir); |
العمل مع التحكم في محتوى القسم المتكرر
يسمح التحكم في محتوى قسم التكرار بتكرار المحتوى الموجود بداخله. باستخدام Aspose.Words، يمكن إنشاء عقد علامة المستند المهيكلة لقسم التكرار وأنواع عناصر القسم المتكرر ولهذا الغرض، يوفر SdtType نوع التعداد REPEATING_SECTION_ITEM عضوا.
يوضح مثال التعليمات البرمجية التالية كيفية ربط عنصر تحكم محتوى مقطع مكرر بجدول:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
CustomXmlPart xmlPart = doc.getCustomXmlParts().add("Books", | |
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" + | |
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" + | |
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>"); | |
Table table = builder.startTable(); | |
builder.insertCell(); | |
builder.write("Title"); | |
builder.insertCell(); | |
builder.write("Author"); | |
builder.endRow(); | |
builder.endTable(); | |
StructuredDocumentTag repeatingSectionSdt = | |
new StructuredDocumentTag(doc, SdtType.REPEATING_SECTION, MarkupLevel.ROW); | |
repeatingSectionSdt.getXmlMapping().setMapping(xmlPart, "/books[1]/book", ""); | |
table.appendChild(repeatingSectionSdt); | |
StructuredDocumentTag repeatingSectionItemSdt = | |
new StructuredDocumentTag(doc, SdtType.REPEATING_SECTION_ITEM, MarkupLevel.ROW); | |
repeatingSectionSdt.appendChild(repeatingSectionItemSdt); | |
Row row = new Row(doc); | |
repeatingSectionItemSdt.appendChild(row); | |
StructuredDocumentTag titleSdt = | |
new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.CELL); | |
titleSdt.getXmlMapping().setMapping(xmlPart, "/books[1]/book[1]/title[1]", ""); | |
row.appendChild(titleSdt); | |
StructuredDocumentTag authorSdt = | |
new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.CELL); | |
authorSdt.getXmlMapping().setMapping(xmlPart, "/books[1]/book[1]/author[1]", ""); | |
row.appendChild(authorSdt); | |
doc.save(dataDir + "Document.docx"); |