עקבו אחרי Content Control SDT

In In In Microsoft Word, אתה יכול ליצור טופס על ידי החל עם תבנית והוספת בקרת תוכן, כולל תיבות של Checkboxes, תיבות טקסט, בוחרים תאריך ורשימות ירידה. In In In Aspose.Words מסמך מובנה או בקרת תוכן מכל מסמך טעון לתוך Aspose.Words הוא מיובא כצומת מובנה. תגי מסמך ממובנים (SDT או בקרת תוכן) מאפשרים הטמעת סממנטיקה מוגדרת של הלקוח, כמו גם התנהגותו והופעתו במסמך.

מבנה Tag יכול להופיע במסמך במקומות הבאים:

  • רמת בלוק - בין פסקאות וטבלאות, כילד של גוף, HeaderFooter, Comment, Footnote או a Shape node.
  • רו-דרג - בין שורות בטבלה, כילד של צומת שולחן.
  • ברמה התאית - בין התאים בשורה שולחן, כילד של רו.
  • Inline-level - בין תוכן Inline בפנים, כילד של Paragraph.
  • נחטף בתוך מערכה נוספת.

הכנסת בקרת תוכן לתוך מסמך

בגרסה זו של 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-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 או בקרת תוכן באופן מתודולוגי.

הדוגמה הבאה של הקוד מראה כיצד להגדיר את המצב הנוכחי של תיבת הסימון:

// 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");

ביקורת תוכן עבור Custom XML Parts

באפשרותך לחבר בקרות תוכן עם נתוני XML (פרט XML חלק) במסמכים Word

הדוגמה הקודית הבאה מראה כיצד לקשור את השליטה בתוכן לחלקים תואמים:

// 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);

המונחים: content control

אתה יכול לנקות את התוכן של שליטה תוכן עם הצגת בעל מקום. StructuredDocumentTag.clear() שיטה מנקה תוכן של תג מסמך מובנה זה ומציגה בעל מקום אם הוא מוגדר. עם לא ניתן לנקות את התוכן של בקרת תוכן אם יש לו תיקונים. אם לפקד תוכן אין בעל מקום, חמישה חללים מוכנסים כמו ב-MS Word (מלבד סעיפים חוזרים, חזרה על פריטים, קבוצות, תיבות בדיקה, ציטוטים). אם בקרת תוכן ממפה ל-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);

שינוי בקרת תוכן וצבעי גבולות

The The The StructuredDocumentTag.Color הנכס מאפשר לך לקבל או להגדיר את צבע בקרת התוכן. הצבע משפיע על בקרת התוכן בשני מצבים:

1.1 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-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 תכונות. כאשר אתה מקליד את הטקסט לתוך בקרת תוכן במסמך הפלט, הטקסט המסוגד יהיה את הסגנון “Quote”.

הדוגמה הקודית הבאה מראה כיצד להגדיר את הסגנון של בקרת תוכן:

// 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, תג המסמך המובנה של הסעיף החוזר על עצמו ועל סוגי הפריט חוזרים ניתן ליצור ולמטרה זו, המונחים: המונחים: מספק REPEAING_SECTIONM חבר.

הדוגמה הבאה של הקוד מראה כיצד לקשור שלט תוכן חוזר לשולחן:

// 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");