package cn.flightfeather.supervision.docx4j.demo;
|
|
import org.docx4j.dml.wordprocessingDrawing.Inline;
|
import org.docx4j.jaxb.Context;
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
|
import org.docx4j.wml.*;
|
|
import java.io.*;
|
import java.math.BigInteger;
|
|
public class AddImage001 {
|
|
private static WordprocessingMLPackage wordMLPackage;
|
private static ObjectFactory factory;
|
|
/**
|
* 首先我们创建包和对象工厂, 因此在类的随处我们都可以使用它们. 然后我们创建一个表格并添加
|
* 边框. 接下来我们创建一个表格行并在第一个域添加一些文本.
|
* 对于第二个域, 我们用与前面一样的图片创建一个段落并添加进去. 最后把行添加到表格中, 并将
|
* 表格添加到包中, 然后保存这个包.
|
*/
|
public static void main (String[] args) throws Exception {
|
wordMLPackage = WordprocessingMLPackage.createPackage();
|
factory = Context.getWmlObjectFactory();
|
|
Tbl table = factory.createTbl();
|
addBorders(table);
|
|
Tr tr = factory.createTr();
|
|
P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("Field 1");
|
addTableCell(tr, paragraphOfText);
|
|
File file = new File( "/Users/liwei/resource/supervision/src/main/resources/static/images/tutu2.png");
|
P paragraphWithImage = addInlineImageToParagraph(createInlineImage(file));
|
addTableCell(tr, paragraphWithImage);
|
|
table.getContent().add(tr);
|
|
wordMLPackage.getMainDocumentPart().addObject(table);
|
wordMLPackage.save(new java.io.File("HelloWord80.docx"));
|
}
|
|
/**
|
* 用给定的段落作为内容向给定的行中添加一个单元格.
|
*
|
* @param tr
|
* @param paragraph
|
*/
|
private static void addTableCell(Tr tr, P paragraph) {
|
Tc tc1 = factory.createTc();
|
tc1.getContent().add(paragraph);
|
tr.getContent().add(tc1);
|
}
|
|
/**
|
* 向新的段落中添加内联图片并返回这个段落.
|
* 这个方法与前面例子中的方法没有区别.
|
*
|
* @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;
|
}
|
|
/**
|
* 使用给定的文件创建一个内联图片.
|
* 跟前面例子中一样, 我们将文件转换成字节数组, 并用它创建一个内联图片.
|
*
|
* @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);
|
}
|
|
/**
|
* 将图片从文件转换成字节数组.
|
*
|
* @param file
|
* @return
|
* @throws FileNotFoundException
|
* @throws IOException
|
*/
|
private static byte[] convertImageToByteArray(File file) throws FileNotFoundException, IOException {
|
InputStream is = new FileInputStream(file );
|
long length = file.length();
|
// You cannot create an array using a long, it needs to be an int.
|
if (length > Integer.MAX_VALUE) {
|
System.out.println("File too large!!");
|
}
|
byte[] bytes = new byte[(int)length];
|
int offset = 0;
|
int numRead = 0;
|
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
|
offset += numRead;
|
}
|
// Ensure all the bytes have been read
|
if (offset < bytes.length) {
|
System.out.println("Could not completely read file "+file.getName());
|
}
|
is.close();
|
return bytes;
|
}
|
|
/**
|
* 给表格添加简单的黑色边框.
|
*
|
* @param table
|
*/
|
private static void addBorders(Tbl table) {
|
table.setTblPr(new TblPr());
|
CTBorder border = new CTBorder();
|
border.setColor("auto");
|
border.setSz(new BigInteger("4"));
|
border.setSpace(new BigInteger("0"));
|
border.setVal(STBorder.SINGLE);
|
|
TblBorders borders = new TblBorders();
|
borders.setBottom(border);
|
borders.setLeft(border);
|
borders.setRight(border);
|
borders.setTop(border);
|
borders.setInsideH(border);
|
borders.setInsideV(border);
|
table.getTblPr().setTblBorders(borders);
|
}
|
}
|