|
|
|
@ -32,8 +32,9 @@ import java.util.List;
|
|
|
|
* @Version: 1.0
|
|
|
|
* @Version: 1.0
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class Jpg2PdfUtil {
|
|
|
|
public class Jpg2PdfUtil {
|
|
|
|
static Logger logger = LoggerFactory.getLogger(Jpg2PdfUtil.class);
|
|
|
|
static Logger logger = LoggerFactory.getLogger(Jpg2PdfUtil.class);
|
|
|
|
public static void imageToPdf(HttpServletResponse response, List<String> filePaths, String pdfName) throws Exception{
|
|
|
|
|
|
|
|
|
|
|
|
public static void imageToPdf(HttpServletResponse response, List<String> filePaths, String pdfName) throws Exception {
|
|
|
|
// 实例化图牿
|
|
|
|
// 实例化图牿
|
|
|
|
Image image = null;
|
|
|
|
Image image = null;
|
|
|
|
pdfName = java.net.URLEncoder.encode(pdfName, "UTF-8");
|
|
|
|
pdfName = java.net.URLEncoder.encode(pdfName, "UTF-8");
|
|
|
|
@ -41,14 +42,14 @@ public class Jpg2PdfUtil {
|
|
|
|
response.reset();
|
|
|
|
response.reset();
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
response.setContentType("application/pdf"); // word格式
|
|
|
|
response.setContentType("application/pdf"); // word格式
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + pdfName+ "("+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +").pdf");
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + pdfName + "(" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ").pdf");
|
|
|
|
PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
|
|
|
|
PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
|
|
|
|
// 开启文桿
|
|
|
|
// 开启文桿
|
|
|
|
doc.open();
|
|
|
|
doc.open();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
for (String filePath : filePaths) {
|
|
|
|
for (String filePath : filePaths) {
|
|
|
|
File file1 = new File(filePath);
|
|
|
|
File file1 = new File(filePath);
|
|
|
|
if(file1.exists()){
|
|
|
|
if (file1.exists()) {
|
|
|
|
if (file1.getName().endsWith(".tif") || file1.getName().endsWith(".tiff")) {
|
|
|
|
if (file1.getName().endsWith(".tif") || file1.getName().endsWith(".tiff")) {
|
|
|
|
Object localObject1 = null;
|
|
|
|
Object localObject1 = null;
|
|
|
|
Object localObject2 = null;
|
|
|
|
Object localObject2 = null;
|
|
|
|
@ -81,7 +82,7 @@ public class Jpg2PdfUtil {
|
|
|
|
((RandomAccessFileOrArray) localObject1).close();
|
|
|
|
((RandomAccessFileOrArray) localObject1).close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else if (file1.getName().endsWith(".png")
|
|
|
|
} else if (file1.getName().endsWith(".png")
|
|
|
|
|| file1.getName().endsWith(".jpg")
|
|
|
|
|| file1.getName().endsWith(".jpg")
|
|
|
|
|| file1.getName().endsWith(".gif")
|
|
|
|
|| file1.getName().endsWith(".gif")
|
|
|
|
|| file1.getName().endsWith(".jpeg")
|
|
|
|
|| file1.getName().endsWith(".jpeg")
|
|
|
|
@ -101,21 +102,21 @@ public class Jpg2PdfUtil {
|
|
|
|
//image.scaleAbsolute(500, 400);
|
|
|
|
//image.scaleAbsolute(500, 400);
|
|
|
|
// 按百分比显示图片的比便
|
|
|
|
// 按百分比显示图片的比便
|
|
|
|
if (width > 1024 || heigth > 786) {
|
|
|
|
if (width > 1024 || heigth > 786) {
|
|
|
|
image.scalePercent(percent+5);
|
|
|
|
image.scalePercent(percent + 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PdfContentByte canvas = writer.getDirectContent();
|
|
|
|
PdfContentByte canvas = writer.getDirectContent();
|
|
|
|
//pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包
|
|
|
|
//pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包
|
|
|
|
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
|
|
|
|
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
|
|
|
|
"UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
|
|
|
|
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
|
|
|
Font fontChinese = new Font(bfChinese, 50, Font.NORMAL,Color.blue);
|
|
|
|
Font fontChinese = new Font(bfChinese, 50, Font.NORMAL, Color.blue);
|
|
|
|
Phrase phrase = new Phrase("老客户看黑科技好客户客户客户客户客户客户看黑科技",fontChinese);
|
|
|
|
Phrase phrase = new Phrase("老客户看黑科技好客户客户客户客户客户客户看黑科技", fontChinese);
|
|
|
|
ColumnText.showTextAligned(
|
|
|
|
ColumnText.showTextAligned(
|
|
|
|
canvas, Element.ALIGN_UNDEFINED, phrase, 200, 200, 50);
|
|
|
|
canvas, Element.ALIGN_UNDEFINED, phrase, 200, 200, 50);
|
|
|
|
Image image1 = null;
|
|
|
|
Image image1 = null;
|
|
|
|
image1 = Image.getInstance("D:\\出差准备文档\\pdf\\9d17f073c61adfaf79438401bc6ffb7.jpg");
|
|
|
|
image1 = Image.getInstance("D:\\出差准备文档\\pdf\\9d17f073c61adfaf79438401bc6ffb7.jpg");
|
|
|
|
//位置
|
|
|
|
//位置
|
|
|
|
image1.setAbsolutePosition(200, 200);
|
|
|
|
image1.setAbsolutePosition(200, 200);
|
|
|
|
// 设置图片的显示大小
|
|
|
|
// 设置图片的显示大小
|
|
|
|
image1.scaleToFit(200, 200);
|
|
|
|
image1.scaleToFit(200, 200);
|
|
|
|
canvas.addImage(image1);
|
|
|
|
canvas.addImage(image1);
|
|
|
|
doc.add(image);
|
|
|
|
doc.add(image);
|
|
|
|
@ -132,19 +133,20 @@ public class Jpg2PdfUtil {
|
|
|
|
writer.close();
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean check(String file) {
|
|
|
|
public static boolean check(String file) {
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
File f = new File(file);
|
|
|
|
File f = new File(file);
|
|
|
|
if(f.isFile()){
|
|
|
|
if (f.isFile()) {
|
|
|
|
pdfReader = new PdfReader(file);
|
|
|
|
pdfReader = new PdfReader(file);
|
|
|
|
if (pdfReader.getNumberOfPages() != 0) {
|
|
|
|
if (pdfReader.getNumberOfPages() != 0) {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
}finally {
|
|
|
|
} finally {
|
|
|
|
if(null != pdfReader){
|
|
|
|
if (null != pdfReader) {
|
|
|
|
pdfReader.close();
|
|
|
|
pdfReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -152,31 +154,31 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void mulFile2One(HttpServletResponse response,List<String> filePaths,String waterMarkName) {
|
|
|
|
public static void mulFile2One(HttpServletResponse response, List<String> filePaths, String waterMarkName) {
|
|
|
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
Iterator<String> iterator = filePaths.iterator();
|
|
|
|
Iterator<String> iterator = filePaths.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
File file = new File(fileStr);
|
|
|
|
File file = new File(fileStr);
|
|
|
|
if(file.isFile()){
|
|
|
|
if (file.isFile()) {
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
if(!flag){
|
|
|
|
if (!flag) {
|
|
|
|
iterator.remove();
|
|
|
|
iterator.remove();
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件损坏");
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + file + "文件损坏");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
iterator.remove();
|
|
|
|
iterator.remove();
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件不存在");
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + file + "文件不存在");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!filePaths.isEmpty()){
|
|
|
|
if (!filePaths.isEmpty()) {
|
|
|
|
// pdf合并工具类
|
|
|
|
// pdf合并工具类
|
|
|
|
Document document = null;
|
|
|
|
Document document = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
document = new Document(new PdfReader(filePaths.get(0)).getPageSize(1));
|
|
|
|
document = new Document(new PdfReader(filePaths.get(0)).getPageSize(1));
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
document.open();
|
|
|
|
document.open();
|
|
|
|
for (String file : filePaths) {
|
|
|
|
for (String file : filePaths) {
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
@ -198,21 +200,21 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
copy.addPage(page);
|
|
|
|
copy.addPage(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(null != pdfReader){
|
|
|
|
if (null != pdfReader) {
|
|
|
|
pdfReader.close();
|
|
|
|
pdfReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
reader.close();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
bos.flush();
|
|
|
|
bos.flush();
|
|
|
|
bos.close();
|
|
|
|
bos.close();
|
|
|
|
}catch (Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
} finally {
|
|
|
|
if(null != document){
|
|
|
|
if (null != document) {
|
|
|
|
document.close();
|
|
|
|
document.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -220,7 +222,7 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void setWatermark(ByteArrayOutputStream bos, PdfReader reader, String waterMarkName, String imgPath){
|
|
|
|
public static void setWatermark(ByteArrayOutputStream bos, PdfReader reader, String waterMarkName, String imgPath) {
|
|
|
|
PdfStamper stamper = null;
|
|
|
|
PdfStamper stamper = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
stamper = new PdfStamper(reader, bos);
|
|
|
|
stamper = new PdfStamper(reader, bos);
|
|
|
|
@ -231,7 +233,7 @@ public class Jpg2PdfUtil {
|
|
|
|
for (int i = 1; i < total; i++) {
|
|
|
|
for (int i = 1; i < total; i++) {
|
|
|
|
content = stamper.getOverContent(i);// 在内容上方加水印
|
|
|
|
content = stamper.getOverContent(i);// 在内容上方加水印
|
|
|
|
//加文字水印
|
|
|
|
//加文字水印
|
|
|
|
if(StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
gs.setFillOpacity(0.3f);
|
|
|
|
gs.setFillOpacity(0.3f);
|
|
|
|
gs.setStrokeOpacity(0.3f);
|
|
|
|
gs.setStrokeOpacity(0.3f);
|
|
|
|
content.setGState(gs);
|
|
|
|
content.setGState(gs);
|
|
|
|
@ -249,7 +251,7 @@ public class Jpg2PdfUtil {
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[0], 250, 1500, 40);
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[0], 250, 1500, 40);
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[0], 800, 1100, 40);
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[0], 800, 1100, 40);
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[0], 800, 1500, 40);*/
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[0], 800, 1500, 40);*/
|
|
|
|
if(waterMarks.length > 1){
|
|
|
|
if (waterMarks.length > 1) {
|
|
|
|
//v:距左 v1:距下 v2:
|
|
|
|
//v:距左 v1:距下 v2:
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[1], 310, 400, 40);
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarks[1], 310, 400, 40);
|
|
|
|
/* content.showTextAligned(Element.ALIGN_CENTER, waterMarks[1], 310, 150, 40);*/
|
|
|
|
/* content.showTextAligned(Element.ALIGN_CENTER, waterMarks[1], 310, 150, 40);*/
|
|
|
|
@ -262,7 +264,7 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content.endText();
|
|
|
|
content.endText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(StringUtils.isNotBlank(imgPath)){
|
|
|
|
if (StringUtils.isNotBlank(imgPath)) {
|
|
|
|
Image image = Image.getInstance(imgPath);
|
|
|
|
Image image = Image.getInstance(imgPath);
|
|
|
|
image.setAbsolutePosition(200, 206); // set the first background
|
|
|
|
image.setAbsolutePosition(200, 206); // set the first background
|
|
|
|
image.scaleToFit(200, 200);
|
|
|
|
image.scaleToFit(200, 200);
|
|
|
|
@ -283,26 +285,26 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void mulFile2One3(HttpServletResponse response,List<String> filePaths,String filename,EmrPdfWaterSet emrPdfWaterSet) {
|
|
|
|
public static void mulFile2One3(HttpServletResponse response, List<String> filePaths, String filename, EmrPdfWaterSet emrPdfWaterSet) {
|
|
|
|
String waterMarkName="";
|
|
|
|
String waterMarkName = "";
|
|
|
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
SimpleDateFormat fmt= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
Iterator<String> iterator = filePaths.iterator();
|
|
|
|
Iterator<String> iterator = filePaths.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
File file = new File(fileStr);
|
|
|
|
File file = new File(fileStr);
|
|
|
|
if(file.isFile()){
|
|
|
|
if (file.isFile()) {
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
if(!flag){
|
|
|
|
if (!flag) {
|
|
|
|
iterator.remove();
|
|
|
|
iterator.remove();
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件损坏");
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + file + "文件损坏");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
iterator.remove();
|
|
|
|
iterator.remove();
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件不存在");
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + file + "文件不存在");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!filePaths.isEmpty()){
|
|
|
|
if (!filePaths.isEmpty()) {
|
|
|
|
// pdf合并工具类
|
|
|
|
// pdf合并工具类
|
|
|
|
Document document = null;
|
|
|
|
Document document = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
@ -313,14 +315,14 @@ public class Jpg2PdfUtil {
|
|
|
|
response.setContentType("application/pdf");
|
|
|
|
response.setContentType("application/pdf");
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
|
|
|
|
document = new Document(new PdfReader(filePaths.get(0)).getPageSize(1));
|
|
|
|
document = new Document(new PdfReader(filePaths.get(0)).getPageSize(1));
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
document.open();
|
|
|
|
document.open();
|
|
|
|
for (String file : filePaths) {
|
|
|
|
for (String file : filePaths) {
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
//判断是否加水印
|
|
|
|
//判断是否加水印
|
|
|
|
if (emrPdfWaterSet.getDownloadEffective()==1) {
|
|
|
|
if (emrPdfWaterSet.getDownloadEffective() == 1) {
|
|
|
|
setWatermark(bos, reader, emrPdfWaterSet.getText(), null);
|
|
|
|
setWatermark(bos, reader, emrPdfWaterSet.getText(), null);
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -328,28 +330,28 @@ public class Jpg2PdfUtil {
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
document.newPage();
|
|
|
|
document.newPage();
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
if (emrPdfWaterSet.getDownloadEffective()==1) {
|
|
|
|
if (emrPdfWaterSet.getDownloadEffective() == 1) {
|
|
|
|
page = copy.getImportedPage(pdfReader, j);
|
|
|
|
page = copy.getImportedPage(pdfReader, j);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
copy.addPage(page);
|
|
|
|
copy.addPage(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(null != pdfReader){
|
|
|
|
if (null != pdfReader) {
|
|
|
|
pdfReader.close();
|
|
|
|
pdfReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
reader.close();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
bos.flush();
|
|
|
|
bos.flush();
|
|
|
|
bos.close();
|
|
|
|
bos.close();
|
|
|
|
}catch (Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
} finally {
|
|
|
|
if(null != document){
|
|
|
|
if (null != document) {
|
|
|
|
document.close();
|
|
|
|
document.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -357,6 +359,54 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void mulFile2One4(HttpServletResponse response, List<String> filePath, String filename) {
|
|
|
|
|
|
|
|
// pdf合并工具类
|
|
|
|
|
|
|
|
Document document = null;
|
|
|
|
|
|
|
|
PdfCopy copy = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
filename = java.net.URLEncoder.encode(filename, "UTF-8");
|
|
|
|
|
|
|
|
response.reset();
|
|
|
|
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
|
|
|
|
response.setContentType("application/pdf");
|
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
|
|
|
|
|
|
|
|
document = new Document(new PdfReader(filePath.get(0)).getPageSize(1));
|
|
|
|
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
|
|
|
|
document.open();
|
|
|
|
|
|
|
|
for (String file : filePath) {
|
|
|
|
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
|
|
|
//判断是否加水印
|
|
|
|
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
|
|
|
document.newPage();
|
|
|
|
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null != pdfReader) {
|
|
|
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
bos.flush();
|
|
|
|
|
|
|
|
bos.close();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
if (null != document) {
|
|
|
|
|
|
|
|
document.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void mulFile2One2(HttpServletResponse response, List<String> filePaths, String filename, EmrPdfWaterSet emrPdfWaterSet) {
|
|
|
|
public static void mulFile2One2(HttpServletResponse response, List<String> filePaths, String filename, EmrPdfWaterSet emrPdfWaterSet) {
|
|
|
|
//是否启用水印
|
|
|
|
//是否启用水印
|
|
|
|
Short effective = emrPdfWaterSet.getEffective();
|
|
|
|
Short effective = emrPdfWaterSet.getEffective();
|
|
|
|
@ -367,18 +417,18 @@ public class Jpg2PdfUtil {
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
File file = new File(fileStr);
|
|
|
|
File file = new File(fileStr);
|
|
|
|
if(file.isFile()){
|
|
|
|
if (file.isFile()) {
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
if(!flag){
|
|
|
|
if (!flag) {
|
|
|
|
iterator.remove();
|
|
|
|
iterator.remove();
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件损坏");
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + file + "文件损坏");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
iterator.remove();
|
|
|
|
iterator.remove();
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件不存在");
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + file + "文件不存在");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!filePaths.isEmpty()){
|
|
|
|
if (!filePaths.isEmpty()) {
|
|
|
|
// pdf合并工具类
|
|
|
|
// pdf合并工具类
|
|
|
|
Document document = null;
|
|
|
|
Document document = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
@ -388,7 +438,7 @@ public class Jpg2PdfUtil {
|
|
|
|
response.setContentType("application/pdf");
|
|
|
|
response.setContentType("application/pdf");
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
|
|
|
|
document = new Document(new PdfReader(filePaths.get(0)).getPageSize(1));
|
|
|
|
document = new Document(new PdfReader(filePaths.get(0)).getPageSize(1));
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
document.open();
|
|
|
|
document.open();
|
|
|
|
for (String file : filePaths) {
|
|
|
|
for (String file : filePaths) {
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
@ -410,21 +460,21 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
copy.addPage(page);
|
|
|
|
copy.addPage(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(null != pdfReader){
|
|
|
|
if (null != pdfReader) {
|
|
|
|
pdfReader.close();
|
|
|
|
pdfReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
reader.close();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
bos.flush();
|
|
|
|
bos.flush();
|
|
|
|
bos.close();
|
|
|
|
bos.close();
|
|
|
|
}catch (Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
} finally {
|
|
|
|
if(null != document){
|
|
|
|
if (null != document) {
|
|
|
|
document.close();
|
|
|
|
document.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -435,6 +485,7 @@ public class Jpg2PdfUtil {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
|
|
|
|
* 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
|
|
|
|
|
|
|
|
*
|
|
|
|
* @param h
|
|
|
|
* @param h
|
|
|
|
* @param w
|
|
|
|
* @param w
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
@ -452,48 +503,48 @@ public class Jpg2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void mulFile2OneOne(HttpServletResponse response,String filePaths,String waterMarkName) {
|
|
|
|
public static void mulFile2OneOne(HttpServletResponse response, String filePaths, String waterMarkName) {
|
|
|
|
if(!filePaths.isEmpty()){
|
|
|
|
if (!filePaths.isEmpty()) {
|
|
|
|
// pdf合并工具类
|
|
|
|
// pdf合并工具类
|
|
|
|
Document document = null;
|
|
|
|
Document document = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
PdfCopy copy = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
document = new Document(new PdfReader(filePaths).getPageSize(1));
|
|
|
|
document = new Document(new PdfReader(filePaths).getPageSize(1));
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
document.open();
|
|
|
|
document.open();
|
|
|
|
PdfReader reader = new PdfReader(filePaths);
|
|
|
|
PdfReader reader = new PdfReader(filePaths);
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
//判断是否加水印
|
|
|
|
//判断是否加水印
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
|
|
|
setWatermark(bos, reader, waterMarkName, null);
|
|
|
|
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
|
|
|
document.newPage();
|
|
|
|
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
setWatermark(bos, reader, waterMarkName, null);
|
|
|
|
page = copy.getImportedPage(pdfReader, j);
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
|
|
|
document.newPage();
|
|
|
|
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
|
|
|
page = copy.getImportedPage(pdfReader, j);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(null != pdfReader){
|
|
|
|
copy.addPage(page);
|
|
|
|
pdfReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (null != pdfReader) {
|
|
|
|
reader.close();
|
|
|
|
pdfReader.close();
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
bos.flush();
|
|
|
|
reader.close();
|
|
|
|
bos.close();
|
|
|
|
try {
|
|
|
|
}catch (Exception e){
|
|
|
|
bos.flush();
|
|
|
|
//e.printStackTrace();
|
|
|
|
bos.close();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
}catch (Exception e){
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
//e.printStackTrace();
|
|
|
|
//e.printStackTrace();
|
|
|
|
}finally {
|
|
|
|
} finally {
|
|
|
|
if(null != document){
|
|
|
|
if (null != document) {
|
|
|
|
document.close();
|
|
|
|
document.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|