Index: document/cookbook/Fields.mdtext =================================================================== --- document/cookbook/Fields.mdtext (revision 1421066) +++ document/cookbook/Fields.mdtext (working copy) @@ -2,7 +2,7 @@
@@ -127,7 +127,7 @@ Index: document/cookbook/Forms.mdtext =================================================================== --- document/cookbook/Forms.mdtext (revision 0) +++ document/cookbook/Forms.mdtext (working copy) @@ -0,0 +1,278 @@ +Documents > Cookbook >Forms + +
+
Form +
+ 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);
+
+

Controls +

Button +
+ 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");
+
+
+ You can get an iterator of the button as follows. +
+
+			Iterator<FormControl> iterator = Button.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ btn = (Button) iterator.next();
+ }
+
+
Label +
+ Below codes will create a label and add it to the text document with a paragraph as the anchor position. +
+
+			Paragraph para = doc.addParagraph("Add form label here:");
+ FrameRectangle labelRtg = new FrameRectangle(0.5, 1.2553, 1.2, 0.5, SupportedLinearMeasure.IN);
+ Label label = (Label) form.createLabel(doc, labelRtg, "Label2","This is a label.");
+
+
+ You can get an iterator of the label as follows. +
+
+			Iterator<FormControl> iterator = Label.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ label = (Label) iterator.next();
+ } +
+
TextBox +
+ 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. +
+
+			Iterator<FormControl> iterator = TextBox.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ textBox = (TextBox) iterator.next();
+ }
+
+
ListBox +
+ 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. +
+
+			Iterator<FormControl> iterator = ListBox.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ listBox = (ListBox) iterator.next();
+ }
+
+
ComboBox +
+ 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. +
+
+			FormControl comboBox = form.createComboBox(doc, new FrameRectangle(0.7972, 1.2862, 2.4441, 0.2669, SupportedLinearMeasure.IN), "combo1", "dd", true);
+ String[] items = { "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj" };
+ ((ComboBox) comboBox).addItems(items);
+
+
+ You can get an iterator of the combo box as follows. +
+
+			Iterator<FormControl> iterator = ComboBox.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ comboBox = (ComboBox) iterator.next();
+ }
+
+
RadioButton +
+ Below codes will create three radio buttons in a group, named "Group1", but each of them has different item value, from 1 to 3. +
+
+			FrameRectangle radioRtg = new FrameRectangle(0.7972, 1.2862, 2.4441, 0.2669, SupportedLinearMeasure.IN);
+ RadioButton radiobutton = (RadioButton) form.createRadioButton(doc, radioRtg, "Group1", "RadioButton 1", "1");
+ RadioButton radiobutton = (RadioButton) form.createRadioButton(doc, radioRtg, "Group1", "RadioButton 2", "2");
+ RadioButton radiobutton = (RadioButton) form.createRadioButton(doc, radioRtg, "Group1", "RadioButton 3", "3");
+
+
+ You can get an iterator of the radio button as follows. +
+
+			Iterator<FormControl> iterator = RadioButton.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ radioBtn = (RadioButton) iterator.next();
+ }
+
+
CheckBox +
+ Below code will create a check box and initialize its label and value in the last two parameters. +
+
+			FrameRectangle checkBoxRtg = new FrameRectangle(0.7972, 1.2862, 2.4441, 0.2669, SupportedLinearMeasure.IN);
+ CheckBox checkBox = (CheckBox) form.createCheckBox(doc, checkBoxRtg, "CheckBox 1", "This is choice 1", "1");
+
+
+ You can get an iterator of the check box as follows. +
+
+			Iterator<FormControl> iterator = CheckBox.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ checkBox = (CheckBox) iterator.next();
+ }
+
+
Date Field +
+ Below code will create a date field, set the spin button and drop-down button visible and also set the date format as "12/07/15". +
+
+			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
+ DateField dateField = (DateField)form.createDateField(para, fieldRtg, "DateField", "20120715");
+ dateField.setSpinButonVisible(true);
+ dateField.setDropDownVisible(true);
+ dateField.formatDate("yy/MM/dd", Locale.US);
+
+
+ You can get an iterator of the date field as follows. +
+
+			Iterator<FormControl> iterator = DateField.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ dateField = (DateField) iterator.next();
+ }
+
+
Time Field +
+ Below code will create a time field, set the spin button visible and set the date format as "15:23:40". +
+
+			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
+ TimeField timeField = (TimeField) form.createTimeField(para, fieldRtg, "TimeField", "15234000");
+ timeField.setSpinButonVisible(true);
+ timeField.formatTime("HH:mm a", Locale.US);
+
+
+ You can get an iterator of the time field as follows. +
+
+			Iterator<FormControl> iterator = TimeField.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ timeField = (TimeField) iterator.next();
+ }
+
+
Numeric Field +
+ Below code will create a numeric field, set the spin button visible and set the decimal accurcy to 3. +
+
+			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
+ NumericField numericField = (NumericField) form.createNumericField(para, fieldRtg, "NumericField", "-154.3567");
+ numericField.setDecimalAccuracy(3);
+ numericField.setSpinButonVisible(true);
+
+
+ You can get an iterator of the numeric field as follows. +
+
+			Iterator<FormControl> iterator = NumericField.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ numericField = (NumericField) iterator.next();
+ }
+
+
Pattern Field +
+ Below code will create a pattern field, set the spin button visible and set the literal mask and edit mask, which only allows 5 digits of numbers. +
+
+			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
+ PatternField patternField = (PatternField) form.createPatternField(para, fieldRtg, "PatternField", "12345");
+ patternField.setEditMask("NNLNNN");
+ patternField.setLiteralMask("##.###");
+ patternField.setSpinButonVisible(true);
+
+
+ You can get an iterator of the pattern field as follows. +
+
+			Iterator<FormControl> iterator = PatternField.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ patternField = (PatternField) iterator.next();
+ }
+
+
Currency Field +
+ Below code will create a currency field, set the spin button visible, set the decimal accurcy to 4 and use the 'CNY' as the currency symbol. +
+
+			FrameRectangle fieldRtg = new FrameRectangle(0.5, 2.0, 2.9433, 0.5567, SupportedLinearMeasure.IN);
+ CurrencyField currencyField = (CurrencyField) form.createCurrencyField(para, fieldRtg, "CurrencyField", "135.467");
+ currencyField.setCurrencySymbol("CNY");
+ currencyField.setDecimalAccuracy(4);
+ currencyField.setSpinButonVisible(true);
+
+
+ You can get an iterator of the currency field as follows. +
+
+			Iterator<FormControl> iterator = CurrencyField.getSimpleIterator(form);
+ while (iterator.hasNext()) {
+ currencyfield = (CurrencyField) iterator.next();
+ }
+
+

