|
@@ -1,9 +1,13 @@
|
|
package org.springblade;
|
|
package org.springblade;
|
|
|
|
|
|
import com.aspose.cad.internal.gl.S;
|
|
import com.aspose.cad.internal.gl.S;
|
|
-import com.documents4j.api.DocumentType;
|
|
|
|
-import com.documents4j.api.IConverter;
|
|
|
|
-import com.documents4j.job.LocalConverter;
|
|
|
|
|
|
+import com.itextpdf.text.BaseColor;
|
|
|
|
+import com.itextpdf.text.DocumentException;
|
|
|
|
+import com.itextpdf.text.Element;
|
|
|
|
+import com.itextpdf.text.pdf.BaseFont;
|
|
|
|
+import com.itextpdf.text.pdf.PdfContentByte;
|
|
|
|
+import com.itextpdf.text.pdf.PdfReader;
|
|
|
|
+import com.itextpdf.text.pdf.PdfStamper;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.jodconverter.core.DocumentConverter;
|
|
import org.jodconverter.core.DocumentConverter;
|
|
import org.jodconverter.core.office.OfficeManager;
|
|
import org.jodconverter.core.office.OfficeManager;
|
|
@@ -19,83 +23,42 @@ import java.net.ConnectException;
|
|
@Slf4j
|
|
@Slf4j
|
|
@SpringBootTest
|
|
@SpringBootTest
|
|
class ServerMainTests {
|
|
class ServerMainTests {
|
|
- public static void main(String[] args) throws ConnectException {
|
|
|
|
|
|
+ public static void main(String[] args) throws IOException, DocumentException {
|
|
String docPath="/Users/wangrui/Downloads/aaa.doc";
|
|
String docPath="/Users/wangrui/Downloads/aaa.doc";
|
|
// String docPath="https://bi.platomix.net/uploads/aaa.doc";
|
|
// String docPath="https://bi.platomix.net/uploads/aaa.doc";
|
|
String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
|
|
String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
|
|
-
|
|
|
|
|
|
+ String pdfPath2="/Users/wangrui/Downloads/aaa2.pdf";
|
|
|
|
+ File outputFile = new File(pdfPath);
|
|
|
|
+ InputStream docxInputStream = new FileInputStream(outputFile);
|
|
// docTopdf(docPath,pdfPath);
|
|
// docTopdf(docPath,pdfPath);
|
|
// wordToPdf(docPath,pdfPath);
|
|
// wordToPdf(docPath,pdfPath);
|
|
- office2PDF(docPath,pdfPath);
|
|
|
|
|
|
+// office2PDF(pdfPath,"zhangsan");
|
|
|
|
+ byte[] zhangs123s = addWatermark(docxInputStream, "zhangs123");
|
|
|
|
+ writePdfToFile(zhangs123s,pdfPath2);
|
|
}
|
|
}
|
|
@Test
|
|
@Test
|
|
- void contextLoads() throws ConnectException {
|
|
|
|
|
|
+ void contextLoads() throws IOException, DocumentException {
|
|
String docPath="/Users/wangrui/Downloads/aaa.doc";
|
|
String docPath="/Users/wangrui/Downloads/aaa.doc";
|
|
// String docPath="https://bi.platomix.net/uploads/aaa.doc";
|
|
// String docPath="https://bi.platomix.net/uploads/aaa.doc";
|
|
String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
|
|
String pdfPath="/Users/wangrui/Downloads/aaa.pdf";
|
|
-
|
|
|
|
|
|
+ File outputFile = new File(pdfPath);
|
|
|
|
+ InputStream docxInputStream = new FileInputStream(outputFile);
|
|
// docTopdf(docPath,pdfPath);
|
|
// docTopdf(docPath,pdfPath);
|
|
// wordToPdf(docPath,pdfPath);
|
|
// wordToPdf(docPath,pdfPath);
|
|
// office2PDF(docPath,pdfPath);
|
|
// office2PDF(docPath,pdfPath);
|
|
|
|
+ addWatermark(docxInputStream,pdfPath);
|
|
}
|
|
}
|
|
- /**
|
|
|
|
- * docx、xlsx、转pdf
|
|
|
|
- * @param docPath
|
|
|
|
- * @param pdfPath
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public static boolean docTopdf(String docPath, String pdfPath) {
|
|
|
|
-// public static boolean docTopdf() {
|
|
|
|
-
|
|
|
|
|
|
|
|
- File inputWord = new File(docPath);
|
|
|
|
- File outputFile = new File(pdfPath);
|
|
|
|
- try {
|
|
|
|
- InputStream docxInputStream = new FileInputStream(inputWord);
|
|
|
|
- OutputStream outputStream = new FileOutputStream(outputFile);
|
|
|
|
- IConverter converter = LocalConverter.builder().build();
|
|
|
|
- String fileTyle=docPath.substring(docPath.lastIndexOf("."),docPath.length());//获取文件类型
|
|
|
|
- if(".docx".equals(fileTyle)){
|
|
|
|
- converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
|
- }else if(".doc".equals(fileTyle)){
|
|
|
|
- converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
|
- }else if(".xls".equals(fileTyle)){
|
|
|
|
- converter.convert(docxInputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
|
- }else if(".xlsx".equals(fileTyle)){
|
|
|
|
- converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
|
|
|
|
- }
|
|
|
|
- docxInputStream.close();
|
|
|
|
- outputStream.close();
|
|
|
|
- inputWord.delete();
|
|
|
|
- System.out.println("pdf转换成功");
|
|
|
|
- return true;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
+ public static void writePdfToFile(byte[] pdfBytes, String filePath) {
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(filePath)) {
|
|
|
|
+ fos.write(pdfBytes);
|
|
|
|
+ System.out.println("PDF 文件已成功写入: " + filePath);
|
|
|
|
+ } catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
- return false;
|
|
|
|
|
|
+ System.err.println("无法写入 PDF 文件: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-// public static void wordToPdf(String docFile,String pdfFile) throws ConnectException {
|
|
|
|
-// // 源文件目录
|
|
|
|
-// File inputFile = new File(docFile);
|
|
|
|
-// // 输出文件目录
|
|
|
|
-// File outputFile = new File(pdfFile);
|
|
|
|
-// if (!outputFile.getParentFile().exists()) {
|
|
|
|
-// outputFile.getParentFile().exists();
|
|
|
|
-// }
|
|
|
|
-// // 连接openoffice服务
|
|
|
|
-// OpenOfficeConnection connection = new SocketOpenOfficeConnection("10.211.55.4", 8100);
|
|
|
|
-//// OpenOfficeConnection connection = new SocketOpenOfficeConnection("localhost", 8100);
|
|
|
|
-//// OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
|
|
|
|
-// connection.connect();
|
|
|
|
-// // 转换word到pdf
|
|
|
|
-// System.out.println(1);
|
|
|
|
-//// DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
|
|
|
-// OpenOfficeDocumentConverter converter = new OpenOfficeDocumentConverter(connection);
|
|
|
|
-// converter.convert(inputFile, outputFile);
|
|
|
|
-// System.out.println(2);
|
|
|
|
-// // 关闭连接
|
|
|
|
-// connection.disconnect();
|
|
|
|
-// }
|
|
|
|
|
|
+
|
|
public static boolean office2PDF(String sourceFile, String destFile) {
|
|
public static boolean office2PDF(String sourceFile, String destFile) {
|
|
// OfficeUrl officeUrl = new OfficeUrl("10.211.55.4:8100");
|
|
// OfficeUrl officeUrl = new OfficeUrl("10.211.55.4:8100");
|
|
// OfficeConnection connection = new OfficeConnection("10.211.55.4", 8100);
|
|
// OfficeConnection connection = new OfficeConnection("10.211.55.4", 8100);
|
|
@@ -126,4 +89,48 @@ class ServerMainTests {
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static byte[] addWatermark(InputStream inputStream, String watermark) throws IOException, DocumentException {
|
|
|
|
+
|
|
|
|
+ PdfReader reader = new PdfReader(inputStream);
|
|
|
|
+ try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
|
|
|
+ PdfStamper stamper = new PdfStamper(reader, os);
|
|
|
|
+ int total = reader.getNumberOfPages() + 1;
|
|
|
|
+ PdfContentByte content;
|
|
|
|
+ // 设置字体
|
|
|
|
+// BaseFont baseFont = BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
|
|
|
+ BaseFont baseFont = BaseFont.createFont();
|
|
|
|
+ // 循环对每页插入水印
|
|
|
|
+ for (int i = 1; i < total; i++) {
|
|
|
|
+ // 水印的起始
|
|
|
|
+ content = stamper.getUnderContent(i);
|
|
|
|
+ // 开始
|
|
|
|
+ content.beginText();
|
|
|
|
+ // 设置颜色
|
|
|
|
+ content.setColorFill(new BaseColor(244, 244, 244));
|
|
|
|
+ // 设置字体及字号
|
|
|
|
+ content.setFontAndSize(baseFont, 50);
|
|
|
|
+ // 设置起始位置
|
|
|
|
+ content.setTextMatrix(400, 780);
|
|
|
|
+ for (int x = 0; x < 5; x++) {
|
|
|
|
+ for (int y = 0; y < 5; y++) {
|
|
|
|
+ content.showTextAlignedKerned(Element.ALIGN_CENTER,
|
|
|
|
+ watermark,
|
|
|
|
+ (100f + x * 350),
|
|
|
|
+ (40.0f + y * 150),
|
|
|
|
+ 30);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ content.endText();
|
|
|
|
+ }
|
|
|
|
+ stamper.close();
|
|
|
|
+ return os.toByteArray();
|
|
|
|
+ }finally {
|
|
|
|
+ reader.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|