콘텐츠 제어 작업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단어와 같이 5 개의 공백이 삽입됩니다(섹션 반복,섹션 항목 반복,그룹,확인란,인용 제외). 콘텐츠 컨트롤이 사용자 지정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보다 약간"부드럽습니다". 예를 들어Color이 빨간색 일 때MS단어는 배경을 분홍색으로 강조 표시합니다.
- 콘텐츠 컨트롤과 상호 작용(편집,선택 등)할 때 콘텐츠 컨트롤의 테두리는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"); |