I use Apache POI XWPF to create and handle MS Word
documents. But I didn't find in the documentation how to change the page orientation.
Apparently this way should make it:
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 doesn't work properly. I can set the page orientation to landscape, and when I read the page orientation in the code, I get landscape. All right. But if I open the saved document I've portrait format. This setting doesn't work in fact. What could be the problem?
As a workaround, I'm forced to start work with a blank document created manually in landscape or portrait format. But I want to create documents programmatically from scratch in needed orientation.
For instance POI HSSF and XSSF have functionality to toggle between landscape and portrait mode. It's setLandscape() method of org.apache.poi.ss.usermodel.PrintSetup
interface.
But what about XWPF
or HWPF
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…