2013-08-07 100 views
0

Apache孵化項目ODFDOM允許用戶以編程方式讀取和創建各種打開的文檔格式文件,包括電子表格。如何使用ODFDOM設置ods電子表格的頁面大小,頁面方向和頁邊距?

我想爲我創建的電子表格設置各種打印選項,使用它們重新獲得的「簡單API」,但是它沒有出現它們已經暴露出一種簡單的方式來修改文檔屬性,例如頁邊距,頁面大小(高度/寬度)和頁面方向(橫向/縱向)。

我需要從SpreadsheetDocument得到的東西,將允許我修改這些值。

回答

1

可以對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執行此操作,並能夠使用此代碼成功更改生成的文檔的屬性。

+0

如果您想爲TextDocument執行此操作,請檢查「spreadsheet.getOfficeMasterStyles()。getMasterPage(」Default「);'spreadsheet.getOfficeMasterStyles()。getMasterPage(」Standard「);'和電子表格引用TextDocument引用 – romeara

+0

這是一些建議。我正在使用odfpy來做同樣的事情,並花費了太多時間沒有獲得橫向方向。 –

相關問題