There are typically four kinds of Text Documents with different modes can be generated: text document, master document, template document and web document.
The following codes generates an empty text document.
We can get the existing Text Document by using the loadDocument function like follows. The loadDocument function has four kinds of parameters: File, InputStream, OdfPackageDocument and String. Here the one with string parameter is used as an example.
From version 0.8, you can copy contents from a source TextDocument and then insert them to the current TextDocument. The insert position is relative to a reference Paragraph, before or after. You can also choose to copy the styles with the contents or not.
TextDocument src = TextDocument.loadDocument("SourceDocument.odt");
TextDocument target = TextDocument.loadDocument("TargetDocument.odt");
Paragraph p1 = target.getParagraphByIndex(0, true);
// insert contents before p1 and copy styles
target.insertContentFromDocumentBefore(src, p1, true);
// insert contents after p1 and don't copy styles
target.insertContentFromDocumentAfter(src, p1, false);
To add a new paragraph to a text document, the following codes can be used, the first one add an empty paragraph to the end of the document, and the second one add a new paragraph with the corresponding texts to the document.
document1.addParagraph(null);
document1.addParagraph("test newParagraph function in textDocument");
The first one will get the second paragraph of this document. The second statement will get the last paragraph with text content. The paragraph without text content will be ignored if the second parameter is true.
The following codes can be used to append text to the end of a paragrah.
para1.appendTextContent("test addText function in textDocument");
The following codes are to get and set the horizontal alignment of a paragraph.
The following codes are to get and set the font of a paragraph.
Font font = para1.getFont();
font.setFontStyle(StyleTypeDefinitions.FontStyle.ITALIC);
para2.setFont(font);
The following code is to apply a hyperlink to this paragraph, and append a text with a hyperlink to this paragraph.
para2.applyHyperlink(newURI("mailto:daisy@odftoolkit.org"));
para2.appendHyperlink("mail to me",new URI("mailto:daisy@odftoolkit.org"));
The following code is to add a comment at the front of the paragraph.
para2.addComment("This is a comment for para2", "SimpleODFCommenter");
From version 0.6.5, we support heading feature. You can test whether a paragraph is heading, apply a plain text paragraph as heading, and get heading level.
// isHeading() and getHeadingLevel();if (para2.isHeading()) { int headingLevel = para2.getHeadingLevel(); System.out.println("para2 is a heading, its level is " + headingLevel + ".");
} // applyHeading(), default level is 1.
para1.applyHeading(); // applyHeading(), heading level is 3.
para1.applyHeading(true, 3);
After you get the object of section, you can use the following codes to set and get the name of the section.
String name = aSection.getName();
aSection.setName("NewName");
You can use the following code to copy and append a section at the end of a document. Copying from a foreign document is supported. You don't need to care whether or not the document is the owner of the source section.
document1.appendSection(aSection, true);
If you want to copy and append a section within a same document, you can specify with the second parameter of "copyAppendSection(Section section, boolean isResourceCopied)" whether the linked resources to this section are copied or not. The following code means to copy a section and append it at the end, but the linked resources are shared between the source section and the copied section.
document1.appendSection(aSection, false);
You can remove this Section from the document content, all the resources that are only linked with this section would be removed too.
aSection.remove();
From version 0.8, we support high level APIs to protect a section. You can use the following code to set the protection without a password:
aSection.setProtected(true);
If you want to protect a section with a password, you can use the following code.
From version 0.4, we support high level APIs for list. You can use the following code to create a list. The two methods are same.
List newList1 = document1.addList(); List newList2 = newList(document1);
Actually, you can append list to table cell, list item, presentation slide and notes. They all have the same ability with text document for list. We call them ListContainer, which can append, remove and get the iterator of list.
Iterator<List> lists = document1.getListIterator(); List list =null; while (lists.hasNext()) {
list = lists.next();
}
After you get the object of list, you can use the following methods to set and get the header of the list.
You can use the following methods to remove item(s).
list.removeItem(item); //item in specific location.
list.removeItem(location); //item in specific collection.
list.removeItems(items);
You can specify with the following methods whether the numbering of the previous list is continued by this list:
// the numbering of the proximate list is continued
newList1.setContinueNumbering(true); // the numbering of the specified list is continued by this list
newList2.setContinueList(list);
You can use the following method to know whether the list is a number list.
ListType type = list.getType();
Now, List API support 3 types of lists, ListType.BULLET, ListType.NUMBER and ListType.IMAGE. The default created list is ListType.BULLET, bullet list. How to create a number list or a image list then? ListDecorator has powerful functions that can help you. ListDecorator is an interface which decides how to decorate a list and its list items. We supply 4 implementations of this interface, BulletDecorator, NumberDecorator, ImageDecorator and OutlineDecorator.
// create a number list.ListDecorator numberDecorator = newNumberDecorator(document1); List numberList1 = document1.addList(numberDecorator); List numberList2 = newList(document1, numberDecorator);
You can implement your own ListDecorator as need, or extend the default four implementations.
You can remove a list from the document, the following two methods are same.
list.remove();
document1.removeList(list);
We also provide funtions to manipulate list items. You can use the following methods to get and set the text content of a item.
In order to add contents to header and arrange these contents in a good layout, I suggest you to use table. It's easy to add a none-border table to header. Below codes show how to add a table with 2 columns and 1 row to header, and then add some text content to the left cell, and add an image to the right cell.
Below codes show how to add a table with 1 column and 1 row to footer, and then add some text content to the cell. You can easily set the style of these text content. The codes describe how to put the text content in the center, set the font of the text content, and set the background of the cell.
From version 0.5, we support high level APIs for text box. Below codes will create a text box and add it to the text document with a paragraph as the anchor position. The FrameRectangle specifies an area and the position of this text box.
From version 0.5.5, we support high level APIs for images. Below codes will create a image and add it to a document with a paragraph as the anchor position.
Paragraph para = doc.getParagraphByIndex(1, false); Image image = Image.newImage(para, newURI("file:/c:/image.jpg"));
Below codes can set the properties of image, and even the vertical (or horizontal) alignment.
image.setTitle("Image title");
image.setDescription("This is a sample image");
image.setVerticalPosition(FrameVerticalPosition.TOP);
image.setHorizontalPosition(FrameHorizontalPosition.RIGHT);
If you want to add a hyperlink to your image, you can use the following code:
From version 0.5.5, we support high level APIs for span. You can create a span based on a text selection. With span, you can set a different style to this small unit.
From version 0.6.5, we support page break feature. You can add page break at the end of text document or after a reference paragraph. It may help you well format your document.
TextDocument newDoc = TextDocument.newTextDocument(); //add a page break at the end of document.
newDoc.addPageBreak(); //add a page break at the end of reference paragraph.Paragraph refParagraph = newDoc.addParagraph("before page break");
newDoc.addPageBreak(refParagraph);
From version 0.8, we support to appoint the page style for the page that follows the added page break. You can get a page style by the name and use it as one parameter when adding a page break.
From version 0.8, we support high level APIs to add table of content (TOC). You can add TOC before/after a reference paragraph by following code. The first parameter is the reference paragraph, the second parameter specifies where to insert the TOC, "true" means before the reference paragraph, "false" means after the reference paragraph.
You can also specify the additional paragraph styles applied on the created TOC. The following codes shows how to generate a TOC before a reference paragraph with customized styles.