|
|
|
@ -1,8 +1,6 @@
|
|
|
|
|
package com.emr.util;
|
|
|
|
|
|
|
|
|
|
import com.lowagie.text.Document;
|
|
|
|
|
import com.lowagie.text.DocumentException;
|
|
|
|
|
import com.lowagie.text.Element;
|
|
|
|
|
import com.lowagie.text.*;
|
|
|
|
|
import com.lowagie.text.Image;
|
|
|
|
|
import com.lowagie.text.pdf.*;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
@ -73,59 +71,77 @@ public class Jpg2PdfUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void mulFile2One(HttpServletResponse response, List<String> files, String waterMarkName) {
|
|
|
|
|
List<PdfReader> list = new ArrayList<>();
|
|
|
|
|
List<PdfReader> pdfReaders = new ArrayList<>();
|
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
|
Iterator<String> iterator = files.iterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
PdfReader check = check(iterator.next());
|
|
|
|
|
if(null != check){
|
|
|
|
|
list.add(check);
|
|
|
|
|
pdfReaders.add(check);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
|
|
|
// pdf合并工具类
|
|
|
|
|
Document document = null;
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
if(!CollectionUtils.isEmpty(pdfReaders)){
|
|
|
|
|
//创建文档容器
|
|
|
|
|
Document document = new Document(PageSize.A4,0,0,0,0);
|
|
|
|
|
PdfCopy copy = null;
|
|
|
|
|
ByteArrayOutputStream bos = null;
|
|
|
|
|
try {
|
|
|
|
|
response.reset();
|
|
|
|
|
document = new Document(list.get(0).getPageSize(1));
|
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
|
document.open();
|
|
|
|
|
for (PdfReader reader : list) {
|
|
|
|
|
bos.flush();
|
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
document.newPage();
|
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
} else {
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
}
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
for (PdfReader reader : pdfReaders) {
|
|
|
|
|
bos = new ByteArrayOutputStream();
|
|
|
|
|
//判断是否加水印
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
reader = new PdfReader(bos.toByteArray());
|
|
|
|
|
}
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
if (null != copy) {
|
|
|
|
|
copy.close();
|
|
|
|
|
//复制合并pdf
|
|
|
|
|
copy2Pdf(copy, reader);
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
try {
|
|
|
|
|
bos.flush();
|
|
|
|
|
bos.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if(null != copy){
|
|
|
|
|
copy.close();
|
|
|
|
|
}
|
|
|
|
|
if (null != document) {
|
|
|
|
|
if(null != document){
|
|
|
|
|
document.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 合并pdf
|
|
|
|
|
* @param copy
|
|
|
|
|
* @param reader
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws BadPdfFormatException
|
|
|
|
|
*/
|
|
|
|
|
private static void copy2Pdf(PdfCopy copy, PdfReader reader) throws IOException, BadPdfFormatException {
|
|
|
|
|
try {
|
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
PdfImportedPage page = copy.getImportedPage(reader, j);
|
|
|
|
|
if(null != page) {
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (BadPdfFormatException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
if(null != reader){
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//加水印
|
|
|
|
|
private void setWatermark(ByteArrayOutputStream bos, PdfReader reader, String waterMarkName, String imgPath) {
|
|
|
|
|