How can the Page Size, Page Orientation, and Page

2019-07-19 05:49发布

The Apache Incubation Project ODFDOM allows users to programmatically read and create various open document format files, including spreadsheets.

I am trying to set various print options for a spreadsheet I am creating, using their re-vamped "Simple API", however it does not appear they have yet exposed an easy way to modify document properties such as page margin, page size (height/width), and page orientation (landscape/portrait).

I need to get from a SpreadsheetDocument to something that will allow me to modify these values.

1条回答
何必那么认真
2楼-- · 2019-07-19 06:33

The necessary calls can be made to some of the underlying ODF objects which the SpreadsheetDocument provides access to. First, we need to get the proper document properties reference (for all examples, "spreadsheet" is a reference to a created SpreadsheetDocument):

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

Then, we can set the various properties, such as margins, orientation, and height/width. Note that the height and width values seem to be required for the page orientation value to work properly, and the height ad width need to be the height and width of the orientation being used:

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

I based this off of another developer's notes on how to do this with the original ODFDOM APIs, and was able to successfully change the generated document's properties using this code.

查看更多
登录 后发表回答