Documents > Cookbook >Fields
Variable Field
You can use the following code to create a variable field, and set the value.
TextDocument doc = TextDocument .newTextDocument();
Paragraph paragraph = doc.addParagraph("test_con_variable:");
VariableField simpleVariableField = Fields .createSimpleVariableField(doc, "test_con_variable");
simpleVariableField.updateField("true", paragraph.getOdfElement());
Following code can be used to set value to variable field, and append it to an ODF element.
simpleVariableField.updateField("user variable content", null);
simpleVariableField.displayField(paragraph.getOdfElement());
Condition Field
Following code can be used to create a condition field.
Paragraph newParagraph = doc.addParagraph("Condition Field Test :");
ConditionField conditionField = Fields .createConditionField(newParagraph.getOdfElement(), "test_con_variable == \"true\"",
"trueText", "falseText");
Hidden Field
Following code can be used to create a hidden field.
newParagraph = doc.addParagraph("Hide Text Field Test :");
conditionField = Fields .createHiddenTextField(newParagraph.getOdfElement(), "test_con_variable == \"true\"", "hiddenText");
Cross Reference Field
Following code can be used to create a reference field.
OdfElement newTextSpanElement = ((TextPElement )doc.addParagraph("Reference Content :").getOdfElement()).newTextSpanElement();
newTextSpanElement.setTextContent("This is a test reference content.");
ReferenceField referenceField = Fields .createReferenceField(newTextSpanElement, "test-ref");
Following code can be used to append a reference field.
referenceField.appendReferenceTo(doc.addParagraph("User Reference Field :").getOdfElement(), ReferenceField .DisplayType .TEXT );
Chapter Field
Following code can be used to create a chapter field.
ChapterField chapterField = Fields .createChapterField(doc.addParagraph("Chapter :").getOdfElement());
Title and Subject Field
Following code can be used to create a title field.
TitleField titleField = Fields .createTitleField(doc.addParagraph("The Title :").getOdfElement());
Following code can be used to create a subject field.
SubjectField subjectField = Fields .createSubjectField(doc.addParagraph("The Subject :").getOdfElement());
Author Field
Following code can be used to create a author initial field and a author name field.
AuthorField authorField = Fields .createAuthorInitialsField(doc.addParagraph("The initials of the author :").getOdfElement());
authorField = Fields .createAuthorNameField(doc.addParagraph("Author :").getOdfElement());
Page Number Field
Following code can be used to create a current page number field.
PageNumberField numberField = Fields .createCurrentPageNumberField(doc.addParagraph("Current Page Number :").getOdfElement());
numberField.setNumberFormat(NumberFormat .UPPERCASE_LATIN_ALPHABET);
numberField.setDisplayPage(DisplayType .NEXT_PAGE);
Following code can be used to create a previous page number and a next page number field.
numberField = Fields .createPreviousPageNumberField(doc.addParagraph("Previous Page Number :").getOdfElement());
numberField = Fields .createNextPageNumberField(doc.addParagraph("Next Page Number :").getOdfElement());
Page Number Field
Following code can be used to create a page count field, and set the number format.
PageCountField countField = Fields .createPageCountField(doc.addParagraph("Page Count :").getOdfElement());
countField.setNumberFormat(NumberFormat .UPPERCASE_LATIN_ALPHABET);
Date Field
Following code can be used to create a date field, and set the format.
DateField dateField = Fields .createDateField(doc.addParagraph("Date :").getOdfElement());
dateField.formatDate("yy-MM -dd");
Time Field
Following code can be used to create a time field, and set the format.
TimeField timeField = Fields .createTimeField(doc.addParagraph("Time :").getOdfElement());
timeField.formatTime("HH :mm:ss a");