org.docx4j.wml.SectPr Java Examples

The following examples show how to use org.docx4j.wml.SectPr. 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_简单例子2.java    From docx4j-template with Apache License 2.0 7 votes vote down vote up
public void createHeaderReference(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, Relationship relationship)  
        throws InvalidFormatException {  
    List<SectionWrapper> sections = wordprocessingMLPackage  
            .getDocumentModel().getSections();  
    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();  
    // There is always a section wrapper, but it might not contain a sectPr  
    if (sectPr == null) {  
        sectPr = factory.createSectPr();  
        t.addObject(sectPr);  
        sections.get(sections.size() - 1).setSectPr(sectPr);  
    }  
    HeaderReference headerReference = factory.createHeaderReference();  
    headerReference.setId(relationship.getId());  
    headerReference.setType(HdrFtrRef.DEFAULT);  
    sectPr.getEGHdrFtrReferences().add(headerReference);  
}
 
Example #2
Source File: WatermarkPicture.java    From kbase-doc with Apache License 2.0 6 votes vote down vote up
private SectPr createSectPr() throws Exception {
		
		String openXML = "<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
				
				// Word adds the background image in each of 3 header parts
	            + "<w:headerReference r:id=\"" + createHeaderPart("even").getId() + "\" w:type=\"even\"/>"
	            + "<w:headerReference r:id=\"" + createHeaderPart("default").getId() + "\" w:type=\"default\"/>"
	            + "<w:headerReference r:id=\"" + createHeaderPart("first").getId() + "\" w:type=\"first\"/>"
	            
	            // Word adds empty footer parts when you create a watermark, but its not necessary
	            
//	            + "<w:footerReference r:id=\"rId9\" w:type=\"even\"/>"
//	            + "<w:footerReference r:id=\"rId10\" w:type=\"default\"/>"
//	            + "<w:footerReference r:id=\"rId12\" w:type=\"first\"/>"
	            
	            + "<w:pgSz w:h=\"15840\" w:w=\"12240\"/>"
	            + "<w:pgMar w:bottom=\"1440\" w:footer=\"708\" w:gutter=\"0\" w:header=\"708\" w:left=\"1440\" w:right=\"1440\" w:top=\"1440\"/>"
	            + "<w:cols w:space=\"708\"/>"
	            + "<w:docGrid w:linePitch=\"360\"/>"
	        +"</w:sectPr>";
	 
		return (SectPr)XmlUtils.unmarshalString(openXML);
	
	}
 
Example #3
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createFooterReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, Relationship relationship)
		throws InvalidFormatException {
	List<SectionWrapper> sections = wordprocessingMLPackage
			.getDocumentModel().getSections();
	SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
	// There is always a section wrapper, but it might not contain a sectPr
	if (sectPr == null) {
		sectPr = factory.createSectPr();
		t.addObject(sectPr);
		sections.get(sections.size() - 1).setSectPr(sectPr);
	}
	FooterReference footerReference = factory.createFooterReference();
	footerReference.setId(relationship.getId());
	footerReference.setType(HdrFtrRef.DEFAULT);
	sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #4
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createHeaderReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, Relationship relationship)
		throws InvalidFormatException {
	List<SectionWrapper> sections = wordprocessingMLPackage
			.getDocumentModel().getSections();
	SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
	// There is always a section wrapper, but it might not contain a sectPr
	if (sectPr == null) {
		sectPr = factory.createSectPr();
		t.addObject(sectPr);
		sections.get(sections.size() - 1).setSectPr(sectPr);
	}
	HeaderReference headerReference = factory.createHeaderReference();
	headerReference.setId(relationship.getId());
	headerReference.setType(HdrFtrRef.DEFAULT);
	sectPr.getEGHdrFtrReferences().add(headerReference);
}
 
Example #5
Source File: AddingPageNrToFooter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * This method fetches the document final section properties, and adds a newly
 * created footer reference to them.
 *
 * @param relationship
 */
