可以對SpreadsheetDocument允許訪問的某些基礎ODF對象進行必要的調用。首先,我們需要得到適當的文檔屬性引用(所有的例子,「電子表格」是一個創造SpreadsheetDocument參考):
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引用 – romeara
這是一些建議。我正在使用odfpy來做同樣的事情,並花費了太多時間沒有獲得橫向方向。 –