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

The following examples show how to use org.docx4j.wml.Text#setValue() . 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
public Comments.Comment createComment(ObjectFactory factory,  
        BigInteger commentId, String author, Date date,  
        String commentContent, RPr commentRPr) throws Exception {  
    Comments.Comment comment = factory.createCommentsComment();  
    comment.setId(commentId);  
    if (author != null) {  
        comment.setAuthor(author);  
    }  
    if (date != null) {  
        comment.setDate(toXMLCalendar(date));  
    }  
    P commentP = factory.createP();  
    comment.getEGBlockLevelElts().add(commentP);  
    R commentR = factory.createR();  
    commentP.getContent().add(commentR);  
    Text commentText = factory.createText();  
    commentR.getContent().add(commentText);  
    commentR.setRPr(commentRPr);  
    commentText.setValue(commentContent);  
    return comment;  
}
 
Example 2
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Hdr getTextHdr(WordprocessingMLPackage wordprocessingMLPackage,
		ObjectFactory factory, Part sourcePart, String content,
		JcEnumeration jcEnumeration) throws Exception {
	Hdr hdr = factory.createHdr();
	P headP = factory.createP();
	Text text = factory.createText();
	text.setValue(content);
	R run = factory.createR();
	run.getContent().add(text);
	headP.getContent().add(run);

	PPr pPr = headP.getPPr();
	if (pPr == null) {
		pPr = factory.createPPr();
	}
	Jc jc = pPr.getJc();
	if (jc == null) {
		jc = new Jc();
	}
	jc.setVal(jcEnumeration);
	pPr.setJc(jc);
	headP.setPPr(pPr);
	hdr.getContent().add(headP);
	return hdr;
}
 
Example 3
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Ftr getTextFtr(WordprocessingMLPackage wordprocessingMLPackage,  
        ObjectFactory factory, Part sourcePart, String content,  
        JcEnumeration jcEnumeration) throws Exception {  
    Ftr ftr = factory.createFtr();  
    P footerP = factory.createP();  
    Text text = factory.createText();  
    text.setValue(content);  
    R run = factory.createR();  
    run.getContent().add(text);  
    footerP.getContent().add(run);  
  
    PPr pPr = footerP.getPPr();  
    if (pPr == null) {  
        pPr = factory.createPPr();  
    }  
    Jc jc = pPr.getJc();  
    if (jc == null) {  
        jc = new Jc();  
    }  
    jc.setVal(jcEnumeration);  
    pPr.setJc(jc);  
    footerP.setPPr(pPr);  
    ftr.getContent().add(footerP);  
    return ftr;  
}
 
