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)); // 设置页面高度