gaolib
gaolib

Reputation: 21

When setting paragraph sections in Poi, Word will add an extra blank page

Currently, after using Poi to draw a table in Word and setting the page containing the table to display horizontally, an extra blank page appears after that page. If you delete the blank page, the page with the table will no longer display horizontally.

Snapshot showing extra page

How to use Poi to set the current page of Word to display horizontally without an extra blank page appearing below it?
this is code :

XWPFParagraph para = doc.createParagraph();
CTSectPr sectPr = para.getCTP().addNewPPr().addNewSectPr();
CTPageSz pageSz = sectPr.addNewPgSz();
pageSz.setOrient(STPageOrientation.LANDSCAPE);

Snapshot before deleting extra page

Snapshot after deleting extra page

Upvotes: 1

Views: 27

Answers (1)

gaolib
gaolib

Reputation: 21

You can set global landscape orientation starting from a specific page, which will make all subsequent pages default to landscape orientation. If you want a particular page to be in portrait orientation, you can create a section and set the section properties to portrait orientation.

Code for setting the entire document to landscape orientation globally:

XWPFDocument document = new XWPFDocument(); CTBody body = document.getDocument().getBody(); CTSectPr sectPr = body.addNewSectPr(); CTPageSz pageSize = sectPr.addNewPgSz(); pageSize.setOrient(STPageOrientation.LANDSCAPE); pageSize.setW(BigInteger.valueOf(16838)); // 设置页面宽度 pageSize.setH(BigInteger.valueOf(11906)); // 设置页面高度

Setting a specific page to landscape orientation through paragraph settings can result in an additional blank page.

XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); CTPPr pPr = paragraph.getCTP().addNewPPr(); CTSectPr sectPr = pPr.addNewSectPr(); CTPageSz pageSize = sectPr.addNewPgSz(); pageSize.setOrient(STPageOrientation.LANDSCAPE); pageSize.setW(BigInteger.valueOf(16838)); // 设置页面宽度 pageSize.setH(BigInteger.valueOf(11906)); // 设置页面高度

Upvotes: 1

Related Questions