2021-03-30 20:00发布
/** * 写入图片在word中 * @param document * @throws IOException * @throws InvalidFormatException */ private void insertImage(CustomXWPFDocument document) throws IOException, InvalidFormatException { //图片 FileInputStream in = new FileInputStream(new("C:\Users\16630\Desktop\image.jpg")); //段落集合 List paragraphs = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { //获取到段落中的所有文本内容 String text = paragraph.getText(); //判断此段落中是否有需要进行替换的文本 if (WordUtil.checkText(text)) { List runs = paragraph.getRuns(); for (XWPFRun run : runs) { //替换模板原来位置 String key = "${image}"; if (run.toString().indexOf(key) != -1) { byte[] ba = new byte[in.available()]; int len = in.read(ba); ByteArrayInputStream byteInputStream = new ByteArrayInputStream(ba, 0, len); //设置图片 document.addPictureData(byteInputStream, XWPFDocument.PICTURE_TYPE_PNG); //创建一个word图片,并插入到文档中-->像素可改 document.createPicture(document.getAllPictures().size() - 1, 240, 240,paragraph); } break; } break; } } }
最多设置5个标签!
一周热门 更多>