การทำงานกับการควบคุมเนื้อหา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"); |