Size and Style +
+ If you want to handle more style settings of a form control like horizontal alignment, you can try ControlStyleHandler and GraphicProperties. +
+
+			ControlStyleHandler handler = button.getDrawControl().getStyleHandler();
+ GraphicProperties properties = handler.getGraphicPropertiesForWrite();
+ properties.setHorizontalPosition(StyleTypeDefinitions.FrameHorizontalPosition.FROMLEFT);
+
+
+ You can use FrameRectangle to change the position and size of a form control. +
+
+			FrameRectangle rtg = new FrameRectangle(0.01, 2.0, 5, 2,SupportedLinearMeasure.IN);
+ button.setRectangle(rtg);
+
+

+ + Index: document/cookbook/Manipulate TextSearch.mdtext =================================================================== --- document/cookbook/Manipulate TextSearch.mdtext (revision 1421066) +++ document/cookbook/Manipulate TextSearch.mdtext (working copy) @@ -23,6 +23,7 @@ }


TextSelection +

Get Index/Text of TextSelection
Run the following codes will get the text content of the searched String "good" and the corresponding index in the text document. @@ -68,23 +69,95 @@ pastesource.pasteAtEndOf(item);
}
-
Replace String +
Replace String with String
The following codes replace all the string "replacesource" with the string "replacedest" in the text document.

 		search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
- if (search3.hasNext()) {
- TextSelection item= (TextSelection) search3.nextSelection();
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith("replacedest");
}
+
Replace String with Paragraph +
+The following codes replace all the string "replacesource" with a paragraph which can be in the same document or in a different document. +
+
+		search2 = null;
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ Paragraph paragraph = textdoc.getParagraphByIndex(0, true);
+ item.replaceWith(paragraph);
+ }
+
+
Replace String with Image +
+The following codes replace all the string "replacesource" with an image. There are two ways to call the replaceWith method. You can use an image URI as parameter. +
+
+		search2 = null;
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ //Use URI as parameter. + item.replaceWith(new URI("image.png"));
+ }
+
+
+Or you can use an Image object as below: +
+
+			//Use Image as parameter.
+			item.replaceWith(Image.newImage(para, new URI("image.png")));
+
+
Replace String with Table +
+The following codes replace all the string "replacesource" with a table named "myTable" in the text document. +
+
+		search2 = null;
+ Table table = textdoc.getTableByName("myTable");
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ item.replaceWith(table);
+ }
+
+
Replace String with Field +
+The following codes replace all the string "replacesource" with a field named "myField" in the text document. +
+
+		search2 = null;
+ Field field = textdoc.getVariableFieldByName("myField");
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ item.replaceWith(field);
+ }
+
+
Replace String with TextDocument +
+The following codes replace all the string "replacesource" with the contents of the text document named replacedest.odt. +
+
+		search2 = null;
+ TextDocument destDoc=TextDocument.loadDocument("replacedest.odt");
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ item.replaceWith(destDoc);
+ }
+

