fix pic for word

This commit is contained in:
Looly 2020-03-20 09:21:18 +08:00
parent 249f20d0ed
commit 81c2850cfc
6 changed files with 74 additions and 7 deletions

View File

@ -19,6 +19,8 @@
### Bug修复
* 【core 】 修复TypeUtil无法获取泛型接口的泛型参数问题issue#I1BRFI@Gitee
* 【core 】 修复MySQL中0000报错问题
* 【core 】 修复BeanPath从Map取值为空的问题issue#790@Github
* 【poi 】 修复添加图片尺寸的单位问题issue#I1C2ER@Gitee
-------------------------------------------------------------------------------------------------------------
## 5.2.3

View File

@ -199,10 +199,10 @@ public class BeanPath implements Serializable{
}
if (bean instanceof Map) {
// 只支持String为key的Map
MapUtil.getAny((Map<String, ?>) bean, unwrapedKeys);
return MapUtil.getAny((Map<String, ?>) bean, unwrapedKeys);
} else {
final Map<String, Object> map = BeanUtil.beanToMap(bean);
MapUtil.getAny(map, unwrapedKeys);
return MapUtil.getAny(map, unwrapedKeys);
}
}
} else {

View File

@ -399,12 +399,12 @@ public class ImgUtil {
if (srcWidth % destWidth == 0) {
cols = srcWidth / destWidth;
} else {
cols = (int) Math.floor((double)srcWidth / destWidth) + 1;
cols = (int) Math.floor((double) srcWidth / destWidth) + 1;
}
if (srcHeight % destHeight == 0) {
rows = srcHeight / destHeight;
} else {
rows = (int) Math.floor((double)srcHeight / destHeight) + 1;
rows = (int) Math.floor((double) srcHeight / destHeight) + 1;
}
// 循环建立切片
Image tag;
@ -1248,6 +1248,18 @@ public class ImgUtil {
return read(new ByteArrayInputStream(imageBytes));
}
/**
* 将图片对象转换为InputStream形式
*
* @param image 图片对象
* @param imageType 图片类型
* @return Base64的字符串表现形式
* @since 4.2.4
*/
public static ByteArrayInputStream toStream(Image image, String imageType) {
return IoUtil.toStream(toBytes(image, imageType));
}
/**
* 将图片对象转换为Base64形式
*
@ -1257,9 +1269,21 @@ public class ImgUtil {
* @since 4.1.8
*/
public static String toBase64(Image image, String imageType) {
return Base64.encode(toBytes(image, imageType));
}
/**
* 将图片对象转换为bytes形式
*
* @param image 图片对象
* @param imageType 图片类型
* @return Base64的字符串表现形式
* @since 5.2.4
*/
public static byte[] toBytes(Image image, String imageType) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
write(image, imageType, out);
return Base64.encode(out.toByteArray());
return out.toByteArray();
}
/**

View File

@ -99,4 +99,13 @@ public class BeanPathTest {
Object result = pattern.get(tempMap);
Assert.assertEquals(2, result);
}
@Test
public void getMapTest () {
BeanPath pattern = BeanPath.create("userInfo[id, photoPath]");
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>)pattern.get(tempMap);
Assert.assertEquals(1, result.get("id"));
Assert.assertEquals("yx.mm.com", result.get("photoPath"));
}
}

View File

@ -7,6 +7,7 @@ import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.poi.exceptions.POIException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
@ -168,7 +169,7 @@ public class Word07Writer implements Closeable {
}
/**
* 增加图片单独成段落
* 增加图片单独成段落增加后图片流关闭默认居中对齐
*
* @param in 图片流
* @param picType 图片类型见Document.PICTURE_TYPE_XXX
@ -179,14 +180,33 @@ public class Word07Writer implements Closeable {
* @since 5.1.6
*/
public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height) {
return addPicture(in, picType, fileName, width, height, ParagraphAlignment.CENTER);
}
/**
* 增加图片单独成段落增加后图片流关闭
*
* @param in 图片流
* @param picType 图片类型见Document.PICTURE_TYPE_XXX
* @param fileName 文件名
* @param width 宽度
* @param height 高度
* @param align 图片的对齐方式
* @return this
* @since 5.2.4
*/
public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height, ParagraphAlignment align) {
final XWPFParagraph paragraph = doc.createParagraph();
paragraph.setAlignment(align);
final XWPFRun run = paragraph.createRun();
try {
run.addPicture(in, picType.getValue(), fileName, width, height);
run.addPicture(in, picType.getValue(), fileName, Units.toEMU(width), Units.toEMU(height));
} catch (InvalidFormatException e) {
throw new POIException(e);
} catch (IOException e) {
throw new IORuntimeException(e);
} finally {
IoUtil.close(in);
}
return this;

View File

@ -1,6 +1,7 @@
package cn.hutool.poi.word.test;
import java.awt.Font;
import java.io.File;
import org.junit.Ignore;
import org.junit.Test;
@ -21,4 +22,15 @@ public class WordWriterTest {
writer.close();
Console.log("OK");
}
@Test
// @Ignore
public void writePicTest() {
Word07Writer writer = new Word07Writer();
writer.addPicture(new File("d:\\test\\qrcodeCustom.jpg"), 100, 200);
// 写出到文件
writer.flush(FileUtil.file("d:/test/writePic.docx"));
// 关闭
writer.close();
}
}