|
@@ -6,6 +6,7 @@ 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;
|
|
@@ -34,11 +35,23 @@ public class PdfHelp {
|
|
|
@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();
|
|
@@ -79,9 +92,21 @@ public class PdfHelp {
|
|
|
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()));
|
|
|
+ String dest = pdfFile.replace(".pdf", String.format("_%d.pdf", user.getId())).replace(localPath, offlinePath);
|
|
|
File file = new File(dest);
|
|
|
|
|
|
if (!force && file.exists()){
|
|
@@ -89,10 +114,7 @@ public class PdfHelp {
|
|
|
}else if(file.exists()){
|
|
|
file.delete();
|
|
|
}
|
|
|
-
|
|
|
- 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());
|
|
|
+ String[] waters = getUserWater(user);
|
|
|
|
|
|
PdfReader reader = new PdfReader(pdfFile);
|
|
|
|
|
@@ -102,33 +124,40 @@ public class PdfHelp {
|
|
|
int pageSize = reader.getNumberOfPages();
|
|
|
PdfContentByte under;
|
|
|
for (int i = 1; i<=pageSize;i++){
|
|
|
- under = stamper.getUnderContent(i);
|
|
|
- AddTextWatermark(under, name);
|
|
|
- AddTextWatermark(under, qx);
|
|
|
+ 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){
|
|
|
+ void AddTextWatermark(PdfContentByte content, String textWatermark, int alignment, float width, float y){
|
|
|
content.saveState();
|
|
|
- Font f = new Font(font, 10);
|
|
|
- f.setColor(0,0,0);
|
|
|
- f.setStyle(Font.NORMAL);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
PdfGState gs = new PdfGState();
|
|
|
gs.setFillOpacity(0.2f);
|
|
|
content.setGState(gs);
|
|
|
- Phrase p = new Phrase(textWatermark, f);
|
|
|
+
|
|
|
|
|
|
content.beginText();
|
|
|
content.setColorFill(new BaseColor(0,0,0));
|
|
|
- content.setFontAndSize(font, 10);
|
|
|
- content.setTextMatrix(0, 0);
|
|
|
- content.showTextAligned(Element.ALIGN_CENTER, textWatermark, 200, 100, 0);
|
|
|
+ content.setFontAndSize(font, size);
|
|
|
+
|
|
|
+ content.showTextAligned(alignment, textWatermark, width, y, 0);
|
|
|
content.endText();
|
|
|
|
|
|
|
|
|
- ColumnText.showTextAligned(content, Element.ALIGN_CENTER, p, 100f,100f, 0);
|
|
|
+
|
|
|
content.restoreState();
|
|
|
}
|
|
|
|