Add Reference to String
To add reference for a string, you can do like the following codes. Here function addHref is used, the parameter of it is an URL object. The codes add network address "http:www.ibm.com" to the string "network".
-
+
 		search2 = null;
search2 = new TextNavigation("network", textdoc);
while (search2.hasNext()) {
@@ -96,7 +169,7 @@
Adding comment is a useful function when review document, such as spell check and security check. You can do it like the following codes. Here, function addComment is used, the first parameter is the comment content, the second parameter is the comment author. The codes add a spell suggestion before the string "natwork".
-
+
 		TextNavigation search4 = new TextNavigation("natwork", textdoc);
while (search4.hasNext()) {
TextSelection selection = (TextSelection) search4.nextSelection();
@@ -107,7 +180,7 @@
Field Selection is a decorator class of TextSelection, which help user replace a text content with field. Following code can be used to search the document content, and replace with a simple field.
-
+
 		TextDocument doc = TextDocument.loadDocument("fieldSample.odt");
TextNavigation search = new TextNavigation("ReplaceDateTarget", doc);
while (search.hasNext()) {
@@ -119,7 +192,7 @@
Following code can be used to search the document content, and replace with a condition field.
-
+
 		TextSelection item = (TextSelection) search.nextSelection();
FieldSelection fieldSelection = new FieldSelection(item);
fieldSelection.replaceWithConditionField("test_con_variable == \"true\"", "trueText", "falseText");
@@ -127,20 +200,20 @@
Following code can be used to replace with a hidden field.
-
+
 		fieldSelection.replaceWithHiddenTextField("test_con_variable == \"true\"", "hiddenText");

Following code can be used to replace with a reference field.
-
+
 		ReferenceField referenceField = Fields.createReferenceField(doc.addParagraph("span").getOdfElement(), "selection-test-ref");
fieldSelection.replaceWithReferenceField(referenceField, ReferenceField.DisplayType.TEXT);

Following code can be used to replace with a variable field.
-
+
 		VariableField userVariableField = Fields.createUserVariableField(doc, "selection_user_variable", "test");
fieldSelection.replaceWithVariableField(userVariableField);
@@ -148,7 +221,7 @@
Similar with TextNavigation, TextStyleNavigation has two main functions: getCurrentItem() and hasNext() which is shown in the following codes. The input parameter of TextStyleNavigation constructor is a map of OdfStyleProperty, so here a TreeMap "searchProps" which contains the Style properties is used to construct the TextStyleNavigation object.
-
+
 		TextStyleNavigation searchStyle1;
TreeMap<OdfStyleProperty, String> searchProps = new TreeMap<OdfStyleProperty, String>();
searchProps.put(StyleTextPropertiesElement.FontName, "Times New Roman1");
Index: document/cookbook/Table.mdtext =================================================================== --- document/cookbook/Table.mdtext (revision 1421066) +++ document/cookbook/Table.mdtext (working copy) @@ -94,6 +94,16 @@ boolean isProtected=table1.isProtected();
table1.setProtected(true);
+

Format Table +
+Since version 0.8, new APIs are added to format a table, which will load the formatting from a template table in a foreign document and apply them to corresponding cells of current table. The template table should be a 5*5 table with predefined formatting, includes number format, text format, text alignment, borders and background. The following code will load formatting from a foreign template table - Table1 in TemplateTable.odt, and apply the styles to a new table - Table2 in TargetTable.odt. +
+
+          Document doc = TextDocument.loadDocument("TargetTable.odt");
+ TableTemplate template = doc.LoadTableTemplateFromForeignTable(new FileInputStream("TableTemplate.odt"), "Table1");
+ Table table = doc.getTableByName("Table2");
+ table.applyStyle(template);
+