Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Create text field and check box
The example shows you how to create a text field and a check box.
import com.independentsoft.office.ExtendedBoolean; import com.independentsoft.office.word.DocumentProtection; import com.independentsoft.office.word.DocumentProtectionType; import com.independentsoft.office.word.FieldCode; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.fields.ComplexField; import com.independentsoft.office.word.fields.ComplexFieldCharacterType; import com.independentsoft.office.word.fields.FormCheckBox; import com.independentsoft.office.word.fields.FormText; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Run run1 = new Run(); run1.addText("Question text:"); Run answerRun1 = new Run("Enter answer here"); ComplexField startComplexField1 = new ComplexField(); startComplexField1.setCharacterType(ComplexFieldCharacterType.START); startComplexField1.getFormFieldProperties().setName("Question1"); startComplexField1.getFormFieldProperties().setEnabled(ExtendedBoolean.TRUE); startComplexField1.getFormFieldProperties().getTextBoxFormFieldProperties().setMaxLength(30); Run startComplexFieldRun1 = new Run(); startComplexFieldRun1.add(startComplexField1); ComplexField separatorComplexField1 = new ComplexField(); separatorComplexField1.setCharacterType(ComplexFieldCharacterType.SEPARATOR); Run separatorComplexFieldRun1 = new Run(); separatorComplexFieldRun1.add(separatorComplexField1); ComplexField endComplexField1 = new ComplexField(); endComplexField1.setCharacterType(ComplexFieldCharacterType.END); Run endComplexFieldRun1 = new Run(); endComplexFieldRun1.add(endComplexField1); FormText formText1 = new FormText(); FieldCode formTextFieldCode1 = new FieldCode(formText1); Run formTextRun1 = new Run(); formTextRun1.add(formTextFieldCode1); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); paragraph1.add(startComplexFieldRun1); paragraph1.add(formTextRun1); paragraph1.add(separatorComplexFieldRun1); paragraph1.add(answerRun1); paragraph1.add(endComplexFieldRun1); ComplexField startComplexField2 = new ComplexField(); startComplexField2.setCharacterType(ComplexFieldCharacterType.START); startComplexField2.getFormFieldProperties().setName("CheckBox1"); startComplexField2.getFormFieldProperties().setEnabled(ExtendedBoolean.TRUE); startComplexField2.getFormFieldProperties().getCheckBoxFormFieldProperties().setAutoSize(ExtendedBoolean.TRUE); startComplexField2.getFormFieldProperties().getCheckBoxFormFieldProperties().setChecked(ExtendedBoolean.FALSE); Run startComplexFieldRun2 = new Run(); startComplexFieldRun2.add(startComplexField2); ComplexField separatorComplexField2 = new ComplexField(); separatorComplexField2.setCharacterType(ComplexFieldCharacterType.SEPARATOR); Run separatorComplexFieldRun2 = new Run(); separatorComplexFieldRun2.add(separatorComplexField2); ComplexField endComplexField2 = new ComplexField(); endComplexField2.setCharacterType(ComplexFieldCharacterType.END); Run endComplexFieldRun2 = new Run(); endComplexFieldRun2.add(endComplexField2); FormCheckBox formCheckBox2 = new FormCheckBox(); FieldCode formTextFieldCode2 = new FieldCode(formCheckBox2); Run formCheckBoxRun2 = new Run(); formCheckBoxRun2.add(formTextFieldCode2); Paragraph paragraph2 = new Paragraph(); paragraph2.add(startComplexFieldRun2); paragraph2.add(formCheckBoxRun2); paragraph2.add(separatorComplexFieldRun2); paragraph2.add(endComplexFieldRun2); doc.getSettings().setDocumentProtection(new DocumentProtection()); doc.getSettings().getDocumentProtection().setProtectionType(DocumentProtectionType.FORMS); doc.getBody().add(paragraph1); doc.getBody().add(new Paragraph()); //just blank paragraph to make space doc.getBody().add(new Paragraph()); //just blank paragraph to make space doc.getBody().add(paragraph2); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*