POI 怎么设置Word纸张为A3

IAM_YXQ 2015-08-19 11:02:46
实现下载word文件功能,用户可选择A4版式和A3横向分栏版式。因为poi默认生成的word是A4版式的,如何改成A3 横向版啊
...全文
402 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
IAM_YXQ 2015-08-20
引用 7 楼 rui888 的回复:
我也 没做过。网上找找解决方案吧。
网上都找遍了,实在是找不到了。不过如何设置纸张,你还是帮了很大的忙。
回复
兔子托尼啊 2015-08-20
我也 没做过。网上找找解决方案吧。
回复
IAM_YXQ 2015-08-20
分栏 已经知道了: if(!section.isSetCols()) { section.addNewCols(); } CTColumns columns=section.getCols(); columns.setNum(BigInteger.valueOf(2)); //文档分栏,2栏 可是如何用XWPFDocument将HTML格式导出到Word,HWPFDocument的我知道怎么用。 好纠结啊,使用HWPFDocument不知道怎么设置纸张和分栏,使用XWPFDocument不知道怎么将HTML格式导出到Word。
回复
兔子托尼啊 2015-08-19
这里 接上面的。 I use Apache POI XWPF to create and handle MS Word documents. But I didn't find in the documentation how to change the page orientation. Apparently this way should make it: XWPFDocument doc = new XWPFDocument(); CTDocument1 document = doc.getDocument(); CTBody body = document.getBody(); if (!body.isSetSectPr()) { body.addNewSectPr(); } CTSectPr section = body.getSectPr(); if(!section.isSetPgSz()) { section.addNewPgSz(); } CTPageSz pageSize = section.getPgSz(); pageSize.setOrient(STPageOrientation.LANDSCAPE); But this method doesn't work properly. I can set the page orientation to landscape, and when I read the page orientation in the code, I get landscape. All right. But if I open the saved document I've portrait format. This setting doesn't work in fact. What could be the problem? As a workaround, I'm forced to start work with a blank document created manually in landscape or portrait format. But I want to create documents programmatically from scratch in needed orientation. For instance POI HSSF and XSSF have functionality to toggle between landscape and portrait mode. It's setLandscape() method of org.apache.poi.ss.usermodel.PrintSetup interface. But what about XWPF or HWPF?
回复
兔子托尼啊 2015-08-19
You were very much on the right path. Setting the orientation to landscape describes the general orientation of the paper, but will still need the size of the paper. Your CTPageSz object doesn't have that yet. This means in addition to your setOrient call, you'll need to both setW and setH. These calls take BigIntegers that are representative of 1/20 Point. For a landscaped LETTER type paper therefore, you'll just: pageSize.setW(BigInteger.valueOf(15840)); pageSize.setH(BigInteger.valueOf(12240)); For Word to recognize it as Landscaped, the width must be greater than the height. You still want to keep the setOrient call as well if you want it to behave properly when going to print. Here's some common paper sizes in points from https://www.gnu.org/software/gv/manual/html_node/Paper-Keywords-and-paper-size-in-points.html you should take these and multiply them by twenty to use in the above method calls Letter 612x792 LetterSmall 612x792 Tabloid 792x1224 Ledger 1224x792 Legal 612x1008 Statement 396x612 Executive 540x720 A0 2384x3371 A1 1685x2384 A2 1190x1684 A3 842x1190 A4 595x842 A4Small 595x842 A5 420x595 B4 729x1032 B5 516x729 Folio 612x936 Quarto 610x780 10x14 720x1008
回复
兔子托尼啊 2015-08-19
static final short A3_PAPERSIZE A3 - 297x420 mm See Also: Constant Field Values Method Detail setPaperSize void setPaperSize(short size) Set the paper size. Parameters: size - the paper size. 参考
回复
引用 3 楼 rui888 的回复:
这里 接上面的。 I use Apache POI XWPF to create and handle MS Word documents. But I didn't find in the documentation how to change the page orientation. Apparently this way should make it: XWPFDocument doc = new XWPFDocument(); CTDocument1 document = doc.getDocument(); CTBody body = document.getBody(); if (!body.isSetSectPr()) { body.addNewSectPr(); } CTSectPr section = body.getSectPr(); if(!section.isSetPgSz()) { section.addNewPgSz(); } CTPageSz pageSize = section.getPgSz(); pageSize.setOrient(STPageOrientation.LANDSCAPE); But this method doesn't work properly. I can set the page orientation to landscape, and when I read the page orientation in the code, I get landscape. All right. But if I open the saved document I've portrait format. This setting doesn't work in fact. What could be the problem? As a workaround, I'm forced to start work with a blank document created manually in landscape or portrait format. But I want to create documents programmatically from scratch in needed orientation. For instance POI HSSF and XSSF have functionality to toggle between landscape and portrait mode. It's setLandscape() method of org.apache.poi.ss.usermodel.PrintSetup interface. But what about XWPF or HWPF?
大牛,我来学习
回复
IAM_YXQ 2015-08-19
引用 3 楼 rui888 的回复:
这里 接上面的。 I use Apache POI XWPF to create and handle MS Word documents. But I didn't find in the documentation how to change the page orientation. Apparently this way should make it: XWPFDocument doc = new XWPFDocument(); CTDocument1 document = doc.getDocument(); CTBody body = document.getBody(); if (!body.isSetSectPr()) { body.addNewSectPr(); } CTSectPr section = body.getSectPr(); if(!section.isSetPgSz()) { section.addNewPgSz(); } CTPageSz pageSize = section.getPgSz(); pageSize.setOrient(STPageOrientation.LANDSCAPE); But this method doesn't work properly. I can set the page orientation to landscape, and when I read the page orientation in the code, I get landscape. All right. But if I open the saved document I've portrait format. This setting doesn't work in fact. What could be the problem? As a workaround, I'm forced to start work with a blank document created manually in landscape or portrait format. But I want to create documents programmatically from scratch in needed orientation. For instance POI HSSF and XSSF have functionality to toggle between landscape and portrait mode. It's setLandscape() method of org.apache.poi.ss.usermodel.PrintSetup interface. But what about XWPF or HWPF?
生成A3纸基本已经实现了,可是怎么设置分栏呢?
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-19 11:02
社区公告
暂无公告