Java Code Examples for org.docx4j.wml.ObjectFactory#createDrawing()

The following examples show how to use org.docx4j.wml.ObjectFactory#createDrawing() . 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: 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 2
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 3
Source File: Docx4j_创建表格_S5_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory, byte[] bytes, String filenameHint,  
        String altText, int id1, int id2, long cx) throws Exception {  
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage  
            .createImagePart(wordMLPackage, bytes);  
    Inline inline = imagePart.createImageInline(filenameHint, altText, id1,  
            id2, cx, false);  
    // Now add the inline in w:p/w:r/w:drawing  
    P p = factory.createP();  
    R run = factory.createR();  
    p.getContent().add(run);  
    Drawing drawing = factory.createDrawing();  
    run.getContent().add(drawing);  
    drawing.getAnchorOrInline().add(inline);  
    return p;  
}
 
Example 4
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 5
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 6
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,  
        ObjectFactory factory, Part sourcePart, byte[] bytes,  
        String filenameHint, String altText, int id1, int id2,  
        JcEnumeration jcEnumeration) throws Exception {  
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage  
            .createImagePart(wordMLPackage, sourcePart, bytes);  
    Inline inline = imagePart.createImageInline(filenameHint, altText, id1,  
            id2, false);  
    P p = factory.createP();  
    R run = factory.createR();  
    p.getContent().add(run);  
    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);  
      
    PBdr pBdr=pPr.getPBdr();  
    if(pBdr==null){  
        pBdr=factory.createPPrBasePBdr();  
    }  
    CTBorder value=new CTBorder();  
    value.setVal(STBorder.SINGLE);  
    value.setColor("000000");  
    value.setSpace(new BigInteger("0"));  
    value.setSz(new BigInteger("3"));  
    pBdr.setBetween(value);  
    pPr.setPBdr(pBdr);  
    setParagraphSpacing(factory, p, jcEnumeration, "0", "0");  
    return p;  
}
 
Example 7
Source File: AddingAnInlineImage.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 *  创建一个对象工厂并用它创建一个段落和一个可运行块R.
 *  然后将可运行块添加到段落中. 接下来创建一个图画并将其添加到可运行块R中. 最后我们将内联
 *  对象添加到图画中并返回段落对象.
 *
 * @param   inline 包含图片的内联对象.
 * @return  包含图片的段落
 */
private static P addInlineImageToParagraph(Inline inline) {
    // 添加内联对象到一个段落中
    ObjectFactory factory = new ObjectFactory();
    P paragraph = factory.createP();
    R run = factory.createR();
    paragraph.getContent().add(run);
    Drawing drawing = factory.createDrawing();
    run.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    return paragraph;
}
 
Example 8
Source File: AddingAnInlineImageToTable.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 *  向新的段落中添加内联图片并返回这个段落.
 *  这个方法与前面例子中的方法没有区别.
 * @param inline
 * @return
 */
private static P addInlineImageToParagraph(Inline inline) {
    // Now add the in-line image to a paragraph
    ObjectFactory factory = new ObjectFactory();
    P paragraph = factory.createP();
    R run = factory.createR();
    paragraph.getContent().add(run);
    Drawing drawing = factory.createDrawing();
    run.getContent().add(drawing);
    drawing.getAnchorOrInline().add(inline);
    return paragraph;
}
 
Example 9
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, Part sourcePart, byte[] bytes,
		String filenameHint, String altText, int id1, int id2,
		JcEnumeration jcEnumeration) throws Exception {
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
			.createImagePart(wordMLPackage, sourcePart, bytes);
	Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
			id2, false);
	P p = factory.createP();
	R run = factory.createR();
	p.getContent().add(run);
	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);
	return p;
}
 
Example 10
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public P newImage(WordprocessingMLPackage wordMLPackage,
		ObjectFactory factory, Part sourcePart, byte[] bytes,
		String filenameHint, String altText, int id1, int id2,
		boolean isUnderLine, String underLineSize,
		JcEnumeration jcEnumeration) throws Exception {
	BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
			.createImagePart(wordMLPackage, sourcePart, bytes);
	Inline inline = imagePart.createImageInline(filenameHint, altText, id1,
			id2, false);
	P p = factory.createP();
	R run = factory.createR();
	p.getContent().add(run);
	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);

	if (isUnderLine) {
		PBdr pBdr = pPr.getPBdr();
		if (pBdr == null) {
			pBdr = factory.createPPrBasePBdr();
		}
		CTBorder value = new CTBorder();
		value.setVal(STBorder.SINGLE);
		value.setColor("000000");
		value.setSpace(new BigInteger("0"));
		value.setSz(new BigInteger(underLineSize));
		pBdr.setBetween(value);
		pPr.setPBdr(pBdr);
	}
	setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
			null, true, "240", STLineSpacingRule.AUTO);
	return p;
}