package com.huimv.receive.common.utils; //import cn.bt.common.utils.StringUtils; //import cn.bt.modules.process.instance.enums.InstanceStatusEnum; //import com.itextpdf.text.*; //import com.itextpdf.text.pdf.*; import cn.hutool.core.io.IoUtil; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /** * @Author: xp * @Date: 2021/7/14 */ public class PdfUtil { /** * 返回PDF流 * * @param response 相应设置 * @param pathName 水印文件路径和名称 * @throws Exception 异常 */ /*public static void returnPdfStream(HttpServletResponse response, String pathName) throws Exception { response.setContentType("application/pdf"); File file = new File(pathName); if (file.exists()) { FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] b = new byte[1024 * 4]; int n; while ((n = in.read(b)) > -1) { out.write(b, 0, n); } out.flush(); in.close(); out.close(); } }*/ public static void returnPdfStream2(HttpServletResponse response, String pathName, String subsidyNane) throws Exception { InputStream in = null; try { File file = new File(pathName); in = new BufferedInputStream(new FileInputStream(file)); String filename = new String((subsidyNane+".pdf").getBytes(), "ISO8859_1"); response.setContentType("application/binary;charset=ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + filename);// 组装附件名称和格式 IoUtil.copy(in, response.getOutputStream()); } catch (Exception e) { System.out.println("------------ 这里除了问题!!!-----------"); System.out.println(e); }finally { if (in != null) { in.close(); } } } public static void returnPdfStream3(HttpServletResponse response, String pathName, String subsidyNane) throws Exception { InputStream in = null; try { File file = new File(pathName); in = new BufferedInputStream(new FileInputStream(file)); String filename = new String((subsidyNane+".xls").getBytes(), "ISO8859_1"); response.setContentType("application/binary;charset=ISO8859_1"); response.setHeader("Content-disposition", "attachment; filename=" + filename);// 组装附件名称和格式 IoUtil.copy(in, response.getOutputStream()); } catch (Exception e) { System.out.println("------------ 这里除了问题!!!-----------"); System.out.println(e); }finally { if (in != null) { in.close(); } } } /** * 返回水印图片路径 * * @param instanceStatus 流程状态 * @return 水印图片路径 */ // public static String returnWatermarkPath(int instanceStatus) { // String watermarkPath = "ioffice-api/src/main/resources/static/watermark"; // if (InstanceStatusEnum.E_0.getCode().equals(instanceStatus)) { // watermarkPath = watermarkPath + "/已作废.png"; // // } else if (InstanceStatusEnum.E_2.getCode().equals(instanceStatus)) { // watermarkPath = watermarkPath + "/审批通过.png"; // // } else { // watermarkPath = watermarkPath + "/审批中.png"; // } // return watermarkPath; // } // // /** // * 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 // * // * @param pdfPath 保存路径 // * @param document 文档 // * @param pdfPathName 文件保存路径和名称 // * @return 书写器(Writer) // * @throws Exception // */ // public static PdfWriter createPdfWriter(String pdfPath, Document document, String pdfPathName) throws Exception { // // //判断文件夹是否存在 // File file = new File(pdfPath); // if (!file.exists()) { // file.mkdir(); // } // file = new File(pdfPathName); // // PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); // writer.setViewerPreferences(PdfWriter.PageModeUseThumbs); // // return writer; // } // // /** // * 设置第一标题内容 // * // * @param title 第一标题 // * @param document 文档 // * @throws Exception 异常 // */ // public static void setPdfFirstTitle(String title, Document document) throws Exception { // Paragraph paragraph = new Paragraph(title, getPdfChineseFont(0)); // paragraph.setAlignment(Element.ALIGN_CENTER); // paragraph.setSpacingAfter(2); // document.add(paragraph); // } // // /** // * 设置第二标题内容 // * // * @param title 第二标题 // * @param document 文档 // * @param alignment 对齐方式 // * @param firstLineIndent 第一行缩进 // * @param spacingAfter 之后间隔 // * @throws Exception // */ // public static void setPdfSecondTitle(String title, Document document, int alignment, int firstLineIndent, int spacingAfter) throws Exception { // Paragraph lsh = new Paragraph(title, getPdfChineseFont(1)); // lsh.setAlignment(alignment); // lsh.setFirstLineIndent(firstLineIndent); // lsh.setSpacingAfter(spacingAfter); // document.add(lsh); // } // // /** // * 设置表格内容 并将表格加入文档中 // * // * @param dates 数据 // * @param document 文档 // * @param table 表格 // * @throws Exception 异常 // */ // public static void setPdfTableContent(Object[][] dates, Document document, PdfPTable table) throws Exception { // for (int i = 0; i < dates.length; i++) { // for (int j = 0; j < dates[i].length; j++) { // //表格的单元格 // PdfPCell pdfCell = new PdfPCell(); // //设置表格行高 // if (i > 0 && i < 3) { // pdfCell.setMinimumHeight(50); // } else { // pdfCell.setMinimumHeight(25); // } // Paragraph paragraph = new Paragraph(dates[i][j] + "", getPdfChineseFont(1)); // pdfCell.setPhrase(paragraph); // // setCellStyle(pdfCell); // table.addCell(pdfCell); // } // } // // document.add(table); // } // // /** // * 设置水印 // * // * @param inputFile 要这设置水印的文件路径 // * @param imageFile 水印图片路径 // * @param tag 标记 // * @return 生成水印文件的路径 // */ // public static String imageWaterMark(String inputFile, String imageFile, String tag) throws Exception { // String[] spe = separatePath(inputFile); // String outputFile = spe[0] + tag + "." + spe[1]; // // PdfReader reader = new PdfReader(inputFile); // PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile)); // // int total = reader.getNumberOfPages() + 1; // // Image image = Image.getInstance(imageFile); // //坐标 // image.setAbsolutePosition(389, 730); // //自定义大小 // image.scaleAbsolute(80, 80); // //旋转 弧度 // image.setRotation(-55); // // //旋转 角度 // image.setRotationDegrees(0); // //依照比例缩放 // image.scalePercent(50); // // PdfGState gs = new PdfGState(); // // 设置透明度为0.2 // gs.setFillOpacity(0.2f); // // // PdfContentByte under; // //给每一页加水印 // for (int i = 1; i < total; i++) { // under = stamper.getUnderContent(i); // under.beginText(); // // 添加水印图片 // under.addImage(image); // under.setGState(gs); // } // stamper.close(); // reader.close(); // return outputFile; // } // // /** // * 拆分原文件路径 // * // * @param path 原文件路径 // * @return 拆分原路径的数组 // */ // public static String[] separatePath(String path) { // if (StringUtils.isBlank(path)) { // return null; // } // String[] sep = path.split("\\."); // return new String[]{sep[0], sep[1]}; // } // // /** // * 设置表格单元格样式 // * // * @param pdfCell 单元格对象 // */ // public static void setCellStyle(PdfPCell pdfCell) { // pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER); // pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // pdfCell.setBackgroundColor(new BaseColor(0xdd7e6b)); // pdfCell.setBorderWidthTop(0.1f); // pdfCell.setBorderWidthBottom(0.1f); // pdfCell.setBorderWidthLeft(0.1f); // pdfCell.setBorderWidthRight(0.1f); // } // // /** // * 设置pdf字体及大小 // * // * @param type 标题或者内容 0-内容 1-标题 // * @return 字体 // * @throws Exception 异常 // */ // public static Font getPdfChineseFont(int type) throws Exception { // // 使用系统字体 // BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // Font font; // if (type == 1) { // font = new Font(bfChinese); // } else { // font = new Font(bfChinese, 18, Font.BOLD); // } // // return font; // } // // /** // * 删除文件夹 // * // * @param folderPath 文件路基 // */ // public static void delFolder(String folderPath) { // // 删除完里面所有内容 // delAllFile(folderPath); // String filePath = folderPath; // filePath = filePath.toString(); // java.io.File myFilePath = new java.io.File(filePath); // // 删除空文件夹 // myFilePath.delete(); // } // // /** // * 删除指定文件夹下所有文件 // * // * @param path 文件路基 // * @return 是否成功 // */ // public static boolean delAllFile(String path) { // boolean flag = false; // File file = new File(path); // String[] tempList = file.list(); // File temp = null; // for (int i = 0; i < tempList.length; i++) { // if (path.endsWith(File.separator)) { // temp = new File(path + tempList[i]); // } else { // temp = new File(path + File.separator + tempList[i]); // } // if (temp.isFile()) { // temp.delete(); // } // if (temp.isDirectory()) { // // 先删除文件夹里面的文件 // delAllFile(path + "/" + tempList[i]); // // 再删除空文件夹 // delFolder(path + "/" + tempList[i]); // flag = true; // } // } // return flag; // } // public static boolean delAllFile(String path) { // boolean flag = false; // File file = new File(path); // File temp = null; // // if (temp.isFile()) { // temp.delete(); // } // return flag; // } }