Example 4
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 5
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 6
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 7
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 8
Source File: Docx4j_创建批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void createCommentEnd(ObjectFactory factory, P p, String pContent,  
        String commentContent, RPr fontRPr, RPr commentRPr,  
        BigInteger commentId, Comments comments) throws Exception{  
    Text txt = factory.createText();  
    txt.setValue(pContent);  
    R run = factory.createR();  
    run.getContent().add(txt);  
    run.setRPr(fontRPr);  
    p.getContent().add(run);  
    Comment commentOne = createComment(factory, commentId, "系统管理员",new Date(), commentContent, commentRPr);  
    comments.getComment().add(commentOne);  
    p.getContent().add(createRunCommentReference(factory, commentId));  
}
 
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 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 11
Source File: TextBoxTest.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
private static P createContent(String textContent) {
	
	P p = Context.getWmlObjectFactory().createP(); 

    R r = Context.getWmlObjectFactory().createR(); 
    p.getContent().add( r); 
        // Create object for t (wrapped in JAXBElement) 
        Text text = Context.getWmlObjectFactory().createText(); 
        JAXBElement<org.docx4j.wml.Text> textWrapped = Context.getWmlObjectFactory().createRT(text); 
        r.getContent().add( textWrapped); 
            text.setValue( textContent); 
            
            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: WMLPackageUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static void replacePlaceholder(MainDocumentPart documentPart, String placeholder, String content ) { 
	// 获取文档对象中所有的Text类型对象
    List<Text> texts = WmlElementUtils.getTargetElements(documentPart, Text.class);  
   // 循环Text类型对象集合
    for (Text text : texts) {  
        Text textElement = (Text) text;  
        if (textElement.getValue().equals(placeholder)) {  
            textElement.setValue(content);  
        }  
    }  
}
 
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: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addTableCell(ObjectFactory factory,
		WordprocessingMLPackage wordMLPackage, Tr tableRow, String content,
		RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor,
		String backgroudColor) {
	Tc tableCell = factory.createTc();
	P p = factory.createP();
	setParagraphAlign(factory, p, jcEnumeration);
	Text t = factory.createText();
	t.setValue(content);
	R run = factory.createR();
	// 设置表格内容字体样式
	run.setRPr(rpr);
	run.getContent().add(t);
	p.getContent().add(run);
	tableCell.getContent().add(p);

	if (hasBgColor) {
		TcPr tcPr = tableCell.getTcPr();
		if (tcPr == null) {
			tcPr = factory.createTcPr();
		}
		CTShd shd = tcPr.getShd();
		if (shd == null) {
			shd = factory.createCTShd();
		}
		shd.setColor("auto");
		shd.setFill(backgroudColor);
		tcPr.setShd(shd);
		tableCell.setTcPr(tcPr);
	}
	tableRow.getContent().add(tableCell);
}
 
Example 17
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static Ftr createFooter(String content) {
    Ftr footer = factory.createFtr();
    P paragraph = factory.createP();
    R run = factory.createR();
    Text text = new Text();
    text.setValue(content);
    run.getContent().add(text);
    paragraph.getContent().add(run);
    footer.getContent().add(paragraph);
    return footer;
}
 
Example 18
Source File: AbstractInliner.java    From yarg with Apache License 2.0 5 votes vote down vote up
@Override
public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object paramValue, Matcher paramsMatcher) {
    try {
        Image image = new Image(paramValue, paramsMatcher);
        if (image.isValid()) {
            Part part = resolveTextPartForDOCX(text, wordPackage);
            BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, part, image.imageContent);
            int originalWidth = imagePart.getImageInfo().getSize().getWidthPx();
            int originalHeight = imagePart.getImageInfo().getSize().getHeightPx();

            double widthScale = (double) image.width / (double) originalWidth;
            double heightScale = (double) image.height  / (double) originalHeight;
            double actualScale = Math.min(widthScale, heightScale);

            long targetWidth = Math.round(originalWidth * actualScale);
            long targetHeight = Math.round(originalHeight * actualScale);

            Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++,
                    XlsxUtils.convertPxToEmu(targetWidth), XlsxUtils.convertPxToEmu(targetHeight), false);

            org.docx4j.wml.Drawing drawing = new org.docx4j.wml.ObjectFactory().createDrawing();
            R run = (R) text.getParent();
            run.getContent().add(drawing);
            drawing.getAnchorOrInline().add(inline);
            text.setValue("");
        }
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to docx file", e);
    }
}
 
Example 19
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
public P createIt() {

		org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

		P p = wmlObjectFactory.createP();
		    // Create object for pPr
		    PPr ppr = wmlObjectFactory.createPPr();
		    p.setPPr(ppr);
		        // Create object for rPr
		        ParaRPr pararpr = wmlObjectFactory.createParaRPr();
		        ppr.setRPr(pararpr);
		            // Create object for u
		            U u = wmlObjectFactory.createU();
		            pararpr.setU(u);
		                u.setVal(org.docx4j.wml.UnderlineEnumeration.SINGLE);
		            // Create object for lang
		            CTLanguage language = wmlObjectFactory.createCTLanguage();
		            pararpr.setLang(language);
		                language.setVal( "en-AU");
		        // Create object for jc
		        Jc jc = wmlObjectFactory.createJc();
		        ppr.setJc(jc);
		            jc.setVal(org.docx4j.wml.JcEnumeration.CENTER);
		    // Create object for r
		    R r = wmlObjectFactory.createR();
		    p.getContent().add( r);
		        // Create object for rPr
		        RPr rpr = wmlObjectFactory.createRPr();
		        r.setRPr(rpr);
		            // Create object for u
		            U u2 = wmlObjectFactory.createU();
		            rpr.setU(u2);
		                u2.setVal(org.docx4j.wml.UnderlineEnumeration.SINGLE);
		            // Create object for lang
		            CTLanguage language2 = wmlObjectFactory.createCTLanguage();
		            rpr.setLang(language2);
		                language2.setVal( "en-AU");
		        // Create object for t (wrapped in JAXBElement)
		        Text text = wmlObjectFactory.createText();
		        JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text);
		        r.getContent().add( textWrapped);
		            text.setValue( "Underlined and centred");
		            
		            

		return p;
		}
 
Example 20
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);
}