【问题标题】:ODFDom set Page Size, Page Orientation, and Page Margins of .odt Text documentODFDom 设置 .odt 文本文档的页面大小、页面方向和页边距
【发布时间】:2016-01-07 13:02:33
【问题描述】:

显然,ods 电子表格文档可以更改这些设置,但使用 odt 只能更改某些参数:

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);

例如如何设置页面方向?我在 odt 文档的 API 中找不到任何参考。

【问题讨论】:

  • 不幸的是,答案是相对于电子表格 (.ods) 而不是文本文档 (.odt)
  • 我认为它会有所帮助。这对我来说似乎是类似的问题。这就是我分享链接的原因。希望你能得到某人的答复。

标签: java odf odfdom


【解决方案1】:

您可以使用 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多