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);

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());

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());

Date Field
Following code can be used to create a date field, and set the format.

            DateField dateField = Fields.createDateField(doc.addParagraph("Date:").getOdfElement());

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");

Powered by the Apache CMS.

Apache "ODF Toolkit" is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2011 The Apache Software Foundation Licensed under the Apache License, Version 2.0. Contact Us
Apache and the Apache feather logos are trademarks of The Apache Software Foundation.
Other names appearing on the site may be trademarks of their respective owners.