Search in sources :

Example 11 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.

the class Docx4J_例子2 method newImage.

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;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) PBdr(org.docx4j.wml.PPrBase.PBdr) CTBorder(org.docx4j.wml.CTBorder) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) BigInteger(java.math.BigInteger) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 12 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.

the class Docx4J_简单例子 method newImage.

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;
}
Also used : P(org.docx4j.wml.P) Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) Jc(org.docx4j.wml.Jc) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 13 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.

the class Docx4J_简单例子2 method createImageParagraph.

// 段落中插入文字和图片
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;
}
Also used : Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) Text(org.docx4j.wml.Text) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 14 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.

the class Docx4jTest method newImage.

/**
 * 创建包含图片的内容
 *
 * @param word
 * @param sourcePart
 * @param imageFilePath
 * @return
 * @throws Exception
 */
public static P newImage(WordprocessingMLPackage word, Part sourcePart, String imageFilePath) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(word, sourcePart, new File(imageFilePath));
    // 随机数ID
    int id = (int) (Math.random() * 10000);
    // 这里的id不重复即可
    Inline inline = imagePart.createImageInline("image", "image", id, id * 2, false);
    Drawing drawing = factory.createDrawing();
    drawing.getAnchorOrInline().add(inline);
    R r = factory.createR();
    r.getContent().add(drawing);
    P p = factory.createP();
    p.getContent().add(r);
    return p;
}
Also used : Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File)

Example 15 with BinaryPartAbstractImage

use of org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage in project docx4j-template by vindell.

the class Docx4j_SaveDocxImg_S3_Test method saveDocxImg.

/**
 * @Description: 提取word图片
 */
public void saveDocxImg(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
        if (entry.getValue() instanceof BinaryPartAbstractImage) {
            BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
            // 图片minetype
            String imgContentType = binImg.getContentType();
            PartName pt = binImg.getPartName();
            String fileName = null;
            if (pt.getName().indexOf("word/media/") != -1) {
                fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
            }
            System.out.println(String.format("mimetype=%s,filePath=%s", imgContentType, pt.getName()));
            FileOutputStream fos = new FileOutputStream(savePath + fileName);
            ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
            fos.close();
        }
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) PartName(org.docx4j.openpackaging.parts.PartName) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) Part(org.docx4j.openpackaging.parts.Part) FileOutputStream(java.io.FileOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File)

Aggregations

BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)19 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)16 P (org.docx4j.wml.P)10 R (org.docx4j.wml.R)10 Drawing (org.docx4j.wml.Drawing)9 File (java.io.File)5 Jc (org.docx4j.wml.Jc)5 PPr (org.docx4j.wml.PPr)5 CTVerticalJc (org.docx4j.wml.CTVerticalJc)4 ObjectFactory (org.docx4j.wml.ObjectFactory)4 STVerticalJc (org.docx4j.wml.STVerticalJc)4 Text (org.docx4j.wml.Text)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)3 BigInteger (java.math.BigInteger)2 PartName (org.docx4j.openpackaging.parts.PartName)2 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)2 CTBorder (org.docx4j.wml.CTBorder)2 PBdr (org.docx4j.wml.PPrBase.PBdr)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1