|
@@ -53,7 +53,7 @@ public class DownloadWordDemo {
|
|
|
XWPFParagraph p1 = row.getCell(j).getParagraphs().get(0);
|
|
|
//通过分隔符获取多张图片路径
|
|
|
String[] filePaths = analysisResult.split(",");
|
|
|
- if(filePaths.length > 0){
|
|
|
+ if (filePaths.length > 0) {
|
|
|
XWPFRun run = null;
|
|
|
//设置图片上的文字描述
|
|
|
// String[] runTexts=imgResult.split("\r\n");
|
|
@@ -63,12 +63,12 @@ public class DownloadWordDemo {
|
|
|
// run.setText(s);
|
|
|
// run.addBreak();
|
|
|
// }
|
|
|
- run=p1.createRun();
|
|
|
+ run = p1.createRun();
|
|
|
// run.addBreak(); //换行
|
|
|
//一个单元格中每行设置几张图片 我这里设置的是每行1张图片 然后换行
|
|
|
//注意: 每行几张图片根据自己业务修改
|
|
|
- for (int k = 0;k < filePaths.length; k++ ){
|
|
|
- if(filePaths[k].toUpperCase().contains(".JPG")||filePaths[k].toUpperCase().contains(".PNG")||filePaths[k].toUpperCase().contains(".SVG")){
|
|
|
+ for (int k = 0; k < filePaths.length; k++) {
|
|
|
+ if (filePaths[k].toUpperCase().contains(".JPG") || filePaths[k].toUpperCase().contains(".PNG") || filePaths[k].toUpperCase().contains(".SVG")) {
|
|
|
// run.addTab(); //添加一个tab
|
|
|
// int index = filePaths[k].lastIndexOf("\\");
|
|
|
// String newStr = filePaths[k].substring(index+1);
|
|
@@ -80,7 +80,7 @@ public class DownloadWordDemo {
|
|
|
InputStream stream = new FileInputStream(filePaths[k]);
|
|
|
//设置第一张图片
|
|
|
//参数1:图片流数据 参数2:图片类型 参数3 图片名称 参数4:图片宽度 参数5:图片高度
|
|
|
- run.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, "Generated"+k, Units.toEMU(420), Units.toEMU(700));
|
|
|
+ run.addPicture(stream, XWPFDocument.PICTURE_TYPE_PNG, "Generated" + k, Units.toEMU(420), Units.toEMU(700));
|
|
|
// run.addTab();
|
|
|
// run.addTab();
|
|
|
// int lastK = k + 1;
|
|
@@ -118,7 +118,6 @@ public class DownloadWordDemo {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 跨列合并
|
|
|
*
|
|
@@ -356,198 +355,199 @@ public class DownloadWordDemo {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ DownloadWordDemo dw = new DownloadWordDemo();
|
|
|
+ dw.downloadWord();
|
|
|
|
|
|
-// public static void main (String[]args) throws IOException {
|
|
|
-// DownloadWordDemo dw = new DownloadWordDemo();
|
|
|
-// dw.downloadWord();
|
|
|
-//
|
|
|
-// }
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * url资源转化为file流
|
|
|
- *
|
|
|
- * @param url
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<File> urlToPdfFile (URL url){
|
|
|
- byte[] bytes = getImageFromNetByUrl(url);
|
|
|
- File file = writePDFToDisk(bytes);
|
|
|
- List<File> list = new ArrayList<>();
|
|
|
- try {
|
|
|
- PDDocument doc = PDDocument.load(file);
|
|
|
- PDFRenderer renderer = new PDFRenderer(doc);
|
|
|
- int pageCount = doc.getNumberOfPages();
|
|
|
-
|
|
|
- //pdf转图片后分块
|
|
|
- int number = pageCount / 10 + 1;
|
|
|
- //保存文件标识
|
|
|
- boolean flag = true;
|
|
|
- //拆分成10张
|
|
|
- for (int i = 0; i < 10; i++) {
|
|
|
- BufferedImage image = null;
|
|
|
- for (int j = i * number; j < (i + 1) * number; j++) {
|
|
|
- if (j >= pageCount) {
|
|
|
- flag = false;
|
|
|
- break;
|
|
|
- }
|
|
|
- if (j == 0) {
|
|
|
- image = renderer.renderImageWithDPI(j, 72);
|
|
|
- } else {
|
|
|
- BufferedImage tmpImage = renderer.renderImageWithDPI(j, 72);
|
|
|
- image = mergeImage(image, tmpImage, true, true, true, 0, null);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * url资源转化为file流
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<File> urlToPdfFile(URL url) {
|
|
|
+ byte[] bytes = getImageFromNetByUrl(url);
|
|
|
+ File file = writePDFToDisk(bytes);
|
|
|
+ List<File> list = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ PDDocument doc = PDDocument.load(file);
|
|
|
+ PDFRenderer renderer = new PDFRenderer(doc);
|
|
|
+ int pageCount = doc.getNumberOfPages();
|
|
|
+
|
|
|
+ //pdf转图片后分块
|
|
|
+ int number = pageCount / 10 + 1;
|
|
|
+ //保存文件标识
|
|
|
+ boolean flag = true;
|
|
|
+ //拆分成10张
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ BufferedImage image = null;
|
|
|
+ for (int j = i * number; j < (i + 1) * number; j++) {
|
|
|
+ if (j >= pageCount) {
|
|
|
+ flag = false;
|
|
|
+ break;
|
|
|
}
|
|
|
- if (flag) {
|
|
|
- file = new File("templates" + File.separator + "tmpPdf" + i + ".png");
|
|
|
- ImageIO.write(image, "png", file);
|
|
|
- list.add(file);
|
|
|
+ if (j == 0) {
|
|
|
+ image = renderer.renderImageWithDPI(j, 72);
|
|
|
+ } else {
|
|
|
+ BufferedImage tmpImage = renderer.renderImageWithDPI(j, 72);
|
|
|
+ image = mergeImage(image, tmpImage, true, true, true, 0, null);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ if (flag) {
|
|
|
+ file = new File("templates" + File.separator + "tmpPdf" + i + ".png");
|
|
|
+ ImageIO.write(image, "png", file);
|
|
|
+ list.add(file);
|
|
|
+ }
|
|
|
}
|
|
|
- return list;
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ return list;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 合并两个图片
|
|
|
- * @param first 图片1
|
|
|
- * @param second 图片2
|
|
|
- * @param horizontal 等于true,则两个图片水平合并显示, 否则两个图片垂直合并显示
|
|
|
- * @param center 图片是否水平居中, horizontal 等于 false 垂直合并才有效
|
|
|
- * @param transparent 合并后的图片背景是否透明色
|
|
|
- * @param gap 图片之间的间距
|
|
|
- * @param color 图片的背景颜色
|
|
|
- * @return 返回合并之后的图片
|
|
|
- */
|
|
|
- public static BufferedImage mergeImage (BufferedImage first, BufferedImage second,boolean horizontal,
|
|
|
- boolean center, boolean transparent, int gap, Color color) throws IOException {
|
|
|
- if (first == null) {
|
|
|
- return second;
|
|
|
- } else if (second == null) {
|
|
|
- return first;
|
|
|
- }
|
|
|
- // 获取原始图片宽度
|
|
|
- int firstWidth = first.getWidth();
|
|
|
- int firstHeight = first.getHeight();
|
|
|
- int secondWidth = second.getWidth();
|
|
|
- int secondHeight = second.getHeight();
|
|
|
- int picGap = gap * 2;
|
|
|
- // 合并后的图片宽高
|
|
|
- int mergeWidth = Math.max(firstWidth, secondWidth) + picGap;
|
|
|
- int mergeHeight = firstHeight + secondHeight + picGap;
|
|
|
- if (horizontal) {
|
|
|
- mergeWidth = firstWidth + secondWidth + picGap;
|
|
|
- mergeHeight = Math.max(firstHeight, secondHeight) + picGap;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 合并两个图片
|
|
|
+ *
|
|
|
+ * @param first 图片1
|
|
|
+ * @param second 图片2
|
|
|
+ * @param horizontal 等于true,则两个图片水平合并显示, 否则两个图片垂直合并显示
|
|
|
+ * @param center 图片是否水平居中, horizontal 等于 false 垂直合并才有效
|
|
|
+ * @param transparent 合并后的图片背景是否透明色
|
|
|
+ * @param gap 图片之间的间距
|
|
|
+ * @param color 图片的背景颜色
|
|
|
+ * @return 返回合并之后的图片
|
|
|
+ */
|
|
|
+ public static BufferedImage mergeImage(BufferedImage first, BufferedImage second, boolean horizontal,
|
|
|
+ boolean center, boolean transparent, int gap, Color color) throws IOException {
|
|
|
+ if (first == null) {
|
|
|
+ return second;
|
|
|
+ } else if (second == null) {
|
|
|
+ return first;
|
|
|
+ }
|
|
|
+ // 获取原始图片宽度
|
|
|
+ int firstWidth = first.getWidth();
|
|
|
+ int firstHeight = first.getHeight();
|
|
|
+ int secondWidth = second.getWidth();
|
|
|
+ int secondHeight = second.getHeight();
|
|
|
+ int picGap = gap * 2;
|
|
|
+ // 合并后的图片宽高
|
|
|
+ int mergeWidth = Math.max(firstWidth, secondWidth) + picGap;
|
|
|
+ int mergeHeight = firstHeight + secondHeight + picGap;
|
|
|
+ if (horizontal) {
|
|
|
+ mergeWidth = firstWidth + secondWidth + picGap;
|
|
|
+ mergeHeight = Math.max(firstHeight, secondHeight) + picGap;
|
|
|
+ }
|
|
|
|
|
|
- // 创建目标图片对象
|
|
|
- BufferedImage target = new BufferedImage(mergeWidth, mergeHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
- if (transparent) {
|
|
|
- // 设置图片背景为透明的
|
|
|
- BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(target);
|
|
|
- target = config.createCompatibleImage(mergeWidth, mergeHeight, Transparency.TRANSLUCENT);
|
|
|
- }
|
|
|
+ // 创建目标图片对象
|
|
|
+ BufferedImage target = new BufferedImage(mergeWidth, mergeHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
+ if (transparent) {
|
|
|
+ // 设置图片背景为透明的
|
|
|
+ BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(target);
|
|
|
+ target = config.createCompatibleImage(mergeWidth, mergeHeight, Transparency.TRANSLUCENT);
|
|
|
+ }
|
|
|
|
|
|
- // 创建绘制目标图片对象
|
|
|
- Graphics2D graphics = target.createGraphics();
|
|
|
- int x1, y1;
|
|
|
- int x2, y2;
|
|
|
- if (horizontal) {
|
|
|
- // 水平合并
|
|
|
- x1 = gap;
|
|
|
- y1 = gap;
|
|
|
- x2 = firstWidth + gap;
|
|
|
- y2 = gap;
|
|
|
+ // 创建绘制目标图片对象
|
|
|
+ Graphics2D graphics = target.createGraphics();
|
|
|
+ int x1, y1;
|
|
|
+ int x2, y2;
|
|
|
+ if (horizontal) {
|
|
|
+ // 水平合并
|
|
|
+ x1 = gap;
|
|
|
+ y1 = gap;
|
|
|
+ x2 = firstWidth + gap;
|
|
|
+ y2 = gap;
|
|
|
+ } else {
|
|
|
+ // 垂直合并
|
|
|
+ if (center) {
|
|
|
+ // 计算居中位置
|
|
|
+ x1 = (mergeWidth - firstWidth) / 2;
|
|
|
+ x2 = (mergeWidth - secondWidth) / 2;
|
|
|
} else {
|
|
|
- // 垂直合并
|
|
|
- if (center) {
|
|
|
- // 计算居中位置
|
|
|
- x1 = (mergeWidth - firstWidth) / 2;
|
|
|
- x2 = (mergeWidth - secondWidth) / 2;
|
|
|
- } else {
|
|
|
- x1 = gap;
|
|
|
- x2 = gap;
|
|
|
- }
|
|
|
- y1 = gap;
|
|
|
- y2 = firstHeight + gap;
|
|
|
- }
|
|
|
-
|
|
|
- // 图片的背景颜色
|
|
|
- if (color != null) {
|
|
|
- graphics.setColor(color);
|
|
|
- graphics.fillRect(0, 0, mergeWidth, mergeHeight);
|
|
|
+ x1 = gap;
|
|
|
+ x2 = gap;
|
|
|
}
|
|
|
+ y1 = gap;
|
|
|
+ y2 = firstHeight + gap;
|
|
|
+ }
|
|
|
|
|
|
- // 按照顺序绘制图片
|
|
|
- graphics.drawImage(first, x1, y1, firstWidth, firstHeight, null);
|
|
|
- graphics.drawImage(second, x2, y2, secondWidth, secondHeight, null);
|
|
|
- graphics.dispose();
|
|
|
- // 返回合并后的图片对象
|
|
|
- return target;
|
|
|
+ // 图片的背景颜色
|
|
|
+ if (color != null) {
|
|
|
+ graphics.setColor(color);
|
|
|
+ graphics.fillRect(0, 0, mergeWidth, mergeHeight);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 将获取的字节数组保存为文件写入硬盘
|
|
|
- *
|
|
|
- * @param data
|
|
|
- */
|
|
|
- public static File writePDFToDisk ( byte[] data){
|
|
|
- try {
|
|
|
- File file = new File("templates" + File.separator + "tmp.pdf"); // 本地目录
|
|
|
- File fileParent = file.getParentFile();
|
|
|
- if (!fileParent.exists()) {
|
|
|
- fileParent.mkdirs();
|
|
|
- file.createNewFile();
|
|
|
- }
|
|
|
- FileOutputStream fops = new FileOutputStream(file);
|
|
|
- fops.write(data);
|
|
|
- fops.flush();
|
|
|
- fops.close();
|
|
|
- return file;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ // 按照顺序绘制图片
|
|
|
+ graphics.drawImage(first, x1, y1, firstWidth, firstHeight, null);
|
|
|
+ graphics.drawImage(second, x2, y2, secondWidth, secondHeight, null);
|
|
|
+ graphics.dispose();
|
|
|
+ // 返回合并后的图片对象
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将获取的字节数组保存为文件写入硬盘
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ public static File writePDFToDisk(byte[] data) {
|
|
|
+ try {
|
|
|
+ File file = new File("templates" + File.separator + "tmp.pdf"); // 本地目录
|
|
|
+ File fileParent = file.getParentFile();
|
|
|
+ if (!fileParent.exists()) {
|
|
|
+ fileParent.mkdirs();
|
|
|
+ file.createNewFile();
|
|
|
}
|
|
|
- return null;
|
|
|
+ FileOutputStream fops = new FileOutputStream(file);
|
|
|
+ fops.write(data);
|
|
|
+ fops.flush();
|
|
|
+ fops.close();
|
|
|
+ return file;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 获取远程http地址视图片
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static byte[] getImageFromNetByUrl (URL url){
|
|
|
- try {
|
|
|
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
- conn.setRequestMethod("GET");
|
|
|
- conn.setConnectTimeout(5 * 1000);
|
|
|
- InputStream inStream = conn.getInputStream();
|
|
|
- byte[] btData = readInputStream(inStream);
|
|
|
- return btData;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
+ /**
|
|
|
+ * 获取远程http地址视图片
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] getImageFromNetByUrl(URL url) {
|
|
|
+ try {
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("GET");
|
|
|
+ conn.setConnectTimeout(5 * 1000);
|
|
|
+ InputStream inStream = conn.getInputStream();
|
|
|
+ byte[] btData = readInputStream(inStream);
|
|
|
+ return btData;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 读取流
|
|
|
- *
|
|
|
- * @param inStream
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static byte[] readInputStream (InputStream inStream) throws Exception {
|
|
|
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int len = 0;
|
|
|
- while ((len = inStream.read(buffer)) != -1) {
|
|
|
- outStream.write(buffer, 0, len);
|
|
|
- }
|
|
|
- inStream.close();
|
|
|
- return outStream.toByteArray();
|
|
|
+ /**
|
|
|
+ * 读取流
|
|
|
+ *
|
|
|
+ * @param inStream
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static byte[] readInputStream(InputStream inStream) throws Exception {
|
|
|
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = inStream.read(buffer)) != -1) {
|
|
|
+ outStream.write(buffer, 0, len);
|
|
|
}
|
|
|
+ inStream.close();
|
|
|
+ return outStream.toByteArray();
|
|
|
}
|
|
|
+}
|