Demos > Automatically Formatting a Document


Simple API enhanced features to support document formatting in version 0.6.5. It supplies methods for manipulating headings, page breaks, hyperlinks, comments, font and alignment. This demo shows how to use these features to help document formatting.

It's a common scenario that we need apply some styles to plain text to improve its appearance. In this demo, a text document is created using plain text from a text file. While reading content, paragraphs whose length is less than 20 characters are changed to headings with a new font style. "Version" and "date" information is set as right alignment and gray text. Each line which matches an URL will be applied as a hyperlink. Each heading and its following paragraphs are considered as a chapter. There will be a page break after page line count larger than a predefined value. Then verify word spelling and add comments as tip to those which may have a spelling mistake. The last one is a security check. After these processes, the plain text has been changed into a formatted document.

This picture shows part of the generated document. The new document looks orderly and beautiful than before.

alt text

Code Introduction

There code of this demo is very clear. Firstly, we open the data source and create a text document. Secondly, we create paragraph for each line. Thirdly, we set different style for different content. Then we do spell and security check with the help of Navigation API. Comments are given if suspicious words are found. Finally, the new created document is saved.

        BufferedReader reader = new BufferedReader(new InputStreamReader(this.class.getResourceAsStream("text.txt")));
        String in = reader.readLine();
        TextDocument doc = TextDocument.newTextDocument();
        int lineCount = 0;
        int pageLineCount = 0;
        Paragraph refParagraph = null;
        while (in != null) {
            in = in.trim();
            Paragraph paragraph = doc.addParagraph(in);
            switch (lineCount) {
            case 1:
                paragraph.setFont(new Font("Arial", FontStyle.BOLD, 16));
            case 2:
            case 3:
                paragraph.setFont(new Font("Tahoma", FontStyle.ITALIC, 10,
                if (in.startsWith("http://")) {
                    paragraph.applyHyperlink(new URI(in));
                if (in.length() < 20) {
                    paragraph.setFont(new Font("Arial", FontStyle.BOLD, 12));
                    if (pageLineCount > 16) {
                        pageLineCount = 0;
            refParagraph = paragraph;
            in = reader.readLine();

        // spell check
        TextNavigation navigation1 = new TextNavigation("lower-level", doc);
        while (navigation1.hasNext()) {
            TextSelection selection = (TextSelection) navigation1
                    "Please change 'lower-level' with 'lower level'.",
        // security check
        TextNavigation navigation2 = new TextNavigation("confidential", doc);
        if (navigation2.hasNext()) {
            TextSelection selection = (TextSelection) navigation2
                            "This is a confidential document, please don't redistribute.",


Powered by the Simple Java API for ODF version 0.6.5.
You can download the code of this demo from here.

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.