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;
}
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);
}
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);
}
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);
}
Aggregations