public static void createFooterReference(Relationship relationship){
 
    List<SectionWrapper> sections =
        wordMLPackage.getDocumentModel().getSections();
 
    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectPr==null ) {
        sectPr = factory.createSectPr();
        wordMLPackage.getMainDocumentPart().addObject(sectPr);
        sections.get(sections.size() - 1).setSectPr(sectPr);
    }
 
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #6
Source File: Docx4j_创建表格_S5_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void setPageMargins(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory) {  
    try {  
        Body body = wordMLPackage.getMainDocumentPart().getContents()  
                .getBody();  
        PageDimensions page = new PageDimensions();  
        PgMar pgMar = page.getPgMar();  
        pgMar.setBottom(BigInteger.valueOf(pixelsToDxa(50)));  
        pgMar.setTop(BigInteger.valueOf(pixelsToDxa(50)));  
        pgMar.setLeft(BigInteger.valueOf(pixelsToDxa(50)));  
        pgMar.setRight(BigInteger.valueOf(pixelsToDxa(50)));  
        SectPr sectPr = factory.createSectPr();  
        body.setSectPr(sectPr);  
        sectPr.setPgMar(pgMar);  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}
 
Example #7
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createFooterReference(  
        WordprocessingMLPackage wordprocessingMLPackage,  
        MainDocumentPart t, ObjectFactory factory, Relationship relationship)  
        throws InvalidFormatException {  
    List<SectionWrapper> sections = wordprocessingMLPackage  
            .getDocumentModel().getSections();  
    SectPr sectPr = sections.get(sections.size() - 1).getSectPr();  
    // There is always a section wrapper, but it might not contain a sectPr  
    if (sectPr == null) {  
        sectPr = factory.createSectPr();  
        t.addObject(sectPr);  
        sections.get(sections.size() - 1).setSectPr(sectPr);  
    }  
    FooterReference footerReference = factory.createFooterReference();  
    footerReference.setId(relationship.getId());  
    footerReference.setType(HdrFtrRef.DEFAULT);  
    sectPr.getEGHdrFtrReferences().add(footerReference);  
}
 
Example #8
Source File: AddingAFooter.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 *  First we retrieve the document sections from the package. As we want to add
 *  a footer, we get the last section and take the section properties from it.
 *  The section is always present, but it might not have properties, so we check
 *  if they exist to see if we should create them. If they need to be created,
 *  we do and add them to the main document part and the section.
 *  Then we create a reference to the footer, give it the id of the relationship,
 *  set the type to header/footer reference and add it to the collection of
 *  references to headers and footers in the section properties.
 *
 * @param relationship
 */
private static void createFooterReference(Relationship relationship) {
    List<SectionWrapper> sections = wordMLPackage.getDocumentModel().getSections();
 
    SectPr sectionProperties = sections.get(sections.size() - 1).getSectPr();
    // There is always a section wrapper, but it might not contain a sectPr
    if (sectionProperties==null ) {
        sectionProperties = factory.createSectPr();
        wordMLPackage.getMainDocumentPart().addObject(sectionProperties);
        sections.get(sections.size() - 1).setSectPr(sectionProperties);
    }
 
    FooterReference footerReference = factory.createFooterReference();
    footerReference.setId(relationship.getId());
    footerReference.setType(HdrFtrRef.DEFAULT);
    sectionProperties.getEGHdrFtrReferences().add(footerReference);
}
 
Example #9
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createFooterReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, Relationship relationship)
		throws InvalidFormatException {
	List<SectionWrapper> sections = wordprocessingMLPackage
			.getDocumentModel().getSections();
	SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
	// There is always a section wrapper, but it might not contain a sectPr
	if (sectPr == null) {
		sectPr = factory.createSectPr();
		t.addObject(sectPr);
		sections.get(sections.size() - 1).setSectPr(sectPr);
	}
	FooterReference footerReference = factory.createFooterReference();
	footerReference.setId(relationship.getId());
	footerReference.setType(HdrFtrRef.DEFAULT);
	sectPr.getEGHdrFtrReferences().add(footerReference);
}
 
