|
|
|
|
@ -37,7 +37,7 @@ public class img2PdfUtil {
|
|
|
|
|
final private static float A4_weight = 595; //标准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,EmrPdfWaterSet pdfWaterSet1) {
|
|
|
|
|
Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
//是否启用水印
|
|
|
|
|
@ -61,7 +61,7 @@ public class img2PdfUtil {
|
|
|
|
|
pdfWaterSet.getText(), pdfWaterSet.getTextX(), pdfWaterSet.getTextY(),
|
|
|
|
|
pdfWaterSet.getTextColor(), pdfWaterSet.getTextSize(), pdfWaterSet.getTextRotation(),
|
|
|
|
|
pdfWaterSet.getEffective(), pdfWaterSet.getIsImg(), pdfWaterSet.getImgFile(), pdfWaterSet.getImgWidth(),
|
|
|
|
|
pdfWaterSet.getImgHeight(), pdfWaterSet.getImgX(), pdfWaterSet.getImgY());
|
|
|
|
|
pdfWaterSet.getImgHeight(), pdfWaterSet.getImgX(), pdfWaterSet.getImgY(),pdfWaterSet1.getText());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ExceptionPrintUtil.printException(e);
|
|
|
|
|
@ -184,7 +184,7 @@ public class img2PdfUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String addWatermarkPic1(File srcImgFile, EmrPdfWaterSet pdfWaterSet, String outPath) {
|
|
|
|
|
public static String addWatermarkPic1(File srcImgFile, EmrPdfWaterSet pdfWaterSet, String outPath,EmrPdfWaterSet pdfWaterSet1) {
|
|
|
|
|
java.awt.Image srcImg = null;
|
|
|
|
|
BufferedImage bufImg = null;
|
|
|
|
|
Graphics2D g = null;
|
|
|
|
|
@ -216,7 +216,8 @@ public class img2PdfUtil {
|
|
|
|
|
g.setFont(font); //设置字体
|
|
|
|
|
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));//设置水印文字透明度
|
|
|
|
|
g.rotate(Math.toRadians(degree), (double) bufImg.getWidth(), (double) bufImg.getHeight());//设置水印旋转
|
|
|
|
|
g.drawString(pdfWaterSet.getText(), srcImg.getWidth(null) / 2 + pdfWaterSet.getTextX(), srcImg.getHeight(null) / 2 + (pdfWaterSet.getTextY() - 200));//画出水印,并设置水印位置
|
|
|
|
|
g.drawString(pdfWaterSet.getText(), srcImg.getWidth(null) / 2 + pdfWaterSet.getTextX() , srcImg.getHeight(null) / 2 + (pdfWaterSet.getTextY() - 300));//画出水印,并设置水印位置
|
|
|
|
|
g.drawString(pdfWaterSet1.getText(), srcImg.getWidth(null) / 2 + pdfWaterSet.getTextX() , srcImg.getHeight(null) / 2 + (pdfWaterSet.getTextY() -100));//画出水印,并设置水印位置
|
|
|
|
|
//System.out.println("画文字图片"+(end2-start2)/1000.0+"s");
|
|
|
|
|
}
|
|
|
|
|
//ByteArrayInputStream fis = new ByteArrayInputStream(imageToByteArr);//创建输入流对象
|
|
|
|
|
@ -367,12 +368,135 @@ public class img2PdfUtil {
|
|
|
|
|
// 中间水印
|
|
|
|
|
content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
|
|
|
|
|
// 底部水印
|
|
|
|
|
/*for (int k = 0; k < text.length(); k++) {
|
|
|
|
|
// 距离底边的距离
|
|
|
|
|
content.setTextRise(10);
|
|
|
|
|
// 将char转成字符串
|
|
|
|
|
content.showText(String.valueOf(text.charAt(k)));
|
|
|
|
|
}*/
|
|
|
|
|
content.endText();
|
|
|
|
|
//第二水印
|
|
|
|
|
content.beginText();
|
|
|
|
|
//16进制颜色转color
|
|
|
|
|
content.setColorFill(color);
|
|
|
|
|
// 设置字体及字号
|
|
|
|
|
content.setFontAndSize(font, textSize);
|
|
|
|
|
// 设置起始位置
|
|
|
|
|
content.setTextMatrix(textX, textY);
|
|
|
|
|
// 中间水印
|
|
|
|
|
content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
|
|
|
|
|
// 底部水印
|
|
|
|
|
content.endText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException | DocumentException e) {
|
|
|
|
|
ExceptionPrintUtil.printException(e);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
if (null != stamper) {
|
|
|
|
|
try {
|
|
|
|
|
stamper.close();
|
|
|
|
|
} catch (DocumentException | IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (null != reader) {
|
|
|
|
|
reader.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void addWaterMark(ByteArrayOutputStream bos, HttpServletResponse response, int upOrUnder, float transparent, String text, int textX, int textY,
|
|
|
|
|
String textColor, int textSize, int textRotation, Short effective, Short isImg,
|
|
|
|
|
String imgFile, int imgWidth, int imgHeight, int imgX, int imgY,String text1) {
|
|
|
|
|
PdfReader reader = null;
|
|
|
|
|
PdfStamper stamper = null;
|
|
|
|
|
try {
|
|
|
|
|
reader = new PdfReader(bos.toByteArray());
|
|
|
|
|
// 加完水印的文件
|
|
|
|
|
if (null != response) {
|
|
|
|
|
stamper = new PdfStamper(reader, response.getOutputStream());
|
|
|
|
|
} else {
|
|
|
|
|
stamper = new PdfStamper(reader, bos);
|
|
|
|
|
}
|
|
|
|
|
// 设置字体
|
|
|
|
|
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
|
|
|
|
|
// PDF总页数
|
|
|
|
|
int total = reader.getNumberOfPages() + 1;
|
|
|
|
|
// 循环对每页插入水印
|
|
|
|
|
PdfContentByte content;
|
|
|
|
|
PdfGState gs = new PdfGState();
|
|
|
|
|
for (int i = 1; i < total; i++) {
|
|
|
|
|
//upOrUnder = 1为在文本之上
|
|
|
|
|
if (upOrUnder == 1) {
|
|
|
|
|
// 水印在之前文本之上
|
|
|
|
|
content = stamper.getOverContent(i);
|
|
|
|
|
} else {
|
|
|
|
|
// 水印在文本之下
|
|
|
|
|
content = stamper.getUnderContent(i);
|
|
|
|
|
}
|
|
|
|
|
//设置填充字体不透明度为0.2f
|
|
|
|
|
gs.setFillOpacity(transparent);
|
|
|
|
|
content.setGState(gs);
|
|
|
|
|
// 图片水印
|
|
|
|
|
if (null != isImg && isImg == 1) {
|
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
|
|
String tomcatPath = request.getSession().getServletContext().getRealPath("/");
|
|
|
|
|
String imgStr = tomcatPath + "static\\pdfWaterSet\\upload\\" + imgFile;
|
|
|
|
|
com.lowagie.text.Image image = null;
|
|
|
|
|
if (StringUtils.isNotBlank(imgStr) && new File(imgStr).isFile()) {
|
|
|
|
|
image = com.lowagie.text.Image.getInstance(imgStr);
|
|
|
|
|
//位置
|
|
|
|
|
image.setAbsolutePosition(imgX, imgY);
|
|
|
|
|
// 设置图片的显示大小
|
|
|
|
|
image.scaleToFit(imgWidth, imgHeight);
|
|
|
|
|
content.addImage(image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 文字水印
|
|
|
|
|
if (effective == 1 && StringUtils.isNotBlank(text)) {
|
|
|
|
|
content.beginText();
|
|
|
|
|
//16进制颜色转color
|
|
|
|
|
Color color = toColorFromString(textColor);
|
|
|
|
|
content.setColorFill(color);
|
|
|
|
|
// 设置字体及字号
|
|
|
|
|
content.setFontAndSize(font, textSize);
|
|
|
|
|
// 设置起始位置
|
|
|
|
|
content.setTextMatrix(textX, textY);
|
|
|
|
|
// 中间水印
|
|
|
|
|
content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
|
|
|
|
|
// 底部水印
|
|
|
|
|
content.endText();
|
|
|
|
|
//第二水印
|
|
|
|
|
content.beginText();
|
|
|
|
|
//16进制颜色转color
|
|
|
|
|
content.setColorFill(color);
|
|
|
|
|
// 设置字体及字号
|
|
|
|
|
content.setFontAndSize(font, textSize);
|
|
|
|
|
// 设置起始位置
|
|
|
|
|
content.setTextMatrix(textX, textY);
|
|
|
|
|
// 中间水印
|
|
|
|
|
content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
|
|
|
|
|
// 底部水印
|
|
|
|
|
content.endText();
|
|
|
|
|
}
|
|
|
|
|
if(StringUtils.isNotBlank(text1)){
|
|
|
|
|
content.beginText();
|
|
|
|
|
//16进制颜色转color
|
|
|
|
|
Color color = toColorFromString(textColor);
|
|
|
|
|
content.setColorFill(color);
|
|
|
|
|
// 设置字体及字号
|
|
|
|
|
content.setFontAndSize(font, textSize);
|
|
|
|
|
// 设置起始位置
|
|
|
|
|
content.setTextMatrix(textX, textY);
|
|
|
|
|
// 中间水印
|
|
|
|
|
content.showTextAligned(Element.ALIGN_LEFT, text1, textX, textY - 100, textRotation);
|
|
|
|
|
// 底部水印
|
|
|
|
|
content.endText();
|
|
|
|
|
//第二水印
|
|
|
|
|
content.beginText();
|
|
|
|
|
//16进制颜色转color
|
|
|
|
|
content.setColorFill(color);
|
|
|
|
|
// 设置字体及字号
|
|
|
|
|
content.setFontAndSize(font, textSize);
|
|
|
|
|
// 设置起始位置
|
|
|
|
|
content.setTextMatrix(textX, textY);
|
|
|
|
|
// 中间水印
|
|
|
|
|
content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
|
|
|
|
|
// 底部水印
|
|
|
|
|
content.endText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|