Since version 0.8, new APIs are added to support forms. Because controls are implementation-dependent, the default form provider will follow the capability defined by OpenOffice.org, it may not be fully compatible with other ODF editors. You can use the following code to create a form.
TextDocument textDoc = TextDocument.newTextDocument(); Form form = textDoc.createForm("Form1");
Following code shows how to get a form in a text document and remove it.
Iterator<Form> iterator = textDoc.getFormIterator(); while (iterator.hasNext()) {
deleteForm = iterator.next();
if (deleteForm.getFormName().equals("Form1")) break;
}
textDoc.removeForm(deleteForm);
Below codes will create a button and add it to the text document with a paragraph as the anchor position. The FrameRectangle specifies an area and the position of this button. The last two parameters are used to specify the control name and the initialized label value.
Paragraph para = doc.addParagraph("Add form button here:"); FrameRectangle btnRtg = new FrameRectangle(0.5, 2, 2.9433, 0.5567, SupportedLinearMeasure.IN); Button btn = (Button)form.createButton(para, btnRtg, "Button1", "Push Button 1");
Below codes will create a text box and add it to the text document with a paragraph as the anchor position. The last parameter are used to specify whether this text box supports multiple-line input.
Paragraph para = doc.addParagraph("Add text box here:"); FrameRectangle textBoxRtg = new FrameRectangle(0.5, 0.2846, 2.9432, 0.8567, SupportedLinearMeasure.IN); TextBox textbox = (TextBox)form.createTextBox(para, textBoxRtg, "TextBox1", "Please input your value here", true);
You can get an iterator of the text box as follows.
Below codes will create a list box and add it to the text document with a paragraph as the anchor position. The fourth parameter is used to specify whether this list box supports multiple selection. And the last parameter is used to specify the visibility of a drop-down list.
Paragraph para = doc.addParagraph("Add list box here:"); FrameRectangle listBoxRtg = new FrameRectangle(0.5752, 0.1429, 2.3307, 0.8398, SupportedLinearMeasure.IN); ListBox listBox = (ListBox)form.createListBox(para, listBoxRtg, "ListBox", true, false);
You can get an iterator of the list box as follows.
Below codes will create a combo box and initialize its entry list with a string array. The last parameter is used to specify the visibility of a drop-down list.