Example #10
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void createHeaderReference(
		WordprocessingMLPackage wordprocessingMLPackage,
		MainDocumentPart t, ObjectFactory factory, Relationship relationship)
		throws InvalidFormatException {
	List<SectionWrapper> sections = wordprocessingMLPackage
			.getDocumentModel().getSections();
	SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
	// There is always a section wrapper, but it might not contain a sectPr
	if (sectPr == null) {
		sectPr = factory.createSectPr();
		t.addObject(sectPr);
		sections.get(sections.size() - 1).setSectPr(sectPr);
	}
	HeaderReference headerReference = factory.createHeaderReference();
	headerReference.setId(relationship.getId());
	headerReference.setType(HdrFtrRef.DEFAULT);
	sectPr.getEGHdrFtrReferences().add(headerReference);
}
 
Example #11
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 设置行号
 * @param distance
 *            :距正文距离 1厘米=567
 * @param start
 *            :起始编号(0开始)
 * @param countBy
 *            :行号间隔
 * @param restartType
 *            :STLineNumberRestart.CONTINUOUS(continuous连续编号)<br/>
 *            STLineNumberRestart.NEW_PAGE(每页重新编号)<br/>
 *            STLineNumberRestart.NEW_SECTION(每节重新编号)
 */
public void setDocInNumType(WordprocessingMLPackage wordPackage,
        String countBy, String distance, String start,
        STLineNumberRestart restartType) {
    SectPr sectPr = getDocSectPr(wordPackage);
    CTLineNumber lnNumType = sectPr.getLnNumType();
    if (lnNumType == null) {
        lnNumType = new CTLineNumber();
        sectPr.setLnNumType(lnNumType);
    }
    if (StringUtils.isNotBlank(countBy)) {
        lnNumType.setCountBy(new BigInteger(countBy));
    }
    if (StringUtils.isNotBlank(distance)) {
        lnNumType.setDistance(new BigInteger(distance));
    }
    if (StringUtils.isNotBlank(start)) {
        lnNumType.setStart(new BigInteger(start));
    }
    if (restartType != null) {
        lnNumType.setRestart(restartType);
    }
}
 
Example #12
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description:设置页边距
 */
public void setDocMarginSpace(WordprocessingMLPackage wordPackage,
        ObjectFactory factory, String top, String left, String bottom,
        String right) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgMar pg = sectPr.getPgMar();
    if (pg == null) {
        pg = factory.createSectPrPgMar();
        sectPr.setPgMar(pg);
    }
    if (StringUtils.isNotBlank(top)) {
        pg.setTop(new BigInteger(top));
    }
    if (StringUtils.isNotBlank(bottom)) {
        pg.setBottom(new BigInteger(bottom));
    }
    if (StringUtils.isNotBlank(left)) {
        pg.setLeft(new BigInteger(left));
    }
    if (StringUtils.isNotBlank(right)) {
        pg.setRight(new BigInteger(right));
    }
}
 
Example #13
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 设置页面大小及纸张方向 landscape横向
 */
public void setDocumentSize(WordprocessingMLPackage wordPackage,
        ObjectFactory factory, String width, String height,
        STPageOrientation stValue) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgSz pgSz = sectPr.getPgSz();
    if (pgSz == null) {
        pgSz = factory.createSectPrPgSz();
        sectPr.setPgSz(pgSz);
    }
    if (StringUtils.isNotBlank(width)) {
        pgSz.setW(new BigInteger(width));
    }
    if (StringUtils.isNotBlank(height)) {
        pgSz.setH(new BigInteger(height));
    }
    if (stValue != null) {
        pgSz.setOrient(stValue);
    }
}
 
