|
|
|
@ -4,11 +4,14 @@ import com.lowagie.text.*;
|
|
|
|
|
import com.lowagie.text.Image;
|
|
|
|
|
import com.lowagie.text.pdf.*;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -26,94 +29,99 @@ import java.util.List;
|
|
|
|
|
* @Version: 1.0
|
|
|
|
|
*/
|
|
|
|
|
public class Jpg2PdfUtil {
|
|
|
|
|
private static Logger log = Logger.getLogger("myMsg");
|
|
|
|
|
/**
|
|
|
|
|
* 利用itext打开pdf文档
|
|
|
|
|
*/
|
|
|
|
|
public static boolean check(String file) {
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
try {
|
|
|
|
|
File f = new File(file);
|
|
|
|
|
if(f.isFile()){
|
|
|
|
|
private PdfReader check(String file) {
|
|
|
|
|
if(StringUtils.isNotBlank(file)) {
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
try {
|
|
|
|
|
pdfReader = new PdfReader(file);
|
|
|
|
|
if (pdfReader.getNumberOfPages() != 0) {
|
|
|
|
|
return true;
|
|
|
|
|
return pdfReader;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
String logStr = "'"+ file + "'"+"文件损坏或不存在";
|
|
|
|
|
log.info(logStr);
|
|
|
|
|
System.out.println(fmt.format(new Date()) + ":" + logStr);
|
|
|
|
|
} finally {
|
|
|
|
|
if (null != pdfReader) {
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
}finally {
|
|
|
|
|
if(null != pdfReader){
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Jpg2PdfUtil(){};
|
|
|
|
|
|
|
|
|
|
public static Jpg2PdfUtil getInstance(){
|
|
|
|
|
return ContainerHolder.HOLDER.jpg2PdfUtil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void mulFile2One(HttpServletResponse response,List<String> files,String waterMarkName) {
|
|
|
|
|
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
private enum ContainerHolder{
|
|
|
|
|
HOLDER;
|
|
|
|
|
private Jpg2PdfUtil jpg2PdfUtil;
|
|
|
|
|
ContainerHolder(){
|
|
|
|
|
jpg2PdfUtil = new Jpg2PdfUtil();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void mulFile2One(HttpServletResponse response, List<String> files, String waterMarkName) {
|
|
|
|
|
List<PdfReader> list = new ArrayList<>();
|
|
|
|
|
//遍历删除,除去损坏,文件不存在,抛异常就是空白页
|
|
|
|
|
Iterator<String> iterator = files.iterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
String fileStr = iterator.next();
|
|
|
|
|
File file = new File(fileStr);
|
|
|
|
|
if(file.isFile()){
|
|
|
|
|
boolean flag = check(fileStr);
|
|
|
|
|
if(!flag){
|
|
|
|
|
iterator.remove();
|
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件损坏");
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
iterator.remove();
|
|
|
|
|
System.out.println(fmt.format(new Date())+":"+file+"文件不存在");
|
|
|
|
|
PdfReader check = check(iterator.next());
|
|
|
|
|
if(null != check){
|
|
|
|
|
list.add(check);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(!files.isEmpty()){
|
|
|
|
|
if (!CollectionUtils.isEmpty(list)) {
|
|
|
|
|
// pdf合并工具类
|
|
|
|
|
Document document = null;
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
PdfCopy copy = null;
|
|
|
|
|
try {
|
|
|
|
|
response.reset();
|
|
|
|
|
document = new Document(new PdfReader(files.get(0)).getPageSize(1));
|
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
|
document = new Document(list.get(0).getPageSize(1));
|
|
|
|
|
copy = new PdfCopy(document, response.getOutputStream());
|
|
|
|
|
document.open();
|
|
|
|
|
for (String file : files) {
|
|
|
|
|
for (PdfReader reader : list) {
|
|
|
|
|
bos.flush();
|
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
//判断是否加水印
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
setWatermark(bos, reader, waterMarkName, null);
|
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
|
reader = new PdfReader(bos.toByteArray());
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
} else {
|
|
|
|
|
page = copy.getImportedPage(reader, j);
|
|
|
|
|
}
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
}
|
|
|
|
|
if(pdfReader != null){
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
if(null != copy){
|
|
|
|
|
} finally {
|
|
|
|
|
if (null != copy) {
|
|
|
|
|
copy.close();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
bos.flush();
|
|
|
|
|
bos.close();
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
//e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if(null != document){
|
|
|
|
|
if (null != document) {
|
|
|
|
|
document.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -122,10 +130,11 @@ public class Jpg2PdfUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//加水印
|
|
|
|
|
private static void setWatermark(ByteArrayOutputStream bos, PdfReader reader, String waterMarkName, String imgPath){
|
|
|
|
|
private void setWatermark(ByteArrayOutputStream bos, PdfReader reader, String waterMarkName, String imgPath) {
|
|
|
|
|
PdfStamper stamper = null;
|
|
|
|
|
try {
|
|
|
|
|
stamper = new PdfStamper(reader, bos);
|
|
|
|
|
//stamper.setEncryption(null, null, ~(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING), PdfWriter.STANDARD_ENCRYPTION_128);
|
|
|
|
|
int total = reader.getNumberOfPages() + 1;
|
|
|
|
|
PdfContentByte content;
|
|
|
|
|
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
|
|
|
|
@ -133,7 +142,7 @@ public class Jpg2PdfUtil {
|
|
|
|
|
for (int i = 1; i < total; i++) {
|
|
|
|
|
content = stamper.getOverContent(i);// 在内容上方加水印
|
|
|
|
|
//加文字水印
|
|
|
|
|
if(StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
gs.setFillOpacity(0.3f);
|
|
|
|
|
gs.setStrokeOpacity(0.3f);
|
|
|
|
|
content.setGState(gs);
|
|
|
|
@ -152,7 +161,7 @@ public class Jpg2PdfUtil {
|
|
|
|
|
content.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 800, 1500, 40);
|
|
|
|
|
content.endText();
|
|
|
|
|
}
|
|
|
|
|
if(StringUtils.isNotBlank(imgPath)){
|
|
|
|
|
if (StringUtils.isNotBlank(imgPath)) {
|
|
|
|
|
Image image = Image.getInstance(imgPath);
|
|
|
|
|
image.setAbsolutePosition(200, 206); // set the first background
|
|
|
|
|
image.scaleToFit(200, 200);
|
|
|
|
@ -172,4 +181,4 @@ public class Jpg2PdfUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|