|
|
@ -1,5 +1,9 @@ |
|
|
|
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.com.ComThread; |
|
|
|
import com.jacob.com.Dispatch; |
|
|
@ -138,7 +142,67 @@ public class WordConvertUtils { |
|
|
|
System.out.println("关闭文档"); |
|
|
|
// 如果没有这句话,winword.exe进程将不会关闭
|
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|