0

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.

4

1 回答 1

0

您可以使用 PageLayoutProperties 类更改页面大小和边距。

关于页面方向,您可以切换页面的宽度和高度以获得横向方向,但我不确定这是否是正确的做法。

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);
}
于 2017-09-21T20:36:59.767 回答