Java Code Examples for org.docx4j.wml.Text#setSpace()

The following examples show how to use org.docx4j.wml.Text#setSpace() . 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_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 添加图片到段落
 */
public void addImageToPara(WordprocessingMLPackage wordMLPackage,
        ObjectFactory factory, P paragraph, String filePath,
        String content, RPr rpr, String altText, int id1, int id2)
        throws Exception {
    R run = factory.createR();
    if (content != null) {
        Text text = factory.createText();
        text.setValue(content);
        text.setSpace("preserve");
        run.setRPr(rpr);
        run.getContent().add(text);
    }

    InputStream is = new FileInputStream(filePath);
    byte[] bytes = IOUtils.toByteArray(is);
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
            .createImagePart(wordMLPackage, bytes);
    Inline inline = imagePart.createImageInline(filePath, altText, id1,
            id2, false);
    Drawing drawing = factory.createDrawing();
    drawing.getAnchorOrInline().add(inline);
    run.getContent().add(drawing);
    paragraph.getContent().add(run);
}
 
Example 2
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 3
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 添加图片到段落
 */
public static void addImageToPara(WordprocessingMLPackage wordMLPackage, ObjectFactory factory, P paragraph,
        String filePath, String content, RPr rpr, String altText, int id1, int id2) throws Exception {
    R run = factory.createR();
    if (content != null) {
        Text text = factory.createText();
        text.setValue(content);
        text.setSpace("preserve");
        run.setRPr(rpr);
        run.getContent().add(text);
    }

    InputStream is = new FileInputStream(filePath);
    byte[] bytes = IOUtils.toByteArray(is);
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
    Inline inline = imagePart.createImageInline(filePath, altText, id1, id2, false);
    Drawing drawing = factory.createDrawing();
    drawing.getAnchorOrInline().add(inline);
    run.getContent().add(drawing);
    paragraph.getContent().add(run);
}
 
Example 4
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addTotalPageNumberField(ObjectFactory factory, P paragraph) {  
    R run = factory.createR();  
    Text txt = new Text();  
    txt.setSpace("preserve");  
    txt.setValue("NUMPAGES  \\* MERGEFORMAT ");  
    run.getContent().add(factory.createRInstrText(txt));  
    paragraph.getContent().add(run);  
}
 
Example 5
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
private void addPageTextField(ObjectFactory factory, P paragraph,
		String value) {
	R run = factory.createR();
	Text txt = new Text();
	txt.setSpace("preserve");
	txt.setValue(value);
	run.getContent().add(txt);
	paragraph.getContent().add(run);
}
 
Example 6
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addTotalPageNumberField(ObjectFactory factory, P paragraph) {
	R run = factory.createR();
	Text txt = new Text();
	txt.setSpace("preserve");
	txt.setValue("NUMPAGES  \\* MERGEFORMAT ");
	run.getContent().add(factory.createRInstrText(txt));
	paragraph.getContent().add(run);
}
 
Example 7
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageNumberField(ObjectFactory factory, P paragraph) {
	R run = factory.createR();
	Text txt = new Text();
	txt.setSpace("preserve");
	txt.setValue("PAGE  \\* MERGEFORMAT ");
	run.getContent().add(factory.createRInstrText(txt));
	paragraph.getContent().add(run);
}
 
Example 8
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public P createImageParagraph(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory,P p,String fileName, String content,byte[] bytes,  
        JcEnumeration jcEnumeration) throws Exception {  
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage  
            .createImagePart(wordMLPackage, bytes);  
    Inline inline = imagePart.createImageInline(fileName, "这是图片", 1,  
            2, false);  
    Text text = factory.createText();  
    text.setValue(content);  
    text.setSpace("preserve");  
    R run = factory.createR();  
    p.getContent().add(run);  
    run.getContent().add(text);  
    Drawing drawing = factory.createDrawing();  
    run.getContent().add(drawing);  
    drawing.getAnchorOrInline().add(inline);  
    PPr pPr = p.getPPr();  
    if (pPr == null) {  
        pPr = factory.createPPr();  
    }  
    Jc jc = pPr.getJc();  
    if (jc == null) {  
        jc = new Jc();  
    }  
    jc.setVal(jcEnumeration);  
    pPr.setJc(jc);  
    p.setPPr(pPr);  
    setParagraphSpacing(factory, p, jcEnumeration, "0", "0");  
    return p;  
}
 
