|
@@ -0,0 +1,553 @@
|
|
|
+package com.yh.saas.plugin.yiliangyiyun.util;
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
|
|
+import com.winsea.svc.base.base.util.DateUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.httpclient.util.DateUtil;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+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;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFRun;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFTable;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFTableRow;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
|
|
|
+import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
+import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
|
|
|
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import sun.awt.image.BufferedImageGraphicsConfig;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+
|
|
|
+public class DownloadWordDemo {
|
|
|
+
|
|
|
+ //参数1:对应样例中测试0004的多张图片的路径
|
|
|
+ //参数2:行对象
|
|
|
+ //参数3:图片上方的文字描述
|
|
|
+ public static void setPics(String analysisResult, XWPFTableRow row, int j) throws IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException {
|
|
|
+ XWPFParagraph p1 = row.getCell(j).getParagraphs().get(0);
|
|
|
+ //通过分隔符获取多张图片路径
|
|
|
+ String[] filePaths = analysisResult.split(",");
|
|
|
+ if(filePaths.length > 0){
|
|
|
+ XWPFRun run = null;
|
|
|
+ //设置图片上的文字描述
|
|
|
+// String[] runTexts=imgResult.split("\r\n");
|
|
|
+// for (String s:runTexts){
|
|
|
+// run=p1.createRun();
|
|
|
+// run.setFontSize(9);
|
|
|
+// run.setText(s);
|
|
|
+// run.addBreak();
|
|
|
+// }
|
|
|
+ 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")){
|
|
|
+// run.addTab(); //添加一个tab
|
|
|
+// int index = filePaths[k].lastIndexOf("\\");
|
|
|
+// String newStr = filePaths[k].substring(index+1);
|
|
|
+// int indexOf = newStr.lastIndexOf(".");
|
|
|
+// String pic = newStr.substring(indexOf);
|
|
|
+// //获取文件名
|
|
|
+// String name = k+pic;
|
|
|
+ //获取文件流
|
|
|
+ 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.addTab();
|
|
|
+// run.addTab();
|
|
|
+// int lastK = k + 1;
|
|
|
+// String name2 = "";
|
|
|
+// if( (lastK) <= filePaths.length-1){
|
|
|
+// int index1 = filePaths[lastK].lastIndexOf("\\");
|
|
|
+// String newStr2 = filePaths[lastK].substring(index1+1);
|
|
|
+// int index2 = newStr2.lastIndexOf(".");
|
|
|
+// String pic2 = newStr2.substring(index2);
|
|
|
+// name2 = lastK+pic2;
|
|
|
+// InputStream stream2 = new FileInputStream(filePaths[lastK]);
|
|
|
+// // 设置第二张图片
|
|
|
+// run.addPicture(stream2, XWPFDocument.PICTURE_TYPE_PNG, "Generated"+lastK, Units.toEMU(450), Units.toEMU(700));
|
|
|
+// //换行
|
|
|
+// run.addBreak();
|
|
|
+// }
|
|
|
+// if(k == filePaths.length-1 && filePaths.length % 2 == 1){
|
|
|
+// run.addBreak();
|
|
|
+// }
|
|
|
+ //设置第一张图片的名称
|
|
|
+// run.setText(name);
|
|
|
+ //这里添加了两个tab 具体添加几个 根据自己的实际格式来调
|
|
|
+// run.addTab();
|
|
|
+// run.addTab();
|
|
|
+ //设置第二张图片的名称
|
|
|
+// run.setText(name2);
|
|
|
+
|
|
|
+// if(lastK == filePaths.length-1){
|
|
|
+// break;
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ row.getCell(j).setParagraph(p1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 跨列合并
|
|
|
+ *
|
|
|
+ * @param table
|
|
|
+ * @param row 所合并的行
|
|
|
+ * @param fromCell 起始列
|
|
|
+ * @param toCell 终止列
|
|
|
+ * @Description
|
|
|
+ */
|
|
|
+ public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
|
|
|
+ for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
|
|
|
+ XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
|
|
|
+ if (cellIndex == fromCell) {
|
|
|
+ cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
|
|
|
+ } else {
|
|
|
+ cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 跨行合并
|
|
|
+ *
|
|
|
+ * @param table
|
|
|
+ * @param col 合并的列
|
|
|
+ * @param fromRow 起始行
|
|
|
+ * @param toRow 终止行
|
|
|
+ * @Description
|
|
|
+ */
|
|
|
+ public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
|
|
|
+ for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
|
|
|
+ XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
|
|
|
+ //第一个合并单元格用重启合并值设置
|
|
|
+ if (rowIndex == fromRow) {
|
|
|
+ cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
|
|
|
+ } else {
|
|
|
+ //合并第一个单元格的单元被设置为“继续”
|
|
|
+ cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: 设置表格总宽度与水平对齐方式
|
|
|
+ */
|
|
|
+ public void setTableWidthAndHAlign(XWPFTable table, String width,
|
|
|
+ STJc.Enum enumValue) {
|
|
|
+ CTTblPr tblPr = getTableCTTblPr(table);
|
|
|
+ // 表格宽度
|
|
|
+ CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
|
|
|
+ if (enumValue != null) {
|
|
|
+ CTJc cTJc = tblPr.addNewJc();
|
|
|
+ cTJc.setVal(enumValue);
|
|
|
+ }
|
|
|
+ // 设置宽度
|
|
|
+ tblWidth.setW(new BigInteger(width));
|
|
|
+ tblWidth.setType(STTblWidth.DXA);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: 得到Table的CTTblPr, 不存在则新建
|
|
|
+ */
|
|
|
+ public CTTblPr getTableCTTblPr(XWPFTable table) {
|
|
|
+ CTTbl ttbl = table.getCTTbl();
|
|
|
+ // 表格属性
|
|
|
+ CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
|
|
|
+ return tblPr;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void downloadWord() throws IOException {
|
|
|
+ String filePath = "D:\\"; //生成一个文档
|
|
|
+ XWPFDocument document = new XWPFDocument();
|
|
|
+
|
|
|
+ XWPFParagraph para; //代表文档、表格、标题等种的段落,由多个XWPFRun组成
|
|
|
+ XWPFRun run; //代表具有同样风格的一段文本
|
|
|
+ XWPFTableRow row;//代表表格的一行
|
|
|
+ XWPFTableCell cell;//代表表格的一个单元格
|
|
|
+ CTTcPr cellPr; //单元格属性
|
|
|
+
|
|
|
+ //设置提案封面内容
|
|
|
+ XWPFParagraph proCoverParagraph = document.createParagraph(); //新建一个标题段落对象(就是一段文字)
|
|
|
+ proCoverParagraph.setAlignment(ParagraphAlignment.LEFT);//样式居中
|
|
|
+ XWPFRun coverRun0 = proCoverParagraph.createRun(); //创建文本对象
|
|
|
+ coverRun0.setBold(true); //加粗
|
|
|
+ coverRun0.setFontSize(12); //字体大小
|
|
|
+ coverRun0.setText(String.valueOf("附件:\r"));
|
|
|
+
|
|
|
+
|
|
|
+ XWPFParagraph proCoverParagraph1 = document.createParagraph(); //新建一个标题段落对象(就是一段文字)
|
|
|
+ proCoverParagraph1.setAlignment(ParagraphAlignment.CENTER);//样式左对齐
|
|
|
+ XWPFRun coverRun1 = proCoverParagraph1.createRun(); //创建文本对象
|
|
|
+ coverRun1.setBold(true); //加粗
|
|
|
+ coverRun1.setFontSize(41); //字体大小
|
|
|
+ coverRun1.setText(String.valueOf("不如意事常八九\r可与人言无二三\r\r提\r\r案\r\r表\r\r"));
|
|
|
+
|
|
|
+ XWPFParagraph proCoverParagraph2 = document.createParagraph(); //新建一个标题段落对象(就是一段文字)
|
|
|
+ proCoverParagraph2.setAlignment(ParagraphAlignment.LEFT);//样式居中
|
|
|
+ XWPFRun coverRun2 = proCoverParagraph2.createRun(); //创建文本对象
|
|
|
+ coverRun2.setBold(true); //加粗
|
|
|
+ coverRun2.setFontSize(16); //字体大小
|
|
|
+ coverRun2.setText(String.valueOf("\r编号:" + "2020001" + "\r填表时间:" + "2020" + "年" + "10" + "月" + "20" + "日"));//给文本赋值
|
|
|
+
|
|
|
+
|
|
|
+ //创建表格
|
|
|
+ XWPFTable table = document.createTable(10, 9); //10行9格
|
|
|
+ table.setCellMargins(3, 5, 3, 5);//在表级别设置单元格边距
|
|
|
+ //列宽自适应
|
|
|
+
|
|
|
+ //设置表格总宽度,字体居中
|
|
|
+// ct.setTableWidthAndHAlign(table, "10000", STJc.CENTER);
|
|
|
+
|
|
|
+ //固定表格宽度,关闭自动伸缩
|
|
|
+ CTTblPr tblPr = table.getCTTbl().getTblPr();
|
|
|
+ tblPr.getTblW().setType(STTblWidth.DXA);
|
|
|
+
|
|
|
+
|
|
|
+ //设置表格样式
|
|
|
+ List<XWPFTableRow> rowList = table.getRows();
|
|
|
+ //扫描行
|
|
|
+ for (int i = 0; i < rowList.size(); i++) {
|
|
|
+ XWPFTableRow infoTableRow = rowList.get(i);
|
|
|
+ List<XWPFTableCell> cellList = infoTableRow.getTableCells();
|
|
|
+ //扫描列
|
|
|
+ for (int j = 0; j < cellList.size(); j++) {
|
|
|
+ XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
|
|
|
+ cellParagraph.setAlignment(ParagraphAlignment.CENTER);//设置表格内容水平居中
|
|
|
+ XWPFRun cellParagraphRun = cellParagraph.createRun();//在段落创建一个文本
|
|
|
+ cellParagraphRun.setFontSize(12);//设置字体大小
|
|
|
+ cellParagraphRun.setBold(true);//加粗
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ XWPFTableCell cell1 = null;
|
|
|
+ cell1 = table.getRow(2).getCell(0);
|
|
|
+ cell1.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //设置第3~7行第1列单元格的内容垂直居中
|
|
|
+
|
|
|
+
|
|
|
+ //设置表格每列宽度
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ row = table.getRow(i);
|
|
|
+ //设置每行的格式(多少格)
|
|
|
+ for (int j = 0; j < 9; j++) {
|
|
|
+ cell = row.getCell(j);//获取行中第j块单元格
|
|
|
+
|
|
|
+ //设置单元格列宽
|
|
|
+ CTTcPr tcpr = cell.getCTTc().addNewTcPr();
|
|
|
+ CTTblWidth cellw = tcpr.addNewTcW();
|
|
|
+ if (j == 0) {
|
|
|
+ cellw.setW(BigInteger.valueOf(1900));
|
|
|
+ }
|
|
|
+ if (j == 1 || j == 5) {
|
|
|
+ cellw.setW(BigInteger.valueOf(550));
|
|
|
+ }
|
|
|
+ if (j == 2 || j == 4 || j == 6 || j == 8) {
|
|
|
+ cellw.setW(BigInteger.valueOf(1280));
|
|
|
+ }
|
|
|
+ if (j == 3 || j == 7) {
|
|
|
+ cellw.setW(BigInteger.valueOf(940));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //合并表格
|
|
|
+ mergeCellsHorizontal(table, 0, 1, 8);//合并第一行,第2~9列
|
|
|
+ mergeCellsHorizontal(table, 2, 1, 8);
|
|
|
+ mergeCellsHorizontal(table, 3, 1, 8);
|
|
|
+ mergeCellsHorizontal(table, 4, 1, 8);
|
|
|
+ mergeCellsHorizontal(table, 5, 1, 8);
|
|
|
+ mergeCellsHorizontal(table, 6, 1, 8);
|
|
|
+ mergeCellsVertically(table, 0, 2, 6);//合并第1列,第3~7行
|
|
|
+ mergeCellsVertically(table, 1, 2, 6);//合并第2列,3~7行
|
|
|
+
|
|
|
+ //对赋值表格
|
|
|
+ List<XWPFTableRow> rowList1 = table.getRows();
|
|
|
+ List<XWPFTableCell> cellList = new ArrayList<XWPFTableCell>();
|
|
|
+ XWPFParagraph cellParagraph = null;//存一行的列
|
|
|
+ XWPFRun cellParagraphRun = null;//单元格
|
|
|
+ //第1行第1列
|
|
|
+ cellList = rowList1.get(0).getTableCells();
|
|
|
+ cellParagraph = cellList.get(0).getParagraphArray(0);
|
|
|
+ cellParagraphRun = cellParagraph.getRuns().get(0);
|
|
|
+ cellParagraphRun.setText(String.valueOf("案名"));
|
|
|
+ cellParagraph = cellList.get(1).getParagraphArray(0);//第2格的段落
|
|
|
+ //第一行第2列
|
|
|
+ cellParagraphRun = cellParagraph.getRuns().get(0);
|
|
|
+ cellParagraphRun.setText(String.valueOf("案名是习惯过了头地方案名是习惯过了头"));
|
|
|
+
|
|
|
+ //第3行第1列
|
|
|
+ cellList = rowList1.get(2).getTableCells();
|
|
|
+ cellParagraph = cellList.get(0).getParagraphArray(0);//第2格的段落
|
|
|
+ cellParagraphRun = cellParagraph.getRuns().get(0);
|
|
|
+ cellParagraphRun.setText(String.valueOf("习惯过了头"));
|
|
|
+
|
|
|
+ //第3行第2列
|
|
|
+ cellParagraph = cellList.get(1).getParagraphArray(0);//第2格的段落
|
|
|
+ cellParagraph.setAlignment(ParagraphAlignment.LEFT);//设置左对齐
|
|
|
+ cellParagraphRun = cellParagraph.getRuns().get(0);
|
|
|
+ cellParagraphRun.setText(String.valueOf("习惯过了头:"));
|
|
|
+
|
|
|
+ //添加图片
|
|
|
+ try (FileInputStream is = new FileInputStream
|
|
|
+ (filePath + "1.jpg")) {
|
|
|
+ cellParagraphRun.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG,
|
|
|
+ filePath + "1.jpg",
|
|
|
+ Units.toEMU(120), Units.toEMU(20)); // 200x200 pixels
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ cellParagraphRun.addBreak();//换行
|
|
|
+ cellParagraphRun.addBreak();
|
|
|
+ XWPFRun cellParagraphRun1 = cellParagraph.createRun();
|
|
|
+ cellParagraphRun1.setFontFamily("微软雅黑");//设置字体
|
|
|
+ cellParagraphRun1.setText("c15宿舍要消毒");
|
|
|
+
|
|
|
+
|
|
|
+ //导出word文档
|
|
|
+ String fileName = "D:/poi导出word文档.docx";
|
|
|
+ File outputFolder = new File("D:/");
|
|
|
+ if (!outputFolder.exists()) {
|
|
|
+ outputFolder.mkdir();
|
|
|
+ }
|
|
|
+ String encode = System.getProperty("file.encoding");
|
|
|
+ try {
|
|
|
+ fileName = new String(fileName.getBytes("UTF-8"), encode);
|
|
|
+ } catch (UnsupportedEncodingException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ FileOutputStream fout = new FileOutputStream(fileName);
|
|
|
+ document.write(fout);
|
|
|
+ fout.close();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (flag) {
|
|
|
+ file = new File("templates" + File.separator + "tmpPdf" + i + ".png");
|
|
|
+ ImageIO.write(image, "png", file);
|
|
|
+ list.add(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建目标图片对象
|
|
|
+ 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;
|
|
|
+ } 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按照顺序绘制图片
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取流
|
|
|
+ *
|
|
|
+ * @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();
|
|
|
+ }
|
|
|
+ }
|