bug:pdf打印空白问题

master
jian.wang 2 years ago
parent e6935db4b3
commit 393397c566

@ -617,12 +617,6 @@ public class CommomService {
Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER"); Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
List<String> outs = new LinkedList<>(); List<String> outs = new LinkedList<>();
if (StringUtils.isNotBlank(names)) { if (StringUtils.isNotBlank(names)) {
if (new File(WATERPICPATH).isDirectory()) {
FileUtils.deleteDirectory(new File(WATERPICPATH));
}
if (!new File(WATERTIFTOJPGPATH).isDirectory()) {
FileUtils.deleteDirectory(new File(WATERTIFTOJPGPATH));
}
String[] rootPathList = rootPaths.split(","); String[] rootPathList = rootPaths.split(",");
String[] nameList = names.split(","); String[] nameList = names.split(",");
String[] sourceList = sources.split(","); String[] sourceList = sources.split(",");

@ -467,6 +467,95 @@ public class img2PdfUtil {
String imgFile, int imgWidth, int imgHeight, int imgX, int imgY) { String imgFile, int imgWidth, int imgHeight, int imgX, int imgY) {
PdfReader reader = null; PdfReader reader = null;
PdfStamper stamper = null; PdfStamper stamper = null;
try {
reader = new PdfReader(bos.toByteArray());
bos.reset();
// 加完水印的文件
stamper = getStamper(reader, bos, response);
// 设置字体
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 循环对每页插入水印
PdfContentByte content;
PdfGState gs = new PdfGState();
// PDF总页数
int total = reader.getNumberOfPages();
for (int i = 1; i <= total; i++) {
//upOrUnder = 1为在文本之上
content = getWatermarkContent(upOrUnder, stamper, i);
gs.setFillOpacity(transparent);
content.setGState(gs);
addImageWatermarkIfNecessary(content, isImg, imgFile, imgWidth, imgHeight, imgX, imgY);//图片水印
addTextWatermark(content, effective, font, text, textColor, textSize, textRotation, textX, textY);//文字水印
}
} catch (IOException | DocumentException e) {
logError(e);
} finally {
closeResources(stamper, reader);
}
}
private static PdfStamper getStamper(PdfReader reader, ByteArrayOutputStream bos, HttpServletResponse response) throws IOException, DocumentException {
return response != null ? new PdfStamper(reader, response.getOutputStream()) : new PdfStamper(reader, bos);
}
private static PdfContentByte getWatermarkContent(int upOrUnder, PdfStamper stamper, int pageIndex) {
return upOrUnder == 1 ? stamper.getOverContent(pageIndex) : stamper.getUnderContent(pageIndex);
}
private static void addImageWatermarkIfNecessary(PdfContentByte content, Short isImg, String imgFile, int imgWidth, int imgHeight, int imgX, int imgY) throws DocumentException, IOException {
if (isImg != null && isImg == 1) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String imgStr = getValidImagePath(request, imgFile);
if (StringUtils.isNotBlank(imgStr) && new File(imgStr).isFile()) {
com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(imgStr);
image.setAbsolutePosition(imgX, imgY);
image.scaleToFit(imgWidth, imgHeight);
content.addImage(image);
}
}
}
private static String getValidImagePath(HttpServletRequest request, String imgFile) {
String tomcatPath = request.getSession().getServletContext().getRealPath("/");
return tomcatPath + "static/pdfWaterSet/upload/" + imgFile;
}
private static void addTextWatermark(PdfContentByte content, Short effective , BaseFont font, String text, String textColor, int textSize, int textRotation, int textX, int textY) {
if (effective == 1 && StringUtils.isNotBlank(text)) {
Color color = toColorFromString(textColor);
content.beginText();
content.setColorFill(color);
content.setFontAndSize(font, textSize);
content.setTextMatrix(textX, textY);
content.showTextAligned(Element.ALIGN_LEFT, text, textX, textY, textRotation);
content.endText();
}
}
private static void logError(Exception e) {
// 记录异常信息,例如日志记录
e.printStackTrace();
}
private static void closeResources(PdfStamper stamper, PdfReader reader) {
if (stamper != null) {
try {
stamper.close();
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
if (reader != null) {
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) {
PdfReader reader = null;
PdfStamper stamper = null;
try { try {
reader = new PdfReader(bos.toByteArray()); reader = new PdfReader(bos.toByteArray());
bos.reset(); // 重置 bos以便 PdfStamper 可以写入 bos.reset(); // 重置 bos以便 PdfStamper 可以写入
@ -554,7 +643,7 @@ public class img2PdfUtil {
reader.close(); reader.close();
} }
} }
} }*/
public static void addWaterMark(ByteArrayOutputStream bos, HttpServletResponse response, int upOrUnder, float transparent, String text, int textX, int textY, 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 textColor, int textSize, int textRotation, Short effective, Short isImg,
String imgFile, int imgWidth, int imgHeight, int imgX, int imgY,String text1) { String imgFile, int imgWidth, int imgHeight, int imgX, int imgY,String text1) {

Loading…
Cancel
Save