Example #14
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 设置页面边框
 */
public void setDocumentBorders(WordprocessingMLPackage wordPackage,
        ObjectFactory factory, CTBorder top, CTBorder right,
        CTBorder bottom, CTBorder left) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgBorders pgBorders = sectPr.getPgBorders();
    if (pgBorders == null) {
        pgBorders = factory.createSectPrPgBorders();
        sectPr.setPgBorders(pgBorders);
    }
    if (top != null) {
        pgBorders.setTop(top);
    }
    if (right != null) {
        pgBorders.setRight(right);
    }
    if (bottom != null) {
        pgBorders.setBottom(bottom);
    }
    if (left != null) {
        pgBorders.setLeft(left);
    }
}
 
Example #15
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 设置行号
 * @param distance
 *            :距正文距离 1厘米=567
 * @param start
 *            :起始编号(0开始)
 * @param countBy
 *            :行号间隔
 * @param restartType
 *            :STLineNumberRestart.CONTINUOUS(continuous连续编号)<br/>
 *            STLineNumberRestart.NEW_PAGE(每页重新编号)<br/>
 *            STLineNumberRestart.NEW_SECTION(每节重新编号)
 */
public static void setDocInNumType(WordprocessingMLPackage wordPackage, String countBy, String distance, String start,
        STLineNumberRestart restartType) {
    SectPr sectPr = getDocSectPr(wordPackage);
    CTLineNumber lnNumType = sectPr.getLnNumType();
    if (lnNumType == null) {
        lnNumType = new CTLineNumber();
        sectPr.setLnNumType(lnNumType);
    }
    if (StringUtils.isNotBlank(countBy)) {
        lnNumType.setCountBy(new BigInteger(countBy));
    }
    if (StringUtils.isNotBlank(distance)) {
        lnNumType.setDistance(new BigInteger(distance));
    }
    if (StringUtils.isNotBlank(start)) {
        lnNumType.setStart(new BigInteger(start));
    }
    if (restartType != null) {
        lnNumType.setRestart(restartType);
    }
}
 
Example #16
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description:设置页边距
 */
public static void setDocMarginSpace(WordprocessingMLPackage wordPackage, ObjectFactory factory, String top, String left,
        String bottom, String right) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgMar pg = sectPr.getPgMar();
    if (pg == null) {
        pg = factory.createSectPrPgMar();
        sectPr.setPgMar(pg);
    }
    if (StringUtils.isNotBlank(top)) {
        pg.setTop(new BigInteger(top));
    }
    if (StringUtils.isNotBlank(bottom)) {
        pg.setBottom(new BigInteger(bottom));
    }
    if (StringUtils.isNotBlank(left)) {
        pg.setLeft(new BigInteger(left));
    }
    if (StringUtils.isNotBlank(right)) {
        pg.setRight(new BigInteger(right));
    }
}
 
Example #17
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 设置页面大小及纸张方向 landscape横向
 */
public static void setDocumentSize(WordprocessingMLPackage wordPackage, ObjectFactory factory, String width, String height,
        STPageOrientation stValue) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgSz pgSz = sectPr.getPgSz();
    if (pgSz == null) {
        pgSz = factory.createSectPrPgSz();
        sectPr.setPgSz(pgSz);
    }
    if (StringUtils.isNotBlank(width)) {
        pgSz.setW(new BigInteger(width));
    }
    if (StringUtils.isNotBlank(height)) {
        pgSz.setH(new BigInteger(height));
    }
    if (stValue != null) {
        pgSz.setOrient(stValue);
    }
}
 
Example #18
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 设置页面边框
 */
