org.docx4j.wml.Br Java Examples

The following examples show how to use org.docx4j.wml.Br. 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_替换模板.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/** 
 * 设置单元格内容 
 *  
 * @param tc 
 * @param content 
 */  
public static void setNewTcContent(Tc tc, String content) {  
    P p = factory.createP();  
    tc.getContent().add(p);  
    R run = factory.createR();  
    p.getContent().add(run);  
    if (content != null) {  
        String[] contentArr = content.split("\n");  
        Text text = factory.createText();  
        text.setSpace("preserve");  
        text.setValue(contentArr[0]);  
        run.getContent().add(text);  
  
        for (int i = 1, len = contentArr.length; i < len; i++) {  
            Br br = factory.createBr();  
            run.getContent().add(br);// 换行  
            text = factory.createText();  
            text.setSpace("preserve");  
            text.setValue(contentArr[i]);  
            run.getContent().add(text);  
        }  
    }  
}
 
Example #2
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, STBrType sTBrType) {
	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
	Br breakObj = new Br();
	breakObj.setType(sTBrType);
	P paragraph = factory.createP();
	paragraph.getContent().add(breakObj);
	documentPart.addObject(paragraph);
}
 
Example #3
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageBreak(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory) {  
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();  
    Br breakObj = new Br();  
    breakObj.setType(STBrType.PAGE);  
    P paragraph = factory.createP();  
    paragraph.getContent().add(breakObj);  
    documentPart.addObject(paragraph);  
}
 
Example #4
Source File: AddingAPageBreak.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * 向文档添加一个换行符
 */
