Java Code Examples for org.docx4j.wml.PPr#setSectPr()

The following examples show how to use org.docx4j.wml.PPr#setSectPr() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Docx4j_Helper.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void testDocx4jSetPageSize() throws Exception {  
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();  
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();  
  
    String titleStr="静夜思    李白";  
    String str="床前明月光,疑似地上霜。";  
    String str2="举头望明月,低头思故乡。";  
    P p = Docx4j_Helper.factory.createP();  
    String rprStr = "<w:rPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rFonts w:hint=\"eastAsia\" w:ascii=\"Times New Roman\" w:hAnsi=\"Times New Roman\" w:eastAsia=\"宋体\"/><w:b/><w:color w:val=\"333333\"/><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>";  
    RPr rpr = (RPr) XmlUtils.unmarshalString(rprStr);  
    setParagraphContent(p, rpr,titleStr);  
    mdp.addObject(p);  
      
    p = Docx4j_Helper.factory.createP();  
    setParagraphContent(p, rpr,str);  
    mdp.addObject(p);  
      
    p = Docx4j_Helper.factory.createP();  
    PPr pPr=Docx4j_Helper.factory.createPPr();  
    //设置文字方向  
    SectPr sectPr = Docx4j_Helper.factory.createSectPr();  
    TextDirection textDirect = Docx4j_Helper.factory.createTextDirection();  
    //文字方向:垂直方向从右往左  
    textDirect.setVal("tbRl");  
    sectPr.setTextDirection(textDirect);  
    Type sectType = Docx4j_Helper.factory.createSectPrType();  
    //下一页  
    sectType.setVal("nextPage");  
    sectPr.setType(sectType);  
    //设置页面大小  
    PgSz pgSz =  Docx4j_Helper.factory.createSectPrPgSz();  
    pgSz.setW(new BigInteger("8335"));  
    pgSz.setH(new BigInteger("11850"));  
    sectPr.setPgSz(pgSz);  
    pPr.setSectPr(sectPr);  
    p.setPPr(pPr);  
    setParagraphContent(p, rpr,str2);  
    mdp.addObject(p);  
      
    p = createParagraphWithHAlign();  
    setParagraphContent(p, rpr,titleStr);  
    mdp.addObject(p);  
      
    p = createParagraphWithHAlign();  
    setParagraphContent(p, rpr,str);  
    mdp.addObject(p);  
      
    p = createParagraphWithHAlign();  
    setParagraphContent(p, rpr,str2);  
    mdp.addObject(p);  
   // Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);  
}
 
Example 2
Source File: FOPAreaTreeHelper.java    From docx4j-export-FO with Apache License 2.0 4 votes vote down vote up
/**
   * Since we start with headers/footers which each take up approx half the page,
   * there is little room for the body content (which would result in many pages,
   * and unnecessary processing).
   * 
   * At the same time, we need enough body content to produce first page, odd page,
   * and even page for each section.
   * 
   * So this method replaces the existing body content with content which is sufficient
   * for our needs.  This method isn't essential, but it should make things faster.
   * 
   * It leaves the headers/footers untouched, since it is those which we're 
   * most interested in at this point.
   *  
   * @param hfPkg
   */
  static void trimContent(WordprocessingMLPackage hfPkg)   {
  	
  	// Find the sectPrs
  	SectPrFinder sf = new SectPrFinder(hfPkg.getMainDocumentPart());
try {
	new TraversalUtil(hfPkg.getMainDocumentPart().getContents(), sf);
} catch (Docx4JException e) {
	// TODO Auto-generated catch block
	log.error(e.getMessage(), e);
}  

List<SectPr> sectPrList = sf.getSectPrList();

// Was there a body level one?
if (hfPkg.getMainDocumentPart().getJaxbElement().getBody().getSectPr()!=null) {
	//then delete the first entry (which is where SectPrFinder put it)
	sectPrList.remove(0);  
}

// Now generate content; let's use
P filler = createFillerP();
List<Object> contents = hfPkg.getMainDocumentPart().getContent();
contents.clear();

for (SectPr sectPr : sectPrList) {
	
	contents.add(filler);
	contents.add(filler);
	contents.add(filler);
	contents.add(filler);
	
	// We expect to cause, in due course, something like:
	// WARN org.apache.fop.apps.FOUserAgent .processEvent line 97 - 
	//          The contents of fo:region-body on page 6 exceed its viewport 
	//          by 29068 millipoints. (See position 1:1038)


	// now add the sectPr
   	P p = Context.getWmlObjectFactory().createP(); 
  	    PPr ppr = Context.getWmlObjectFactory().createPPr(); 
  	    p.setPPr(ppr);
  	    ppr.setSectPr(sectPr);
  	    
	contents.add(p);
	
}

// Add content before the body level sectPr
if (hfPkg.getMainDocumentPart().getJaxbElement().getBody().getSectPr()!=null) {

	contents.add(filler);
	contents.add(filler);
	contents.add(filler);
	contents.add(filler);
	
}
  }