Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -120,12 +120,6 @@
|
||||
<artifactId>javase</artifactId>
|
||||
<version>3.3.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itextpdf</artifactId>
|
||||
<version>5.4.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
@@ -165,17 +159,11 @@
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
<version>2.3.29</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jacob</groupId>
|
||||
<artifactId>jacob</artifactId>
|
||||
<version>1.18</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
@@ -196,6 +184,25 @@
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jacob</groupId>
|
||||
<artifactId>jacob</artifactId>
|
||||
<version>1.18</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itextpdf</artifactId>
|
||||
<version>5.5.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itext-asian</artifactId>
|
||||
<version>5.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
|
||||
@@ -35,6 +35,12 @@ public class DocPdfComponent {
|
||||
@Value("${templateUrl.prefixUrl:http://127.0.0.1:8080/template/}")
|
||||
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
|
||||
*
|
||||
@@ -170,4 +176,12 @@ public class DocPdfComponent {
|
||||
public String getPrefixTemplateUrl() {
|
||||
return prefixTemplateUrl;
|
||||
}
|
||||
|
||||
public String getUploadPdfUrl() {
|
||||
return uploadPdfUrl;
|
||||
}
|
||||
|
||||
public String getPrefixPdfUrl() {
|
||||
return prefixPdfUrl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
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;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.Version;
|
||||
import org.apache.pdfbox.io.MemoryUsageSetting;
|
||||
import org.apache.pdfbox.multipdf.PDFMergerUtility;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -138,7 +145,91 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* pdf合并
|
||||
*
|
||||
* @param files 源文件的相对路径
|
||||
* @param targetPath 输出的pdf的路径
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static File mulFile2One(List<File> files, String targetPath) throws Exception {
|
||||
PDFMergerUtility mergePdf = new PDFMergerUtility();
|
||||
for (File f : files) {
|
||||
if (f.exists() && f.isFile()) {
|
||||
// 循环添加要合并的pdf
|
||||
mergePdf.addSource(f);
|
||||
}
|
||||
}
|
||||
// 设置合并生成pdf文件名称
|
||||
mergePdf.setDestinationFileName(targetPath);
|
||||
// 合并pdf
|
||||
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
|
||||
//ToDo:根据需求实现是否要删除源文件
|
||||
return new File(targetPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +77,8 @@
|
||||
<poi.version>4.1.2</poi.version>
|
||||
<flowable.version>6.5.0</flowable.version>
|
||||
|
||||
<pdfbox.version>2.0.25</pdfbox.version>
|
||||
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
@@ -401,6 +403,12 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox</artifactId>
|
||||
<version>${pdfbox.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user