【问题标题】:How can the Page Size, Page Orientation, and Page Margins of an ods Spreadsheet Be Set Using ODFDOM?如何使用 ODFDOM 设置 ods 电子表格的页面大小、页面方向和页边距?
【发布时间】:2013-08-07 16:15:34
【问题描述】:

Apache 孵化项目ODFDOM 允许用户以编程方式读取和创建各种开放文档格式文件,包括电子表格。

我正在尝试使用他们重新改进的“简单 API”为我正在创建的电子表格设置各种打印选项,但似乎他们还没有公开一种简单的方法来修改文档属性,例如页边距、页面大小(高度/宽度)和页面方向(横向/纵向)。

我需要从 SpreadsheetDocument 获取允许我修改这些值的东西。

【问题讨论】:

    标签: java spreadsheet odf odftoolkit odfdom


    【解决方案1】:

    可以对 SpreadsheetDocument 提供访问的一些底层 ODF 对象进行必要的调用。首先,我们需要获取正确的文档属性引用(对于所有示例,“电子表格”是对创建的电子表格文档的引用):

        StyleMasterPageElement defaultPage = spreadsheet.getOfficeMasterStyles().getMasterPage("Default");
        String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute();
        OdfStylePageLayout pageLayoutStyle = defaultPage.getAutomaticStyles().getPageLayout(pageLayoutName);
        PageLayoutProperties pageLayoutProps = PageLayoutProperties.getOrCreatePageLayoutProperties(pageLayoutStyle);
    

    然后,我们可以设置各种属性,例如边距、方向和高度/宽度。请注意,页面方向值似乎需要高度和宽度值才能正常工作,并且高度广告宽度需要是所使用方向的高度和宽度:

        pageLayoutProperties.setPageHeight(pageHeightInMM);
        pageLayoutProperties.setPageWidth(pageWidthInMM);
        pageLayoutProperties.setPrintOrientation(PrintOrientation.LANDSCAPE);
        pageLayoutProperties.setMarginLeft(leftMarginInMM);
        pageLayoutProperties.setMarginRight(rightMarginInMM);
        pageLayoutProperties.setMarginTop(topMarginInMM);
        pageLayoutProperties.setMarginBottom(bottomMarginInMM);
    

    我基于another developer's notes 的基础上如何使用原始 ODFDOM API 执行此操作,并且能够使用此代码成功更改生成的文档的属性。

    【讨论】:

    • 如果您想为 TextDocument 执行此操作,请检查 spreadsheet.getOfficeMasterStyles().getMasterPage("Default");spreadsheet.getOfficeMasterStyles().getMasterPage("Standard"); 行以及对 TextDocument 引用的电子表格引用
    • 这是一些令人兴奋的建议。我正在使用 odfpy 做同样的事情,并且花了太多时间没有获得横向。
    • 哇,这该死的复杂而且有点无证。非常感谢@romeara!
    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2019-08-09
    相关资源
    最近更新 更多