Setting the page size and orientation with ODFDOM for tables aka spreadsheets

Most tasks that seem to be quite easy tend to turn out as a desaster. Setting the page size and orientation with ODFDOM was such a thing. While styling with ODFDOM is powerfull and a horror at the same time. You actually never know where the attribute have to be placed or which attributes to use.

After I finished an export to “ods” (aka spreadsheet) it simply wanted to set the format to “A4 landscape”. Here is how to do that:

First create your spreadsheet and sheet ( you might already have done that):


OdfSpreadsheetDocument output = OdfSpreadsheetDocument.newSpreadsheetDocument ();
final OdfTable sheet = OdfTable.newTable ( output );

Now change the styles to “A4 landscape”:

First we need to get the “master page” named “Default”:


StyleMasterPageElement defaultPage = output.getOfficeMasterStyles ().getMasterPage ( "Default" );

The master page tells us the name of the page style:


String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute ();

Which gives us the page layout object:


OdfStylePageLayout pageLayout = defaultPage.getAutomaticStyles ().getPageLayout ( pageLayoutName );

Finally we can set “A4 landscape”:


pageLayout.setProperty ( OdfPageLayoutProperties.PrintOrientation, "landscape" );
pageLayout.setProperty ( OdfPageLayoutProperties.PageHeight, "210.01mm" );
pageLayout.setProperty ( OdfPageLayoutProperties.PageWidth, "297mm" );
pageLayout.setProperty ( OdfPageLayoutProperties.NumFormat, "1" );

All four properties seem to be required. Also the width and height have to be rotatate according to “landscape”.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

One thought on “Setting the page size and orientation with ODFDOM for tables aka spreadsheets”