|
|
|
@ -8,11 +8,16 @@ import com.itextpdf.text.pdf.PdfWriter;
|
|
|
|
import com.lowagie.text.DocumentException;
|
|
|
|
import com.lowagie.text.DocumentException;
|
|
|
|
import com.lowagie.text.Element;
|
|
|
|
import com.lowagie.text.Element;
|
|
|
|
import com.lowagie.text.pdf.*;
|
|
|
|
import com.lowagie.text.pdf.*;
|
|
|
|
|
|
|
|
import com.sun.media.jai.codec.ImageCodec;
|
|
|
|
|
|
|
|
import com.sun.media.jai.codec.ImageEncoder;
|
|
|
|
|
|
|
|
import com.sun.media.jai.codec.JPEGEncodeParam;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
import javax.media.jai.JAI;
|
|
|
|
|
|
|
|
import javax.media.jai.RenderedOp;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.*;
|
|
|
|
@ -33,10 +38,10 @@ import java.util.List;
|
|
|
|
* @Version: 1.0
|
|
|
|
* @Version: 1.0
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class img2PdfUtil {
|
|
|
|
public class img2PdfUtil {
|
|
|
|
final private static float A4_weight = 595-60; //标准A4的宽
|
|
|
|
final private static float A4_weight = 595; //标准A4的宽
|
|
|
|
final private static float A4_height = 842-60; //标准A4的高
|
|
|
|
final private static float A4_height = 842; //标准A4的高
|
|
|
|
public static void imageToPdf(HttpServletResponse response, List<String> filePaths, String pdfName, EmrPdfWaterSet pdfWaterSet){
|
|
|
|
public static void imageToPdf(HttpServletResponse response, List<String> filePaths, String pdfName, EmrPdfWaterSet pdfWaterSet){
|
|
|
|
Document document = new Document(PageSize.A4,30,30,30,30); //创建文档容器
|
|
|
|
Document document = new Document(PageSize.A4,0,0,0,0); //创建文档容器
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
//是否启用水印
|
|
|
|
//是否启用水印
|
|
|
|
Short effective = pdfWaterSet.getEffective();
|
|
|
|
Short effective = pdfWaterSet.getEffective();
|
|
|
|
@ -147,7 +152,8 @@ public class img2PdfUtil {
|
|
|
|
BufferedImage bufImg = null;
|
|
|
|
BufferedImage bufImg = null;
|
|
|
|
Graphics2D g = null;
|
|
|
|
Graphics2D g = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
srcImg = ImageIO.read(srcImgFile);//文件转化为图片
|
|
|
|
//srcImg = ImageIO.read(srcImgFile);//文件转化为图片
|
|
|
|
|
|
|
|
srcImg = ImageIO.read(new FileInputStream(srcImgFile));
|
|
|
|
//System.out.println("读取图片"+(end-start)/1000.0+"s");
|
|
|
|
//System.out.println("读取图片"+(end-start)/1000.0+"s");
|
|
|
|
int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
|
|
|
|
int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
|
|
|
|
int srcImgHeight = srcImg.getHeight(null);//获取图片的高
|
|
|
|
int srcImgHeight = srcImg.getHeight(null);//获取图片的高
|
|
|
|
@ -218,7 +224,6 @@ public class img2PdfUtil {
|
|
|
|
} else if ((w < A4_weight) && (h > A4_height)) {
|
|
|
|
} else if ((w < A4_weight) && (h > A4_height)) {
|
|
|
|
percent = (A4_height * 100) / h;
|
|
|
|
percent = (A4_height * 100) / h;
|
|
|
|
} else if ((w > A4_weight) && (h > A4_height)) {
|
|
|
|
} else if ((w > A4_weight) && (h > A4_height)) {
|
|
|
|
|
|
|
|
|
|
|
|
percent = (A4_weight * 100) / w;
|
|
|
|
percent = (A4_weight * 100) / w;
|
|
|
|
h = (h * percent) / 100;
|
|
|
|
h = (h * percent) / 100;
|
|
|
|
if (h > A4_height) {
|
|
|
|
if (h > A4_height) {
|
|
|
|
@ -226,7 +231,11 @@ public class img2PdfUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
img.setAlignment(com.lowagie.text.Image.ALIGN_CENTER);
|
|
|
|
img.setAlignment(com.lowagie.text.Image.ALIGN_CENTER);
|
|
|
|
img.scalePercent(percent);
|
|
|
|
if(percent != 100){
|
|
|
|
|
|
|
|
img.scaleAbsolute(A4_weight,A4_height);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
img.scalePercent(percent);
|
|
|
|
|
|
|
|
}
|
|
|
|
document.add(img);
|
|
|
|
document.add(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -442,4 +451,39 @@ public class img2PdfUtil {
|
|
|
|
//java.awt.Color[r=0,g=0,b=255]
|
|
|
|
//java.awt.Color[r=0,g=0,b=255]
|
|
|
|
return new Color(r,g,b);
|
|
|
|
return new Color(r,g,b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 将 tiff 转换 jpg 格式
|
|
|
|
|
|
|
|
* @param filePath
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String tiffTuanJPG(String filePath){
|
|
|
|
|
|
|
|
String format = filePath.substring(filePath.lastIndexOf(".")+1);
|
|
|
|
|
|
|
|
String turnJpgFile = filePath.replace("tiff", "jpg");
|
|
|
|
|
|
|
|
if(format.equals("tiff")){
|
|
|
|
|
|
|
|
File fileTiff = new File(turnJpgFile);
|
|
|
|
|
|
|
|
if(fileTiff.exists()){
|
|
|
|
|
|
|
|
System.out.println("该tiff文件已经转换为 JPG 文件:"+turnJpgFile);
|
|
|
|
|
|
|
|
return turnJpgFile;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderedOp rd = JAI.create("fileload", filePath);//读取iff文件
|
|
|
|
|
|
|
|
OutputStream ops = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
ops = new FileOutputStream(turnJpgFile);
|
|
|
|
|
|
|
|
//文件存储输出流
|
|
|
|
|
|
|
|
JPEGEncodeParam param = new JPEGEncodeParam();
|
|
|
|
|
|
|
|
ImageEncoder image = ImageCodec.createImageEncoder("jpg", ops, param); //指定输出格式
|
|
|
|
|
|
|
|
image.encode(rd );
|
|
|
|
|
|
|
|
//解析输出流进行输出
|
|
|
|
|
|
|
|
ops.close();
|
|
|
|
|
|
|
|
System.out.println("tiff转换jpg成功:"+filePath);
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return turnJpgFile;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|