how to set page margins for word document using apache poi?

孤者浪人 提交于 2019-12-23 07:31:40

问题


I want to set page-margins for word document created using apache poi-3.9. I found it can be done using CTPageMar but CTPageMar is not being resolved. I am using apache poi-3.9

I tried this

CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(720L));
    pageMar.setTop(BigInteger.valueOf(1440L));
    pageMar.setRight(BigInteger.valueOf(720L));
    pageMar.setBottom(BigInteger.valueOf(1440L));

回答1:


As far as I understand, the problem is not related to the provided code, it is connected with missing CTPageMar class.

The source of missing classes problem is clearly explained in Apache POI FAQ:

To use the new OOXML file formats, POI requires a jar containing the file format XSDs [...] You can download the jar by hand from the POI Maven Repository.

URL from FAQ is not working, but you can find required jar in central maven repo.




回答2:


To resolve the class CTPageMar you will have to add the package org.apache.poi:ooxml-schema to your classpath. The corresponding JAR contains all schemas. You can find more information here.



来源:https://stackoverflow.com/questions/29791287/how-to-set-page-margins-for-word-document-using-apache-poi

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