How can the Page Size, Page Orientation, and Page Margins of an ods Spreadsheet Be Set Using ODFDOM?

安稳与你 提交于 2019-12-24 02:16:40

问题


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:


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.



来源:https://stackoverflow.com/questions/18108452/how-can-the-page-size-page-orientation-and-page-margins-of-an-ods-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!