How to set page orientation for Word document?
Answer is right.
I just had to add extra dependencies to be able to access CTPageSz class.
// SBT config
"org.apache.poi" % "poi-ooxml" % "4.1.0", // Base library
"org.apache.poi" % "ooxml-schemas" % "1.4", // required to access CTPageSz
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