Example 9
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
private void addPageTextField(ObjectFactory factory, P paragraph,  
        String value) {  
    R run = factory.createR();  
    Text txt = new Text();  
    txt.setSpace("preserve");  
    txt.setValue(value);  
    run.getContent().add(txt);  
    paragraph.getContent().add(run);  
}
 
Example 10
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageNumberField(ObjectFactory factory, P paragraph) {  
    R run = factory.createR();  
    Text txt = new Text();  
    txt.setSpace("preserve");  
    txt.setValue("PAGE  \\* MERGEFORMAT ");  
    run.getContent().add(factory.createRInstrText(txt));  
    paragraph.getContent().add(run);  
}
 
Example 11
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public P createImageParagraph(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, P p, String fileName, String content,
		byte[] bytes, JcEnumeration jcEnumeration) throws Exception {
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
			.createImagePart(wordMLPackage, bytes);
	Inline inline = imagePart.createImageInline(fileName, "这是图片", 1, 2,
			false);
	Text text = factory.createText();
	text.setValue(content);
	text.setSpace("preserve");
	R run = factory.createR();
	p.getContent().add(run);
	run.getContent().add(text);
	Drawing drawing = factory.createDrawing();
	run.getContent().add(drawing);
	drawing.getAnchorOrInline().add(inline);
	PPr pPr = p.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	p.setPPr(pPr);
	setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
			null, true, "240", STLineSpacingRule.AUTO);
	return p;
}
 
Example 12
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
private void addPageTextField(ObjectFactory factory, P paragraph,
		String value) {
	R run = factory.createR();
	Text txt = new Text();
	txt.setSpace("preserve");
	txt.setValue(value);
	run.getContent().add(txt);
	paragraph.getContent().add(run);
}
 
Example 13
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addTotalPageNumberField(ObjectFactory factory, P paragraph) {
	R run = factory.createR();
	Text txt = new Text();
	txt.setSpace("preserve");
	txt.setValue("NUMPAGES  \\* MERGEFORMAT ");
	run.getContent().add(factory.createRInstrText(txt));
	paragraph.getContent().add(run);
}
 
Example 14
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addPageNumberField(ObjectFactory factory, P paragraph) {
	R run = factory.createR();
	Text txt = new Text();
	txt.setSpace("preserve");
	txt.setValue("PAGE  \\* MERGEFORMAT ");
	run.getContent().add(factory.createRInstrText(txt));
	paragraph.getContent().add(run);
}
 
Example 15
Source File: Docx4j_Helper.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/** 
 * 设置段落内容 
 */  
private static void setParagraphContent(P p, RPr rpr,String content) {  
    Text t = Docx4j_Helper.factory.createText();  
    t.setSpace("preserve");  
    t.setValue(content);  
    R run = Docx4j_Helper.factory.createR();  
    run.setRPr(rpr);  
    run.getContent().add(t);  
    p.getContent().add(run);  
}
 
Example 16
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
private void addComplexField(P p, String instrText, String instrText2) {

	    org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();

	    // Create object for r
	    R r = wmlObjectFactory.createR(); 
	    p.getContent().add( r); 
        // Create object for fldChar (wrapped in JAXBElement) 
        FldChar fldchar = wmlObjectFactory.createFldChar(); 
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar); 
        r.getContent().add( fldcharWrapped); 
            fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
        // Create object for instrText (wrapped in JAXBElement) 
        Text text = wmlObjectFactory.createText(); 
        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRInstrText(text); 
        r.getContent().add( textWrapped); 
        text.setValue( instrText); 
        text.setSpace( "preserve");    
        
	    R r2 = wmlObjectFactory.createR(); 
	    p.getContent().add( r2); 
	    
        if (!instrText2.equals("")){
    	    
        	r2.getContent().add(wmlObjectFactory.createRTab());
        	
            text = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRInstrText(text); 
            r2.getContent().add( textWrapped2); 
            text.setValue( instrText2); 
            text.setSpace( "preserve");
        	
        }

        
