I use Apache POI XWPF to create and process MS Word documents. But I did not find in the documentation how to change the page orientation.
Obviously, this method should do this:
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 does not work correctly. I can set the page orientation to landscape, and when I read the page orientation in the code, I get a landscape. Fine. But if I open the saved document, I have a portrait format. This parameter does not work. What could be the problem?
As a workaround, I have to start working with a blank document created manually in landscape or portrait format. But I want to create documents programmatically from scratch in the right orientation.
For example, the HSSF and XSSF POIs have functionality for switching between landscape and portrait modes. This is the setLandscape () interface method org.apache.poi.ss.usermodel.PrintSetup .
But what about XWPF or HWPF ?
java apache-poi orientation-changes landscape-portrait xwpf
kapand
source share