کار با کنترل محتوا SDT
در Microsoft Word، شما می توانید یک فرم را با شروع با یک قالب و اضافه کردن کنترل محتوا، از جمله checkboxes، جعبه های متن، انتخاب کننده های تاریخ و لیست های کشویی ایجاد کنید. در 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"); |
مثال کد زیر نشان می دهد که چگونه کنترل محتوا از نوع rich text box را ایجاد کنیم:
// 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 وارد می شود (به جز تکرار بخش ها، تکرار بخش ها، گروه ها، جعبه های چک، نقل قول ها). اگر یک کنترل محتوا به custom 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 Word پس زمینه کنترل محتوا را هنگام حرکت ماوس بر روی کنترل محتوا برجسته می کند. این به شناسایی کنترل محتوا کمک می کند. رنگ برجسته کردن کمی “نرم تر” از 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); |
کار با کنترل محتوای بخش تکراری
بخش repeating content control امکان تکرار محتوای موجود در آن را فراهم می کند. با استفاده از 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"); |