//        R r = factory.createR();
//        r.setRsidRPr("TOC entry text");
//        R.Tab tab = new R.Tab();
//        r.getRunContent().add(tab);             
//        p.getParagraphContent().add(r);
        
//		org.docx4j.wml.Text idx = wmlObjectFactory.createText();
//		JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRInstrText(idx); 
//        r.getContent().add( textWrapped2); 
//        idx.setValue( idxText); 
//        idx.setSpace( "preserve"); 

        // Create object for fldChar (wrapped in JAXBElement) 
        fldchar = wmlObjectFactory.createFldChar(); 
        fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar); 
        r2.getContent().add( fldcharWrapped); 
            fldchar.setFldCharType(org.docx4j.wml.STFldCharType.END);

	}
 
Example 17
Source File: AddingTableOfContent.java    From docx4j-template with Apache License 2.0 3 votes vote down vote up
/**
 * (不知道该怎么翻译, 因此将英文原注释保留)
 *  Adds the field that Word uses to create a table of content to the paragraph.
 *
 *  First we create a run and a text. Then we indicate that all spaces in the
 *  text are to be preserved and set the value to that of the TOC field.
 *  This field definition takes some arguments. The exact definition can be
 *  found in §17.16.5.58 of the Office Open XML standard. In this case we
 *  specify that we want to include all paragrapsh formatted with headings of
 *  levels 1-3 (\0 “1-3”). We also specify that we want all entries to be
 *  hyperlinks (\h), that we want to hide tab leader and page numbers in Web
 *  layout view (\z), and that we want to use the applied paragraph outline
 *  level (\\u).
 *  Finally we take the text and use it to create a JAXB element containing text
 *  and add this to the run, which we then add to the given paragraph.
 *  
 *  将Word用于创建目录表的域添加到段落中.
 *  
 *  首先创建一个可运行块和一个文本. 然后指出文本中所有的空格都被保护并给TOC域设置值. 这个域定义
 *  需要一些参数, 确切定义可以在Office Open XML标准的§17.16.5.58找到, 这种情况我们指定所有
 *  段落使用1-3级别的标题来格式化(\0 "1-3"). 我们同时指定所有的实体作为超链接(\h), 而且在Web
 *  视图中隐藏标签和页码(\z), 我们要使用段落大纲级别应用(\\u).
 *  最后使用文本对象创建了一个JAXB元素包含文本并添加到随后被添加到段落中的可运行块中. 
 *  
 *  @param paragraph
 */
private static void addTableOfContentField(P paragraph) {
    R run = factory.createR();
    Text txt = new Text();
    txt.setSpace("preserve");
    txt.setValue("TOC \\o \"1-3\" \\h \\z \\u");
    run.getContent().add(factory.createRInstrText(txt));
    paragraph.getContent().add(run);
}
 
Example 18
Source File: AddingPageNrToFooter.java    From docx4j-template with Apache License 2.0 3 votes vote down vote up
/**
 *  Creating the page number field is nearly the same as creating the field in
 *  the TOC example. The only difference is in the value. We use the PAGE
 *  command, which prints the number of the current page, together with the
 *  MERGEFORMAT switch, which indicates that the current formatting should be
 *  preserved when the field is updated.
 *
 * @param paragraph
 */
private static void addPageNumberField(P paragraph) {
    R run = factory.createR();
    Text txt = new Text();
    txt.setSpace("preserve");
    txt.setValue(" PAGE   \\* MERGEFORMAT ");
    run.getContent().add(factory.createRInstrText(txt));
    paragraph.getContent().add(run);
}