Элементы управления содержимым идеально подходят для создания шаблонов, поскольку элементы управления содержимым могут помочь нам исправить положение содержимого, указать тип содержимого (например, дату, изображение или текст), а также ограничить или включить редактирование содержимого. В этой статье я продемонстрирую, как вставить следующие типы элементов управления содержимым в документ Word на Java.
- Поле со списком
- Установите флажок
- Текст
- Изображение
- Средство выбора даты
- Выпадающий список
Фриспир. Документ для Java
Перед использованием приведенного ниже кода нам необходимо загрузить FreeSpire. Doc для Java, а затем импортируйте Spire.Doc.jar файл в ваш проект. Для проекта maven вы можете обратиться к этому онлайн-руководству для установки FreeSpire. Документ для Java из репозитория maven.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.util.Date; public class ContentControls { public static void main(String[] args){ //create a new Word document Document document = new Document(); Section section = document.addSection(); Paragraph paragraph = section.addParagraph(); TextRange txtRange = paragraph.appendText("The following example shows how to add content controls in a Word document."); section.addParagraph(); //add combo box content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Combo Box Content Control: "); txtRange.getCharacterFormat().setItalic(true); StructureDocumentTagInline sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Combo_Box); sd.getSDTProperties().setAlias("ComboBox"); sd.getSDTProperties().setTag("ComboBox"); SdtComboBox cb = new SdtComboBox(); cb.getListItems().add(new SdtListItem("Item 1")); cb.getListItems().add(new SdtListItem("Item 2")); cb.getListItems().add(new SdtListItem("Item 3")); sd.getSDTProperties().setControlProperties(cb); TextRange rt = new TextRange(document); rt.setText(cb.getListItems().get(0).getDisplayText()); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); //add checkbox content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Check Box Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Check_Box); sd.getSDTProperties().setAlias("CheckBox"); sd.getSDTProperties().setTag("CheckBox"); SdtCheckBox scb = new SdtCheckBox(); sd.getSDTProperties().setControlProperties(scb); rt = new TextRange(document); sd.getChildObjects().add(rt); scb.setChecked(true); section.addParagraph(); //add text content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Text Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Text); sd.getSDTProperties().setAlias("Text"); sd.getSDTProperties().setTag("Text"); SdtText text = new SdtText(true); text.isMultiline(true); sd.getSDTProperties().setControlProperties(text); rt = new TextRange(document); rt.setText("Text"); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); paragraph = section.addParagraph(); txtRange = paragraph.appendText("Picture Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setControlProperties(new SdtPicture()); sd.getSDTProperties().setAlias("Picture"); sd.getSDTProperties().setTag("Picture"); DocPicture pic = new DocPicture(document); pic.setWidth(10f); pic.setHeight(10f); pic.loadImage("logo.png"); sd.getSDTContent().getChildObjects().add(pic); section.addParagraph(); //add date picker content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Date Picker Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Date_Picker); sd.getSDTProperties().setAlias("Date"); sd.getSDTProperties().setTag("Date"); SdtDate date = new SdtDate(); date.setCalendarType(CalendarType.Default); date.setDateFormat("yyyy.MM.dd"); date.setFullDate(new Date()); sd.getSDTProperties().setControlProperties(date); rt = new TextRange(document); rt.setText("2018.12.25"); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); //add drop-down list content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Drop-Down List Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List); sd.getSDTProperties().setAlias("DropDownList"); sd.getSDTProperties().setTag("DropDownList"); SdtDropDownList sddl = new SdtDropDownList(); sddl.getListItems().add(new SdtListItem("Option 1")); sddl.getListItems().add(new SdtListItem("Option 2")); sd.getSDTProperties().setControlProperties(sddl); rt = new TextRange(document); rt.setText(sddl.getListItems().get(0).getDisplayText()); sd.getSDTContent().getChildObjects().add(rt); //save and launch the file document.saveToFile("addContentControls.docx", FileFormat.Docx_2013); } }
Выход:
Оригинал: “https://dev.to/eiceblue/insert-content-controls-into-word-document-in-java-1oc6”