Search in sources :

Example 16 with BinaryPartAbstractImage

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

the class Docx4jStyle_S3 method newImage.

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

Example 17 with BinaryPartAbstractImage

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

the class WordprocessingMLPackageRender method addImageToPackage.

/**
 *  Docx4j拥有一个由字节数组创建图片部件的工具方法, 随后将其添加到给定的包中. 为了能将图片添加
 *  到一个段落中, 我们需要将图片转换成内联对象. 这也有一个方法, 方法需要文件名提示, 替换文本,
 *  两个id标识符和一个是嵌入还是链接到的指示作为参数.
 *  一个id用于文档中绘图对象不可见的属性, 另一个id用于图片本身不可见的绘制属性. 最后我们将内联
 *  对象添加到段落中并将段落添加到包的主文档部件.
 *
 *  @param wordMLPackage 要添加图片的包
 *  @param bytes         图片对应的字节数组
 *  @throws Exception    不幸的createImageInline方法抛出一个异常(没有更多具体的异常类型)
 */
public void addImageToPackage(byte[] bytes) throws Exception {
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wmlPackage, bytes);
    int docPrId = 1;
    int cNvPrId = 2;
    Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
    P paragraph = WmlElementUtils.addInlineImageToParagraph(inline);
    wmlPackage.getMainDocumentPart().addObject(paragraph);
}
Also used : P(org.docx4j.wml.P) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 18 with BinaryPartAbstractImage

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

the class AddingAnInlineImageToTable method createInlineImage.

/**
 * 使用给定的文件创建一个内联图片.
 * 跟前面例子中一样, 我们将文件转换成字节数组, 并用它创建一个内联图片.
 *
 * @param file
 * @return
 * @throws Exception
 */
private static Inline createInlineImage(File file) throws Exception {
    byte[] bytes = convertImageToByteArray(file);
    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
    int docPrId = 1;
    int cNvPrId = 2;
    return imagePart.createImageInline("Filename hint", "Alternative text", docPrId, cNvPrId, false);
}
Also used : BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)

Example 19 with BinaryPartAbstractImage

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

the class Docx4j_工具类_S3_Test method addImageToPara.

/**
 * @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);
}
Also used : Drawing(org.docx4j.wml.Drawing) R(org.docx4j.wml.R) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Text(org.docx4j.wml.Text) Inline(org.docx4j.dml.wordprocessingDrawing.Inline) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) FileInputStream(java.io.FileInputStream)

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