修复问题
This commit is contained in:
@@ -35,6 +35,12 @@ public class DocPdfComponent {
|
|||||||
@Value("${templateUrl.prefixUrl:http://127.0.0.1:8080/template/}")
|
@Value("${templateUrl.prefixUrl:http://127.0.0.1:8080/template/}")
|
||||||
private String prefixTemplateUrl;
|
private String prefixTemplateUrl;
|
||||||
|
|
||||||
|
@Value("${templateUrl.uploadPdfUrl:http://127.0.0.1:8080/template/pdf/}")
|
||||||
|
private String uploadPdfUrl;
|
||||||
|
|
||||||
|
@Value("${templateUrl.prefixPdfUrl:http://127.0.0.1:8080/template/pdf/}")
|
||||||
|
private String prefixPdfUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ftl模板生成word
|
* 根据ftl模板生成word
|
||||||
*
|
*
|
||||||
@@ -170,4 +176,12 @@ public class DocPdfComponent {
|
|||||||
public String getPrefixTemplateUrl() {
|
public String getPrefixTemplateUrl() {
|
||||||
return prefixTemplateUrl;
|
return prefixTemplateUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUploadPdfUrl() {
|
||||||
|
return uploadPdfUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrefixPdfUrl() {
|
||||||
|
return prefixPdfUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package com.yxt.common.base.utils;
|
package com.yxt.common.base.utils;
|
||||||
|
|
||||||
|
import com.itextpdf.text.BaseColor;
|
||||||
|
import com.itextpdf.text.Element;
|
||||||
|
import com.itextpdf.text.Rectangle;
|
||||||
|
import com.itextpdf.text.pdf.*;
|
||||||
import com.jacob.activeX.ActiveXComponent;
|
import com.jacob.activeX.ActiveXComponent;
|
||||||
import com.jacob.com.ComThread;
|
import com.jacob.com.ComThread;
|
||||||
import com.jacob.com.Dispatch;
|
import com.jacob.com.Dispatch;
|
||||||
@@ -138,7 +142,67 @@ public class WordConvertUtils {
|
|||||||
System.out.println("关闭文档");
|
System.out.println("关闭文档");
|
||||||
// 如果没有这句话,winword.exe进程将不会关闭
|
// 如果没有这句话,winword.exe进程将不会关闭
|
||||||
ComThread.Release();
|
ComThread.Release();
|
||||||
|
new File(wordFile).delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param inputFile 源文件全路径:你的PDF文件地址
|
||||||
|
* @param outputFile 输出全路径:添加水印后生成PDF存放的地址
|
||||||
|
* @param outDir 添加水印后生成PDF存放的目录
|
||||||
|
* @param waterMarkName 你的水印
|
||||||
|
* @return 注意:源文件全路径和输出全路径最好不要写同一样的,不然有可能会报“java.io.FileNotFoundException:请求的操作无法在使用用户映射区域打开的文件上执行”的错误
|
||||||
|
*/
|
||||||
|
public static boolean waterMark(String inputFile, String outputFile, String outDir, String waterMarkName) {
|
||||||
|
try {
|
||||||
|
File targetFile = new File(outDir);
|
||||||
|
if (!targetFile.exists()) {
|
||||||
|
targetFile.mkdirs();
|
||||||
|
}
|
||||||
|
PdfReader reader = new PdfReader(inputFile);
|
||||||
|
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
|
||||||
|
// 这里的字体设置比较关键,这个设置是支持中文的写法
|
||||||
|
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
|
||||||
|
int total = reader.getNumberOfPages() + 1;
|
||||||
|
PdfContentByte under;
|
||||||
|
Rectangle pageRect = null;
|
||||||
|
for (int i = 1; i < total; i++) {
|
||||||
|
pageRect = stamper.getReader().getPageSizeWithRotation(i);
|
||||||
|
// 计算水印X,Y坐标
|
||||||
|
// float x = pageRect.getWidth() / 10;
|
||||||
|
// float y = pageRect.getHeight() / 10 - 10;
|
||||||
|
float x = 290;
|
||||||
|
float y = 400;
|
||||||
|
// 获得PDF最顶层
|
||||||
|
under = stamper.getOverContent(i);
|
||||||
|
under.saveState();
|
||||||
|
// set Transparency
|
||||||
|
PdfGState gs = new PdfGState();
|
||||||
|
// 设置透明度为0.2
|
||||||
|
// gs.setFillOpacity(1.f);
|
||||||
|
gs.setFillOpacity(0.3f);
|
||||||
|
under.setGState(gs);
|
||||||
|
under.restoreState();
|
||||||
|
under.beginText();
|
||||||
|
under.setFontAndSize(base, 35);
|
||||||
|
// under.setColorFill(BaseColor.ORANGE);
|
||||||
|
under.setColorFill(BaseColor.BLACK);
|
||||||
|
|
||||||
|
// 水印文字成45度角倾斜
|
||||||
|
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, y, 45);
|
||||||
|
// 添加水印文字
|
||||||
|
under.endText();
|
||||||
|
under.setLineWidth(1f);
|
||||||
|
under.stroke();
|
||||||
|
}
|
||||||
|
stamper.close();
|
||||||
|
reader.close();
|
||||||
|
new File(inputFile).delete();
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user