private static void addPageBreak() {
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
 
    Br breakObj = new Br();
    breakObj.setType(STBrType.PAGE);
 
    P paragraph = factory.createP();
    paragraph.getContent().add(breakObj);
    documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
 
Example #5
Source File: AddingPageNrToFooter.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a page break to the document.
 *
 * @param documentPart
 */
private static void addPageBreak(MainDocumentPart documentPart) {
    Br breakObj = new Br();
    breakObj.setType(STBrType.PAGE);
    P paragraph = factory.createP();
    paragraph.getContent().add(breakObj);
    documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
 
Example #6
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageBreak(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory) {
	MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
	Br breakObj = new Br();
	breakObj.setType(STBrType.PAGE);
	P paragraph = factory.createP();
	paragraph.getContent().add(breakObj);
	documentPart.addObject(paragraph);
}
 
Example #7
Source File: PlaceholderReplacer.java    From docx-stamper with MIT License 5 votes vote down vote up
private void replaceLineBreaks(ParagraphWrapper paragraphWrapper) {
  Br lineBreak = Context.getWmlObjectFactory().createBr();
  R run = RunUtil.create(lineBreak);
  while (paragraphWrapper.getText().contains(this.lineBreakPlaceholder)) {
    replace(paragraphWrapper, this.lineBreakPlaceholder, run);
  }
}
 
Example #8
Source File: LineBreakReplacementTest.java    From docx-stamper with MIT License 5 votes vote down vote up
private void lineBreaksAreReplaced(WordprocessingMLPackage document) {
    P paragraph = (P) document.getMainDocumentPart().getContent().get(2);
    Assert.assertTrue(new ParagraphWrapper(paragraph).getText().contains("This paragraph should be"));
    Assert.assertTrue(new ParagraphWrapper(paragraph).getText().contains("split in"));
    Assert.assertTrue(new ParagraphWrapper(paragraph).getText().contains("three lines"));

    Assert.assertEquals(R.class, paragraph.getContent().get(1).getClass());
    Assert.assertEquals(Br.class, ((R)paragraph.getContent().get(1)).getContent().get(0).getClass());

    Assert.assertEquals(R.class, paragraph.getContent().get(3).getClass());
    Assert.assertEquals(Br.class, ((R)paragraph.getContent().get(3)).getContent().get(0).getClass());
}
 
Example #9
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @Description: 段落添加Br 页面Break(分页符)
 */
public static void addPageBreak(P para, STBrType sTBrType) {
    Br breakObj = new Br();
    breakObj.setType(sTBrType);
    para.getContent().add(breakObj);
}
 
Example #10
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @Description: 段落添加Br 页面Break(分页符)
 */
public void addPageBreak(P para, STBrType sTBrType) {
    Br breakObj = new Br();
    breakObj.setType(sTBrType);
    para.getContent().add(breakObj);
}
 
Example #11
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
private void exportTextRegion(TrpTextRegionType tr, boolean wordBased, P p, Tc cell, MainDocumentPart mdp) {
	
	if (p == null){
		p = factory.createP();
		mdp.addObject(p);
	}

	List<TextLineType> lines = tr.getTextLine();
	for (int i=0; i<lines.size(); ++i) {
		TrpTextLineType trpL = (TrpTextLineType) lines.get(i);
											
		try {
			if (wordBased && trpL.getWord().size()>0){
				getFormattedTextForLineElement(trpL.getWord(), p, mdp);
			}
			else {
				getFormattedTextForShapeElement(trpL, p, mdp);
				
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/* with ¶ the user can mark a new paragraph inside a text region
		 * unicode is \u00B6
		 */
		if (trpL.getCustomTagList().containsParagraphTag()){
			//then new paragraph should be used;
			//logger.debug("paragraph sign detected ");
			p = factory.createP();
			if (cell != null){
				cell.getContent().add(p);
			}
			else{
				mdp.addObject(p);
			}
			
		}
		/*add line break after each text line
		 * or omit this if explicitely wished to have dense lines
		 * No line break at end of paragraph
		 */
		else if (preserveLineBreaks && !(i+1==lines.size()) ){
			Br br = factory.createBr(); // this Br element is used break the current and go for next line
			p.getContent().add(br);
		}
	}
	
}
 
Example #12
Source File: BrWriter.java    From docx4j-export-FO with Apache License 2.0 4 votes vote down vote up
@Override
public Node toNode(AbstractWmlConversionContext context, Object unmarshalledNode, 
		Node modelContent, TransformState state, Document doc)
		throws TransformerException {
	
	Br modelData = (Br)unmarshalledNode;
	Element ret;
	
	if (modelData.getType()!=null 
			&& modelData.getType().equals(STBrType.PAGE)) {
	
		ret = doc.createElementNS(XSL_FO, "block");
		ret.setAttribute("break-before", "page");
	
	} else {
		
		/* w:br is converted to fo:block with @line-height="0pt", to get rid of
		 * unwanted vertical whitespace.  If effect, it results in a carriage return.
		 * 
		 * However, some users use contiguous w:br to create vertical spacing
		 * which works in Word.  But FOP only does a single carriage return
		 * for contiguous fo:block/@line-height="0pt"
		 * 
		 * So we need to work around this.  Possible approaches:
		 * 
		 * 1. pre-process to convert the w:br to a w:p.  This is difficult,
		 *    for example, numbering.
		 * 
		 * 2. set TransformState, so the attribute is not set for the 2nd w:br
		 * 
		 * 3. set some custom attribute on the br
		 * 
		 * 4. post-process the XSL FO, so in contiguous fo:block/@line-height="0pt"
		 *    the attributes on 2nd and subsequent are changed.
		 *    
		 *    eg in org/docx4j/convert/out/fo/renderers/AbstractFORenderer_POSTPROCESSING.xslt
		 *     
		 * It turns out the easiest approach is to catch the special case in the XSLT
		 * 
		 */
	
		ret = doc.createElementNS(XSL_FO, "block");  // yuck .. block/inline/block
		// see http://stackoverflow.com/a/3664468/1031689 answer
		// at http://stackoverflow.com/questions/3661483/inserting-a-line-break-in-a-pdf-generated-from-xsl-fo-using-xslvalue-of
		ret.setAttribute("linefeed-treatment", "preserve");
		ret.setAttribute("white-space-treatment", "preserve");
		//ret.setAttribute("space-after", "0in"); // doesn't help
		ret.setAttribute("line-height", "0pt"); // suits FOP 1.1
		ret.setTextContent("\n");
	}
	return ret;
}