|
|
|
|
@ -30,95 +30,6 @@ import java.util.List;
|
|
|
|
|
* @Version: 1.0
|
|
|
|
|
*/
|
|
|
|
|
public class Jpg2PdfUtil {
|
|
|
|
|
static Logger logger = LoggerFactory.getLogger(Jpg2PdfUtil.class);
|
|
|
|
|
public static void imageToPdf(HttpServletResponse response, List<String> filePaths, String pdfName) throws Exception{
|
|
|
|
|
// 实例化图牿
|
|
|
|
|
Image image = null;
|
|
|
|
|
pdfName = java.net.URLEncoder.encode(pdfName, "UTF-8");
|
|
|
|
|
Document doc = openDocument(response, pdfName);
|
|
|
|
|
try {
|
|
|
|
|
for (String filePath : filePaths) {
|
|
|
|
|
File file1 = new File(filePath);
|
|
|
|
|
if(file1.exists()){
|
|
|
|
|
if (file1.getName().endsWith(".tif") || file1.getName().endsWith(".tiff")) {
|
|
|
|
|
Object localObject1 = null;
|
|
|
|
|
Object localObject2 = null;
|
|
|
|
|
URL paramURL = Utilities.toURL(filePath);
|
|
|
|
|
try {
|
|
|
|
|
if (paramURL.getProtocol().equals("file")) {
|
|
|
|
|
localObject2 = paramURL.getFile();
|
|
|
|
|
localObject2 = Utilities
|
|
|
|
|
.unEscapeURL((String) localObject2);
|
|
|
|
|
localObject1 = new RandomAccessFileOrArray(
|
|
|
|
|
(String) localObject2);
|
|
|
|
|
} else {
|
|
|
|
|
localObject1 = new RandomAccessFileOrArray(paramURL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pageNums = TiffImage
|
|
|
|
|
.getNumberOfPages((RandomAccessFileOrArray) localObject1);
|
|
|
|
|
if (pageNums > 0) {
|
|
|
|
|
for (int i = 1; i <= pageNums; i++) {
|
|
|
|
|
localObject2 = TiffImage.getTiffImage(
|
|
|
|
|
(RandomAccessFileOrArray) localObject1, i);
|
|
|
|
|
image = (Image) localObject2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (localObject1 != null) {
|
|
|
|
|
((RandomAccessFileOrArray) localObject1).close();
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (localObject1 != null) {
|
|
|
|
|
((RandomAccessFileOrArray) localObject1).close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else if (file1.getName().endsWith(".png")
|
|
|
|
|
|| file1.getName().endsWith(".jpg")
|
|
|
|
|
|| file1.getName().endsWith(".gif")
|
|
|
|
|
|| file1.getName().endsWith(".jpeg")
|
|
|
|
|
) {
|
|
|
|
|
// 实例化图牿
|
|
|
|
|
image = Image.getInstance(filePath);
|
|
|
|
|
}
|
|
|
|
|
// 获得图片的高庿
|
|
|
|
|
float heigth = image.getHeight();
|
|
|
|
|
float width = image.getWidth();
|
|
|
|
|
// 合理压缩,h>w,按w压缩,否则按w压缩
|
|
|
|
|
int percent = getPercent(heigth, width);
|
|
|
|
|
|
|
|
|
|
// 设置图片居中显示
|
|
|
|
|
//image.setAlignment(Image.MIDDLE);
|
|
|
|
|
image.setAlignment(Image.ALIGN_CENTER);
|
|
|
|
|
//image.scaleAbsolute(500, 400);
|
|
|
|
|
// 按百分比显示图片的比便
|
|
|
|
|
if (width > 1024 || heigth > 786) {
|
|
|
|
|
image.scalePercent(percent+5);
|
|
|
|
|
}
|
|
|
|
|
doc.add(image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
if (doc.isOpen()) {
|
|
|
|
|
doc.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Document openDocument(HttpServletResponse response,String pdfName) throws Exception{
|
|
|
|
|
Document doc = new Document();
|
|
|
|
|
response.reset();
|
|
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
|
|
response.setContentType("application/pdf"); // word格式
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + pdfName+ "("+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +").pdf");
|
|
|
|
|
// 定义输出文件的位罿
|
|
|
|
|
PdfWriter.getInstance(doc,response.getOutputStream());
|
|
|
|
|
// 开启文桿
|
|
|
|
|
doc.open();
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 利用itext打开pdf文档
|
|
|
|
|
*/
|
|
|
|
|
@ -162,25 +73,23 @@ public class Jpg2PdfUtil {
|
|
|
|
|
if(!files.isEmpty()){
|
|
|
|
|
// pdf合并工具类
|
|
|
|
|
Document document = null;
|
|
|
|
|
ByteArrayOutputStream bos = null;
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
PdfCopy copy = null;
|
|
|
|
|
PdfReader reader = null;
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
try {
|
|
|
|
|
response.reset();
|
|
|
|
|
document = new Document(new PdfReader(files.get(0)).getPageSize(1));
|
|
|
|
|
copy = new PdfCopy(document,response.getOutputStream());
|
|
|
|
|
document.open();
|
|
|
|
|
bos = new ByteArrayOutputStream();
|
|
|
|
|
for (String file : files) {
|
|
|
|
|
bos.flush();
|
|
|
|
|
PdfReader reader = new PdfReader(file);
|
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
|
//判断是否加水印
|
|
|
|
|
if (StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
setWatermark(bos, file, waterMarkName, null);
|
|
|
|
|
setWatermark(bos, reader, waterMarkName, null);
|
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
|
}
|
|
|
|
|
reader = new PdfReader(file);
|
|
|
|
|
int n = reader.getNumberOfPages();
|
|
|
|
|
pdfReader = new PdfReader(bos.toByteArray());
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
document.newPage();
|
|
|
|
|
PdfImportedPage page = null;
|
|
|
|
|
@ -191,6 +100,10 @@ public class Jpg2PdfUtil {
|
|
|
|
|
}
|
|
|
|
|
copy.addPage(page);
|
|
|
|
|
}
|
|
|
|
|
if(pdfReader != null){
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
@ -198,19 +111,11 @@ public class Jpg2PdfUtil {
|
|
|
|
|
if(null != copy){
|
|
|
|
|
copy.close();
|
|
|
|
|
}
|
|
|
|
|
if(pdfReader != null){
|
|
|
|
|
pdfReader.close();
|
|
|
|
|
}
|
|
|
|
|
if(null != reader){
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
if(null != bos){
|
|
|
|
|
try {
|
|
|
|
|
bos.flush();
|
|
|
|
|
bos.close();
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
bos.flush();
|
|
|
|
|
bos.close();
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if(null != document){
|
|
|
|
|
document.close();
|
|
|
|
|
@ -221,11 +126,9 @@ public class Jpg2PdfUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//加水印
|
|
|
|
|
private static void setWatermark(ByteArrayOutputStream bos, String input, String waterMarkName, String imgPath){
|
|
|
|
|
PdfReader reader = null;
|
|
|
|
|
public static void setWatermark(ByteArrayOutputStream bos, PdfReader reader, String waterMarkName, String imgPath){
|
|
|
|
|
PdfStamper stamper = null;
|
|
|
|
|
try {
|
|
|
|
|
reader = new PdfReader(input);
|
|
|
|
|
stamper = new PdfStamper(reader, bos);
|
|
|
|
|
int total = reader.getNumberOfPages() + 1;
|
|
|
|
|
PdfContentByte content;
|
|
|
|
|
@ -235,7 +138,6 @@ public class Jpg2PdfUtil {
|
|
|
|
|
content = stamper.getOverContent(i);// 在内容上方加水印
|
|
|
|
|
//加文字水印
|
|
|
|
|
if(StringUtils.isNotBlank(waterMarkName)) {
|
|
|
|
|
|
|
|
|
|
gs.setFillOpacity(0.3f);
|
|
|
|
|
gs.setStrokeOpacity(0.3f);
|
|
|
|
|
content.setGState(gs);
|
|
|
|
|
@ -269,29 +171,9 @@ public class Jpg2PdfUtil {
|
|
|
|
|
try {
|
|
|
|
|
assert stamper != null;
|
|
|
|
|
stamper.close();
|
|
|
|
|
reader.close();
|
|
|
|
|
} catch (DocumentException | IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 在不改变图片形状的同时,判断,如果h>w,则按h压缩,否则在w>h或w=h的情况下,按宽度压缩
|
|
|
|
|
* @param h
|
|
|
|
|
* @param w
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static int getPercent(float h, float w) {
|
|
|
|
|
int p = 0;
|
|
|
|
|
float p2 = 0.0f;
|
|
|
|
|
if (h > w) {
|
|
|
|
|
p2 = 210 / h * 279;
|
|
|
|
|
} else {
|
|
|
|
|
p2 = 210 / w * 279;
|
|
|
|
|
}
|
|
|
|
|
p = Math.round(p2);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|