废话不多说,直接上代码
@Test
public void testWordFX() throws IOException, InvalidFormatException, Exception{
//以下代码不一定要用,可以通过其他方法打开自己项目里面的模板 from this
InputStream inputStream = ClassPathResource.class.getResourceAsStream("/file/任免表.docx");
InputStream inputConf = ClassPathResource.class.getResourceAsStream("/file/任免表.json");
JSONObject conf = JSON.parseObject(IOUtils.toString(inputConf, "utf-8"));
AppointDismissBean result = new AppointDismissBean();
List<LeaderFamilyBean> listContent = result.getLeaderFamilyList();
List<Object> list = new ArrayList<Object>();
List<List<Object>> loops = new ArrayList<List<Object>>();
loops.add(list);
Word07Template word07Template = new Word07Template();
XWPFDocument doc = word07Template.generateDoc(inputStream, conf, loops, result);
//end this
//自动生成对应文本内容后,下面代码生成图片内容
//查找word文档内所有的table
Iterator<XWPFTable> tables = doc.getTablesIterator();
//跳过第一个table
for(int i = 0;i < 1;i++) {
tables.hasNext();
tables.next();
}
//下面注释代码是遍历剩下的Word里面的所有表格,并获取每个空格内容
// while(tables.hasNext()) {
// XWPFTable table = tables.next();
// List<XWPFTableRow> rows = table.getRows();
// for(int i = 0;i < rows.size();i++) {
// XWPFTableRow row = rows.get(i);
// List<XWPFTableCell> cells = row.getTableCells();
// for(int j = 0;j < cells.size();j++) {
// XWPFTableCell cell = cells.get(j);
// System.out.print(cell.getText() + "\t");
// }
// System.out.println();
// }
// }
//跳到第二个table
tables.hasNext();
//获取table对象
XWPFTable table = tables.next();
//获取行列表
List<XWPFTableRow> rows = table.getRows();
//获取第一行对象
XWPFTableRow row = rows.get(0);
//获取第一行的所有cell cell格子,监狱?
List<XWPFTableCell> cells = row.getTableCells();
//获取最后一格的对象
XWPFTableCell cell = cells.get(cells.size()-1);
//移除最后一格内容
cell.removeParagraph(0);
//增加最后一格段落内容
XWPFParagraph cellP = cell.addParagraph();
//设置段落居中
cellP.setAlignment(ParagraphAlignment.CENTER);
//创建XWFPRun对象
XWPFRun r = cellP.createRun();
// 图片地址
// String imgFile = "https://timgsa.baidu.com.jpg";
//根据图片地址url获取图片
// URL url = new URL(imgFile);
// HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// conn.setRequestMethod("GET");
// conn.setConnectTimeout(50 * 1000);
// InputStream inStream = conn.getInputStream();
//根据图片路径
String imgFile = "C:/dev/workspace/hrywcl/src/main/resources/file/a.jpg";
// 设置图片默认类型
int format = XWPFDocument.PICTURE_TYPE_JPEG;
// 判断图片类型
if (imgFile.endsWith(".emf")) {
format = XWPFDocument.PICTURE_TYPE_EMF;
} else if (imgFile.endsWith(".wmf")) {
format = XWPFDocument.PICTURE_TYPE_WMF;
} else if (imgFile.endsWith(".pict")) {
format = XWPFDocument.PICTURE_TYPE_PICT;
} else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
format = XWPFDocument.PICTURE_TYPE_JPEG;
} else if (imgFile.endsWith(".png")) {
format = XWPFDocument.PICTURE_TYPE_PNG;
} else if (imgFile.endsWith(".dib")) {
format = XWPFDocument.PICTURE_TYPE_DIB;
} else if (imgFile.endsWith(".gif")) {
format = XWPFDocument.PICTURE_TYPE_GIF;
} else if (imgFile.endsWith(".tiff")) {
format = XWPFDocument.PICTURE_TYPE_TIFF;
} else if (imgFile.endsWith(".eps")) {
format = XWPFDocument.PICTURE_TYPE_EPS;
} else if (imgFile.endsWith(".bmp")) {
format = XWPFDocument.PICTURE_TYPE_BMP;
} else if (imgFile.endsWith(".wpg")) {
format = XWPFDocument.PICTURE_TYPE_WPG;
}
// 创建输入流 (根据图片地址url添加图片)
// try (InputStream is = inStream) {
// // 加入图片 is:输入流 format:图片类型 imgFile:图片地址 Units.toEMU(200):像素规格 200x200 pixels
// r.addPicture(is, format, imgFile, Units.toEMU(90), Units.toEMU(110));
// } catch (InvalidFormatException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 创建输入流 (根据路径添加图片)
try (FileInputStream is = new FileInputStream(imgFile)) {
// 加入图片 is:输入流 format:图片类型 imgFile:图片地址 Units.toEMU(200):像素规格 200x200 pixels
r.addPicture(is, format, imgFile, Units.toEMU(200), Units.toEMU(200));
}
try (FileOutputStream out = new FileOutputStream("e:/呵呵.docx")) {
// 输出doc文档
doc.write(out);
}
}
当然如果是段落的话,可以按照下面的方法在指定位置添加图片
//插入图片
if(text.equals("${Picture}")){
XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.CENTER);//居中
XWPFRun newParaRun = newPara.createRun();
newParaRun.addPicture(new FileInputStream("./doc/bus.png"),XWPFDocument.PICTURE_TYPE_PNG,"bus.png,",Units.toEMU(200), Units.toEMU(200));
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}