123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- package com.qxgmat.help;
- import com.itextpdf.text.*;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Rectangle;
- import com.itextpdf.text.pdf.*;
- import com.qxgmat.data.dao.entity.User;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import java.awt.*;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- @Service
- public class PdfHelp {
- private static final Logger logger = LoggerFactory.getLogger(PdfHelp.class);
- @Value("${upload.local_path}")
- private String localPath;
- @Value("${upload.web_url}")
- private String webUrl;
- @Value("${upload.offline_path}")
- private String offlinePath;
- @Value("${upload.offline_url}")
- private String offlineUrl;
- @Value("${upload.water}")
- private String water;
- private BaseFont font;
- private int top = 72;
- private int left = 93;
- private int size = 10;
- @Autowired
- private void getFont(@Value("${upload.font}") String path) throws IOException, DocumentException {
- FontFactory.registerDirectories();
- String sub = "";
- if (path.toLowerCase().endsWith(".ttc")){
- sub += ",1";
- }
- if (path.startsWith(".")){
-
- try {
- font = BaseFont.createFont(this.getClass().getClassLoader().getResource(path.replaceFirst(".","")).toString()+sub,BaseFont.IDENTITY_H,true);
- }catch (Exception e){
- font = BaseFont.createFont(this.getClass().getClassLoader().getResource(path).toString()+sub,BaseFont.IDENTITY_H,true);
- }
- }else{
- font = BaseFont.createFont(path+sub,BaseFont.IDENTITY_H,true);
- }
- }
- public static BufferedImage toBufferedImage(Image img)
- {
- if (img instanceof BufferedImage)
- {
- return (BufferedImage) img;
- }
-
- BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
-
- Graphics2D bGr = bimage.createGraphics();
- bGr.drawImage(img, 0, 0, null);
- bGr.dispose();
-
- return bimage;
- }
- public String[] getUserWater(User user){
-
- String mobile = user.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})","$1****$2");
- String name = (user.getRealStatus() > 0? user.getRealName().replaceAll("(.{1}).*(.{1})?","$1*$2 ") : "") + mobile;
- String qx = "千行ID "+String.format("%04d", user.getId());
- return new String[]{name, qx};
- }
- public String getOfflineUrl(String offline){
- return offline.replace(offlinePath, offlineUrl);
- }
- public String generatePdfImage(User user, String pdfUrl, boolean force) throws IOException, DocumentException {
- String pdfFile = pdfUrl.replace(webUrl, localPath);
- String dest = pdfFile.replace(".pdf", String.format("_%d.pdf", user.getId())).replace(localPath, offlinePath);
- File file = new File(dest);
- if (!force && file.exists()){
- return dest;
- }else if(file.exists()){
- file.delete();
- }
- String[] waters = getUserWater(user);
- PdfReader reader = new PdfReader(pdfFile);
-
-
-
- PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
- int pageSize = reader.getNumberOfPages();
- PdfContentByte under;
- for (int i = 1; i<=pageSize;i++){
- under = stamper.getOverContent(i);
- Rectangle r = reader.getPageSize(i);
- float width = r.getWidth();
- float height = r.getHeight();
- float[] a = new float[]{top, (height/2-top)/2-size/2 + top, height / 2-size/2, (height/2 + (height - top - height/2)/2)-size/2, height-top-size};
- for (int j = 0; j < 5; j ++){
- AddTextWatermark(under, waters[0], Element.ALIGN_LEFT, left, a[j]);
- AddTextWatermark(under, waters[1], Element.ALIGN_CENTER, width / 2,a[j]);
- AddTextWatermark(under, waters[0], Element.ALIGN_RIGHT, width - left,a[j]);
- }
- }
- stamper.close();
- reader.close();
- return dest;
- }
- void AddTextWatermark(PdfContentByte content, String textWatermark, int alignment, float width, float y){
- content.saveState();
- PdfGState gs = new PdfGState();
- gs.setFillOpacity(0.2f);
- content.setGState(gs);
- content.beginText();
- content.setColorFill(new BaseColor(0,0,0));
- content.setFontAndSize(font, size);
- content.showTextAligned(alignment, textWatermark, width, y, 0);
- content.endText();
-
- content.restoreState();
- }
- }
|