العمل مع التحكم في المحتوى SDT

في Microsoft Word، يمكنك إنشاء نموذج من خلال البدء بالقالب وإضافة عناصر التحكم في المحتوى، بما في ذلك مربعات الاختيار ومربعات النص ومنتقيات التاريخ والقوائم المنسدلة. في Aspose.Words، يتم استيراد علامة المستند المنظمة أو عنصر تحكم المحتوى من أي مستند تم تحميله في Aspose.Words كعقدة StructuredDocumentTag. تسمح علامات المستندات المنظمة (SDT أو التحكم في المحتوى) بتضمين دلالات محددة من قبل العميل بالإضافة إلى سلوكها ومظهرها في المستند.

يمكن أن تظهر StructuredDocumentTag في مستند في الأماكن التالية:

  • مستوى الكتلة - بين الفقرات والجداول، كعنصر فرعي للنص الأساسي أو رأس التذييل أو التعليق أو الحاشية السفلية أو عقدة الشكل
  • مستوى الصف - بين الصفوف في الجدول، كفرع لعقدة الجدول
  • مستوى الخلية - بين الخلايا الموجودة في صف الجدول، كفرع لعقدة الصف
  • المستوى المضمن - بين المحتوى المضمن بالداخل، كطفل للفقرة
  • متداخلة داخل StructuredDocumentTag أخرى

إدراج عناصر التحكم في المحتوى في المستند

في هذا الإصدار من Aspose.Words، يمكن إنشاء الأنواع التالية من SDT أو التحكم في المحتوى:

  • Checkbox
  • DropDownList
  • ComboBox
  • Date
  • BuildingBlockGallery
  • Group
  • Picture
  • RichText
  • PlainText

يوضح مثال التعليمات البرمجية التالي كيفية إنشاء عنصر تحكم في المحتوى لمربع اختيار النوع:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Open the empty document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag SdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
// Insert content control into the document
builder.InsertNode(SdtCheckBox);
dataDir = dataDir + "CheckBoxTypeContentControl_out.docx";
doc.Save(dataDir, SaveFormat.Docx);

يوضح مثال التعليمات البرمجية التالي كيفية إنشاء عنصر تحكم محتوى لمربع نص منسق للنوع:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
Document doc = new Document();
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
Paragraph para = new Paragraph(doc);
Run run = new Run(doc);
run.Text = "Hello World";
run.Font.Color = Color.Green;
para.Runs.Add(run);
sdtRichText.ChildNodes.Add(para);
doc.FirstSection.Body.AppendChild(sdtRichText);
dataDir = dataDir + "RichTextBoxContentControl_out.docx";
doc.Save(dataDir);

يوضح مثال التعليمات البرمجية التالي كيفية إنشاء عنصر تحكم المحتوى في مربع التحرير والسرد النوع:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
Document doc = new Document();
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.ComboBox, MarkupLevel.Block);
sdt.ListItems.Add(new SdtListItem("Choose an item", "-1"));
sdt.ListItems.Add(new SdtListItem("Item 1", "1"));
sdt.ListItems.Add(new SdtListItem("Item 2", "2"));
doc.FirstSection.Body.AppendChild(sdt);
dataDir = dataDir + "ComboBoxContentControl_out.docx";
doc.Save(dataDir);

كيفية تحديث عناصر التحكم في المحتوى

يشرح هذا القسم كيفية تحديث قيم SDT أو التحكم في المحتوى برمجياً.

يوضح مثال التعليمات البرمجية التالي كيفية تعيين الحالة الحالية لخانة الاختيار:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Open an existing document
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get the first content control from the document
StructuredDocumentTag SdtCheckBox = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
// StructuredDocumentTag.Checked property gets/sets current state of the Checkbox SDT
if (SdtCheckBox.SdtType == SdtType.Checkbox)
SdtCheckBox.Checked = true;
dataDir = dataDir + "SetCurrentStateOfCheckBox_out.docx";
doc.Save(dataDir);

يوضح مثال التعليمات البرمجية التالي كيفية تعديل عناصر التحكم في المحتوى من نوع مربع نص عادي وقائمة منسدلة وصورة:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Open an existing document
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
if (sdt.SdtType == SdtType.PlainText)
{
sdt.RemoveAllChildren();
Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph;
Run run = new Run(doc, "new text goes here");
para.AppendChild(run);
}
else if (sdt.SdtType == SdtType.DropDownList)
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
}
else if (sdt.SdtType == SdtType.Picture)
{
Shape shape = (Shape)sdt.GetChild(NodeType.Shape, 0, true);
if (shape.HasImage)
{
shape.ImageData.SetImage(dataDir + "Watermark.png");
}
}
}
dataDir = dataDir + "ModifyContentControls_out.docx";
doc.Save(dataDir);

