本文介绍了如何使用ODFDOM设置ods电子表格的页面大小,页面方向和页边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试使用重新制作的简单API"为正在创建的电子表格设置各种打印选项,但是看来它们还没有提供修改文档属性(如页边距,页面)的简便方法大小(高度/宽度)和页面方向(横向/纵向).

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

我需要从 SpreadsheetDocument 可以修改这些值的地方.

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

推荐答案

可以对SpreadsheetDocument提供访问权限的某些基础ODF对象进行必要的调用.首先,我们需要获取正确的文档属性参考(对于所有示例,"spreadsheet"是对已创建的SpreadsheetDocument的引用):

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

我基于另一位开发人员的说明,说明如何使用原始ODFDOM API进行此操作,并能够使用此代码成功更改生成的文档的属性.

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.

这篇关于如何使用ODFDOM设置ods电子表格的页面大小,页面方向和页边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:27