Create a simple table document using the ODF Toolkit

If you have a task to make a couple of simple reports, and your users use Open Office, then you do not need to use large reporting systems like Bird, Jasper or their analogues.

The ODF Toolkit is great for this task.

ODF Toolkit , according to information from the official website, is a set of Java modules that allow you to create, scan or manipulate documents in the format of an open document (ISO / IEC 26300 == ODF). Unlike other approaches, which are based on runtime manipulations with heavy editors through the automation interface, the ODF Toolkit is lightweight and ideal for use on the server.

image

I work on a project to automate internal business processes and often users ask for reports on cumulative information, and due to the fact that Open Office is very common at the Agricultural Bank, Bird is used to build reports.

It allows you to generate any document format. In principle, a great solution, simple, understandable, but it became interesting whether it is possible to implement the same thing inside Java.

So I met with the ODF Toolkit.

I will describe a number of basic methods, you can learn more on the odftoolkit.org project page.

So, let's start by creating a document:

TextDocument doc = TextDocument.newTextDocument();

After that, let's move on to creating a paragraph, and if it's Apache Poi (a similar library, but only for the word format), then it's a few lines, and here everything is quite concise:

Paragraph subparagraphHeaderOne = doc.addParagraph(β€œβ€);

And if we want to assign the text in the next step, then we can use a similar construction:

Paragraph subparagrafFilial = paragraphHeader.addParagraph(null);

An example of creating an Apache poi paragraph:

XWPFParagraph paragraphHeader = xwpfDocument.createParagraph();
   XWPFRun subpargraphHeader = paragraphHeader.createRun();
        subpargraphHeader.setText(β€œβ€);
        subpargraphHeader.addBreak();

Personally, I am enthralled by such conciseness, but continue on.

Creating styles for a paragraph:

OdfOfficeStyles styles = doc.getOrCreateDocumentStyles ();
       OdfStyle style = styles.newStyle ( "Source Text", OdfStyleFamily.Paragraph );
          style.setProperty ( OdfParagraphProperties.VerticalAlign, "#middle");
          paragraph.getOdfElement().setStyleName("Source Text");

But there is a small problem, according to official documentation, style assignment happens like this:

paragraph.setStyleName("Source Text")

image

But unfortunately, it doesn’t work that way, which cost a certain amount of nerve cells and time to find a solution.

Creating a table:

Table TableOne = doc.addTable( , );

Working with cells is possible both by creating a new paragraph, and directly with the cell itself

By creating a new paragraph:

Paragraph cellOne = Paragraph.newParagraph(subparagraphTableOne.getCellByPosition(0, 1));
 cellOne.appendTextContent("");

Directly with a cell:

Cell cellOne = subparagraphTableOne.getCellByPosition(0, 0);
 cellOne.setStringValue("")

Working with a table style and its borders:

Create a border:

Border border= new Border(Color.WHITE, 2, StyleTypeDefinitions.SupportedLinearMeasure.PT);

The first variable is the choice of the border to which the style will be applied:

table.getRowByIndex(0).getCellByIndex(1).setBorders(StyleTypeDefinitions.CellBordersType.BOTTOM, border);

Set the font style:

Font font = new Font("Times New Roman", StyleTypeDefinitions.FontStyle.REGULAR, 12);

I want to end this short review, if the project interests you, then the odftoolkit.org project website has a more advanced guide for working with this API.

I hope this article helps someone go the simpler way.

All Articles