public static void setDocumentBorders(WordprocessingMLPackage wordPackage, ObjectFactory factory, CTBorder top,
        CTBorder right, CTBorder bottom, CTBorder left) {
    SectPr sectPr = getDocSectPr(wordPackage);
    PgBorders pgBorders = sectPr.getPgBorders();
    if (pgBorders == null) {
        pgBorders = factory.createSectPrPgBorders();
        sectPr.setPgBorders(pgBorders);
    }
    if (top != null) {
        pgBorders.setTop(top);
    }
    if (right != null) {
        pgBorders.setRight(right);
    }
    if (bottom != null) {
        pgBorders.setBottom(bottom);
    }
    if (left != null) {
        pgBorders.setLeft(left);
    }
}
 
Example #19
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 设置分节符 nextPage:下一页 continuous:连续 evenPage:偶数页 oddPage:奇数页
 */
public void setDocSectionBreak(WordprocessingMLPackage wordPackage,
        String sectValType) {
    if (StringUtils.isNotBlank(sectValType)) {
        SectPr sectPr = getDocSectPr(wordPackage);
        Type sectType = sectPr.getType();
        if (sectType == null) {
            sectType = new Type();
            sectPr.setType(sectType);
        }
        sectType.setVal(sectValType);
    }
}
 
Example #20
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description:设置文字方向 tbRl 垂直
 */
public void setDocTextDirection(WordprocessingMLPackage wordPackage,
        String textDirection) {
    if (StringUtils.isNotBlank(textDirection)) {
        SectPr sectPr = getDocSectPr(wordPackage);
        TextDirection textDir = sectPr.getTextDirection();
        if (textDir == null) {
            textDir = new TextDirection();
            sectPr.setTextDirection(textDir);
        }
        textDir.setVal(textDirection);
    }
}
 
Example #21
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description:设置word 垂直对齐方式(Word默认方式都是"顶端对齐")
 */
public void setDocVAlign(WordprocessingMLPackage wordPackage,
        STVerticalJc valignType) {
    if (valignType != null) {
        SectPr sectPr = getDocSectPr(wordPackage);
        CTVerticalJc valign = sectPr.getVAlign();
        if (valign == null) {
            valign = new CTVerticalJc();
            sectPr.setVAlign(valign);
        }
        valign.setVal(valignType);
    }
}
 
Example #22
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description:设置word 垂直对齐方式(Word默认方式都是"顶端对齐")
 */
public static void setDocVAlign(WordprocessingMLPackage wordPackage, STVerticalJc valignType) {
    if (valignType != null) {
        SectPr sectPr = getDocSectPr(wordPackage);
        CTVerticalJc valign = sectPr.getVAlign();
        if (valign == null) {
            valign = new CTVerticalJc();
            sectPr.setVAlign(valign);
        }
        valign.setVal(valignType);
    }
}
 
Example #23
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description:设置文字方向 tbRl 垂直
 */
public static void setDocTextDirection(WordprocessingMLPackage wordPackage, String textDirection) {
    if (StringUtils.isNotBlank(textDirection)) {
        SectPr sectPr = getDocSectPr(wordPackage);
        TextDirection textDir = sectPr.getTextDirection();
        if (textDir == null) {
            textDir = new TextDirection();
            sectPr.setTextDirection(textDir);
        }
        textDir.setVal(textDirection);
    }
}
 
Example #24
Source File: WordProcessor.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
private SectPr createSectPr() throws JAXBException, Docx4JException, IOException {
	String openXML = "<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
			// Word adds the background image in each of 3 header parts
			+ "<w:headerReference r:id=\"" + createHeaderPart("even").getId() + "\" w:type=\"even\"/>"
			+ "<w:headerReference r:id=\"" + createHeaderPart("default").getId() + "\" w:type=\"default\"/>"
			+ "<w:headerReference r:id=\"" + createHeaderPart("first").getId() + "\" w:type=\"first\"/>"

            + "<w:pgSz w:h=\"15840\" w:w=\"12240\"/>"
            + "<w:pgMar w:bottom=\"1440\" w:footer=\"708\" w:gutter=\"0\" w:header=\"708\" w:left=\"1440\" w:right=\"1440\" w:top=\"1440\"/>"
            + "<w:cols w:space=\"708\"/>"
            + "<w:docGrid w:linePitch=\"360\"/>"
            +"</w:sectPr>";
	return (SectPr)XmlUtils.unmarshalString(openXML);
}
 
