79348550

Date: 2025-01-11 17:26:58
Score: 1
Natty:
Report link

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

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: gaolib