sama
sama

Reputation: 19

Avoid Word displaying blank page at the beginning of .doc generated by Apache POI

I use Apache POI in a Java project. I have worked on a landscape page, with the following code:

private void changeOrientation (XWPFDocument document, String orientation) 
{
CTDocument1 doc = document.getDocument ();
CTBody body = doc.getBody ();
CTSectPr section = body.addNewSectPr ();
XWPFParagraph para = document.createParagraph ();
CTP ctp = para.getCTP ();
CTPPr br = ctp.addNewPPr ();
br.setSectPr (section);
CTPageSz pageSize;
if (section.isSetPgSz ()) {
pageSize = section.getPgSz ();
}   else {
pageSize = section.addNewPgSz ();
}
pageSize.setOrient (STPageOrientation.LANDSCAPE);
if (orientation.equals ( "landscape")) {
pageSize.setOrient (STPageOrientation.LANDSCAPE);
pageSize.setW (BigInteger.valueOf (842 * 20));
pageSize.setH (BigInteger.valueOf (595 * 20));
}
 else {
pageSize.setOrient (STPageOrientation.PORTRAIT);   
pageSize.setH (BigInteger.valueOf (842 * 20));
pageSize.setW (BigInteger.valueOf (595 * 20));
}
}

I call the method after creating the document

private void dipl()
{
XWPFDocument document = new XWPFDocument ();
String landscape = "landscape";
changeOrientation (document, landscape);
} // ......

The problem is Word displays a blank portrait page at the beginning of the document before the landscape page. So, how can I avoid creating the blank page?

Upvotes: 1

Views: 874

Answers (1)

Axel Richter
Axel Richter

Reputation: 61852

The default section properties for the Word document are set in body only, not in a paragraph. If section properties are in a paragraph, then this are properties for additional sections, for example if the document contains parts in landscape and parts in portrait format.

The minimal working example for creating a Word document having letter paper size in landscape format only is:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class CreateWordLandscape {
 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  CTDocument1 ctDocument = document.getDocument();
  CTBody ctBody = ctDocument.getBody();
  CTSectPr ctSectPr = (ctBody.isSetSectPr())?ctBody.getSectPr():ctBody.addNewSectPr();
  CTPageSz ctPageSz = (ctSectPr.isSetPgSz())?ctSectPr.getPgSz():ctSectPr.addNewPgSz();
  ctPageSz.setOrient(STPageOrientation.LANDSCAPE);
  //paper size letter
  ctPageSz.setW(java.math.BigInteger.valueOf(Math.round(11 * 1440))); //11 inches
  ctPageSz.setH(java.math.BigInteger.valueOf(Math.round(8.5 * 1440))); //8.5 inches

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");

  FileOutputStream out = new FileOutputStream("CreateWordLandscape.docx");  
  document.write(out);
  out.close();
  document.close();

 }
}

Upvotes: 2

Related Questions