Example #25
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 设置分节符 nextPage:下一页 continuous:连续 evenPage:偶数页 oddPage:奇数页
 */
public static void setDocSectionBreak(WordprocessingMLPackage wordPackage, String sectValType) {
    if (StringUtils.isNotBlank(sectValType)) {
        SectPr sectPr = getDocSectPr(wordPackage);
        Type sectType = sectPr.getType();
        if (sectType == null) {
            sectType = new Type();
            sectPr.setType(sectType);
        }
        sectType.setVal(sectValType);
    }
}
 
Example #26
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);
	
}
  }
 
Example #27
Source File: XsltFOFunctions.java    From docx4j-export-FO with Apache License 2.0 4 votes vote down vote up
/**
 * FOP inserts a blank page if necessary so that a section with page numbering
 * from 1 would be face up when printed double sided. Word doesn't do that
 * (unless you have an odd section type), so this function mimics Word's 
 * behaviour. 
 * 
 * @param context
 * @return
 * @since 3.2.2
 */
public static String getForcePageCount(FOConversionContext context) {
	
	// see http://www.w3.org/TR/xsl/#force-page-count
	
	ConversionSectionWrapper wrapper = context.getSections().peekNextSection();
	
	if (wrapper==null) {
		// final section
		return "no-force";
	} else {
		SectPr.Type secType = wrapper.getSectPr().getType();
		
		CTPageNumber pgNumType = wrapper.getSectPr().getPgNumType();
		Boolean isExplicitOdd = null; // null means numbering will continue from the highest page number in the previous section
		if (pgNumType!=null && pgNumType.getStart()!=null) {
			int start = pgNumType.getStart().intValue();
			if ( start % 2 == 0) {
				isExplicitOdd = Boolean.FALSE;    				
			} else {
				isExplicitOdd = Boolean.TRUE;    				    				
			}
		}
		
		if (secType==null || secType.getVal().equals("nextPage") ) {
    		return "no-force";  
		} else if (isExplicitOdd==null  // LIMITATION: We don't get this right after the user has set the page number explicitly in a previous section
						|| isExplicitOdd) {
			// The normal case
			if ( secType.getVal().equals("evenPage") ) {
 			// Even page section breaks, which begin the new section on the next even-numbered page.
 			// (What happens if that section has w:pgNumType/@w:start="1"?)
 			return "end-on-odd";
 		} else if ( secType.getVal().equals("oddPage") ) {
 			// Odd page section breaks, which begin the new section on the next odd-numbered page
 			return "end-on-even";
 		} else {
 			// continuous (!)
     		return "no-force";    			    			
 		}
		} else {
			// section starts with p2 or p4
			if ( secType.getVal().equals("evenPage") ) {
 			// Even page section breaks, which begin the new section on the next even-numbered page.
 			// (What happens if that section has w:pgNumType/@w:start="1"?)
 			return "end-on-even";
 		} else if ( secType.getVal().equals("oddPage") ) {
 			// Odd page section breaks, which begin the new section on the next odd-numbered page
 			return "end-on-odd";
 		} else {
 			// continuous (!)
     		return "no-force";    			    			
 		}
			
		}
	}

}
 
Example #28
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public static SectPr getDocSectPr(WordprocessingMLPackage wordPackage) {
    SectPr sectPr = wordPackage.getDocumentModel().getSections().get(0).getSectPr();
    return sectPr;
}
 
Example #29
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 #30
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public SectPr getDocSectPr(WordprocessingMLPackage wordPackage) {
    SectPr sectPr = wordPackage.getDocumentModel().getSections().get(0)
            .getSectPr();
    return sectPr;
}