ربط التحكم في المحتوى بأجزاء XML المخصصة

يمكنك ربط عناصر التحكم في المحتوى ببيانات XML (جزء XML مخصص) في مستندات Word.

يوضح مثال التعليمات البرمجية التالي كيفية ربط التحكم في المحتوى بأجزاء XML المخصصة:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
CustomXmlPart xmlPart = doc.CustomXmlParts.Add(Guid.NewGuid().ToString("B"), "<root><text>Hello, World!</text></root>");
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(sdt);
sdt.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", "");
dataDir = dataDir + "BindSDTtoCustomXmlPart_out.doc";
// Save the document to disk.
doc.Save(dataDir);

XMLMapping لنطاق علامات المستند المنظم

يمكنك الحصول على تعيين نطاق علامات المستند المنظم هذا إلى بيانات XML في جزء XML مخصص من المستند الحالي باستخدام خاصية StructuredDocumentTagRangeStart.XmlMapping. ومع ذلك، يمكن استخدام أسلوب SetMapping لتعيين نطاق علامات مستند منظم إلى بيانات XML.

يوضح مثال التعليمات البرمجية التالي كيفية تعيين تعيين XML:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTagRangeStart sdtRangeStart = (StructuredDocumentTagRangeStart)doc.GetChild(NodeType.StructuredDocumentTagRangeStart, 0, true);
sdtRangeStart.XmlMapping.SetMapping(doc.CustomXmlParts[0], "/Root/Element", null);
doc.Save(dataDir + "output.docx");

مسح محتويات عنصر التحكم في المحتوى

يمكنك مسح محتويات عنصر تحكم المحتوى من خلال عرض عنصر نائب. تقوم طريقة StructuredDocumentTag.Clear بمسح محتويات علامة المستند المنظمة هذه وتعرض عنصرًا نائبًا إذا تم تعريفه. ومع ذلك، ليس من الممكن مسح محتويات عنصر تحكم المحتوى إذا كان يحتوي على مراجعات. إذا لم يكن عنصر تحكم المحتوى يحتوي على عنصر نائب، فسيتم إدراج خمس مسافات كما هو الحال في Microsoft Word (باستثناء الأقسام المتكررة، وعناصر القسم المتكررة، والمجموعات، وخانات الاختيار، والاستشهادات). إذا تم تعيين عنصر تحكم المحتوى إلى XML مخصص، فسيتم مسح عقدة XML المشار إليها.

يوضح مثال التعليمات البرمجية التالي كيفية مسح محتوى التحكم في المحتوى:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
sdt.Clear();
dataDir = dataDir + "ClearContentsControl_out.doc";
// Save the document to disk.
doc.Save(dataDir);

تغيير ألوان الخلفية والحدود للتحكم في المحتوى

تتيح لك خاصية StructuredDocumentTag.Color الحصول على لون التحكم في المحتوى أو تعيينه. يؤثر اللون على التحكم في المحتوى في حالتين:

  1. يقوم برنامج MS Word بتمييز خلفية عنصر التحكم في المحتوى عندما يتحرك الماوس فوق عنصر التحكم في المحتوى. وهذا يساعد على تحديد التحكم في المحتوى. لون التمييز “أكثر نعومة” قليلاً من Color. على سبيل المثال، يقوم MS Word بتمييز الخلفية باللون الوردي، عندما يكون Color باللون الأحمر.
  2. عندما تتفاعل (التحرير، الانتقاء، إلخ) مع التحكم في المحتوى، يتم تلوين حدود التحكم في المحتوى باستخدام Color.

يوضح مثال التعليمات البرمجية التالي كيفية تغيير لون التحكم في المحتوى:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
sdt.Color = 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-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Style style = doc.Styles[StyleIdentifier.Quote];
sdt.Style = style;
dataDir = dataDir + "SetContentControlStyle_out.docx";
// Save the document to disk.
doc.Save(dataDir);

العمل مع تكرار التحكم في محتوى القسم

يسمح التحكم في محتوى القسم المكرر بتكرار المحتوى الموجود بداخله. باستخدام Aspose.Words، يمكن إنشاء عقد علامة المستند المنظمة للقسم المتكرر وأنواع عناصر القسم المتكرر ولهذا الغرض، توفر نوع التعداد SdtType خاصية RepeatingSectionItem.

يوضح مثال التعليمات البرمجية التالي كيفية ربط عنصر تحكم محتوى مقطع متكرر بجدول.

// 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);
CustomXmlPart xmlPart = doc.CustomXmlParts.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.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
doc.Save(dataDir + "Document.docx");