sarah.ferguson
sarah.ferguson

Reputation: 3257

ODFDom set Page Size, Page Orientation, and Page Margins of .odt Text document

Apparently these settings can be changed for ods spreadsheet documents, but with odt only certain parameters can be changed:

StyleMasterPageElement defaultPage = templateDocument.getOfficeMasterStyles().getMasterPage("Default");
String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute();
OdfStylePageLayout pageLayoutStyle = defaultPage.getAutomaticStyles().getPageLayout(pageLayoutName);
TextProperties textProperties = TextProperties.getOrCreateTextProperties(pageLayoutStyle);

textProperties.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD);

how to for instance set the Page Orientation? I cannot find any reference in the API for odt documents.

Upvotes: 0

Views: 795

Answers (1)

FredQc
FredQc

Reputation: 66

You can change page size and margins with PageLayoutProperties class.

About the page orientation, you can switch the width and height of the page to get the landscape orientation, but I am not sure if it is the right way to do it.

public static void main(String[] args) throws Exception
{
    TextDocument odt = TextDocument.newTextDocument(); // From the simple odftoolkit
    odt.addParagraph("Test text...");

    // Getting the page layout properties
    StyleMasterPageElement defaultPage = odt.getOfficeMasterStyles().getMasterPage("Standard");
    String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute();        
    OdfStylePageLayout pageLayoutStyle = defaultPage.getAutomaticStyles().getPageLayout(pageLayoutName);
    PageLayoutProperties pageLayoutProps = PageLayoutProperties.getOrCreatePageLayoutProperties(pageLayoutStyle);

    // Setting paper size "letter", portrait orientation
    pageLayoutProps.setPageWidth(215.9); // millimeter...
    pageLayoutProps.setPageHeight(279.4);
    odt.save(new File(System.getProperty("user.home"), "letter_portrait.odt"));

    // Setting paper size "letter", landscape orientation : just switch width/height...
    pageLayoutProps.setPageWidth(279.4);
    pageLayoutProps.setPageHeight(215.9);
    odt.save(new File(System.getProperty("user.home"), "letter_landscape.odt"));

    // Setting paper size "legal", portrait orientation
    pageLayoutProps.setPageWidth(216); 
    pageLayoutProps.setPageHeight(356);
    odt.save(new File(System.getProperty("user.home"), "legal_portrait.odt"));

    // And so on...

    // And you can also set the page margins
    pageLayoutProps.setMarginTop(10);
    pageLayoutProps.setMarginBottom(10);
    pageLayoutProps.setMarginRight(10);
    pageLayoutProps.setMarginLeft(10);
}

Upvotes: 1

Related Questions