Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Select checkbox
The example shows you how to find checkbox and mark it as "checked".
import java.util.List; import com.independentsoft.office.ExtendedBoolean; import com.independentsoft.office.IContentElement; 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; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument("c:\\test\\input.docx"); List
allElements = doc.getContentElements(); for (IContentElement element : allElements) { if (element instanceof Paragraph) { Paragraph paragraph = (Paragraph)element; for (int i = 0; i < paragraph.getContent().size(); i++) { if (paragraph.getContent().get(i) instanceof Run) { Run run = (Run)paragraph.getContent().get(i); for (int r = 0; r < run.getContent().size(); r++) { if (run.getContent().get(r) instanceof ComplexField) { ComplexField complexField = (ComplexField)run.getContent().get(r); if (complexField.getFormFieldProperties() != null && complexField.getFormFieldProperties().getName() != null && complexField.getFormFieldProperties().getName().equals("CheckBox1") && complexField.getFormFieldProperties().getCheckBoxFormFieldProperties() != null) { complexField.getFormFieldProperties().getCheckBoxFormFieldProperties().setChecked(ExtendedBoolean.TRUE); } } } } } } } 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*