Index: document/cookbook/Fields.mdtext =================================================================== --- document/cookbook/Fields.mdtext (revision 1421066) +++ document/cookbook/Fields.mdtext (working copy) @@ -2,7 +2,7 @@
+ TextDocument textDoc = TextDocument.newTextDocument();+
+ Form form = textDoc.createForm("Form1");
+
+ Iterator<Form> iterator = textDoc.getFormIterator();+
+ while (iterator.hasNext()) {
+ deleteForm = iterator.next();
+ if (deleteForm.getFormName().equals("Form1"))
+ break;
+ }
+ textDoc.removeForm(deleteForm);
+
+ 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");
+
+ Iterator<FormControl> iterator = Button.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ btn = (Button) iterator.next();
+ }
+
+ 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.");
+
+ Iterator<FormControl> iterator = Label.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ label = (Label) iterator.next();
+ } +
+ 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);
+
+ Iterator<FormControl> iterator = TextBox.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ textBox = (TextBox) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = ListBox.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ listBox = (ListBox) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = ComboBox.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ comboBox = (ComboBox) iterator.next();
+ }
+
+ 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");
+
+ Iterator<FormControl> iterator = RadioButton.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ radioBtn = (RadioButton) iterator.next();
+ }
+
+ 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");
+
+ Iterator<FormControl> iterator = CheckBox.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ checkBox = (CheckBox) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = DateField.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ dateField = (DateField) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = TimeField.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ timeField = (TimeField) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = NumericField.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ numericField = (NumericField) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = PatternField.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ patternField = (PatternField) iterator.next();
+ }
+
+ 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);
+
+ Iterator<FormControl> iterator = CurrencyField.getSimpleIterator(form);+
+ while (iterator.hasNext()) {
+ currencyfield = (CurrencyField) iterator.next();
+ }
+
+ ControlStyleHandler handler = button.getDrawControl().getStyleHandler();+
+ GraphicProperties properties = handler.getGraphicPropertiesForWrite();
+ properties.setHorizontalPosition(StyleTypeDefinitions.FrameHorizontalPosition.FROMLEFT);
+
+ FrameRectangle rtg = new FrameRectangle(0.01, 2.0, 5, 2,SupportedLinearMeasure.IN);+
+ button.setRectangle(rtg);
+
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");
}
+ search2 = null;+
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ Paragraph paragraph = textdoc.getParagraphByIndex(0, true);
+ item.replaceWith(paragraph);
+ }
+
+ 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"));
+ }
+
+ //Use Image as parameter. + item.replaceWith(Image.newImage(para, new URI("image.png")));+
+
+ search2 = null;+
+ Table table = textdoc.getTableByName("myTable");
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ item.replaceWith(table);
+ }
+
+ search2 = null;+
+ Field field = textdoc.getVariableFieldByName("myField");
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ item.replaceWith(field);
+ }
+
+ search2 = null;
+ TextDocument destDoc=TextDocument.loadDocument("replacedest.odt");
+ search2 = new TextNavigation("replacesource", textdoc);
+ while (search2.hasNext()) {
+ TextSelection item= (TextSelection) search2.nextSelection();
+ item.replaceWith(destDoc);
+ }
+
+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");@@ -148,7 +221,7 @@
fieldSelection.replaceWithVariableField(userVariableField);
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);
+