ensign/src/main/java/com/yem/wm/utils/ContactSignUtils.java

163 lines
6.3 KiB
Java
Raw Normal View History

2024-08-26 09:19:12 +08:00
package com.yem.wm.utils;
import com.yem.wm.es.encasement.util.FileUtil;
import kd.bos.exception.KDBizException;
2024-08-27 14:28:28 +08:00
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
2024-08-26 09:19:12 +08:00
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
2024-08-27 14:28:28 +08:00
import org.apache.poi.xwpf.usermodel.*;
2024-08-26 09:19:12 +08:00
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Description: 合同盖章
* @Date: 2024/8/8 17:59
* @Created: by ZZSLL
*/
public class ContactSignUtils {
public static List<CTBookmark> getFileBookMarks(InputStream in) {
List<CTBookmark> list = new ArrayList<>();
try (XWPFDocument doc = new XWPFDocument(in)) {
for (XWPFParagraph paragraph : doc.getParagraphs()) {
list.addAll(paragraph.getCTP().getBookmarkStartList());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return list;
}
/**
* Word 文档中的书签位置插入图片并保存修改后的文档
*
* @param wordFile 原始 Word 文档
* @param imageFile 图片文件
* @param outfileName 修改后的 Word 文档保存路径
* @param bookmarkName 书签名称
* @return 修改后的 Word 文件
* @throws IOException IO 异常
* @throws InvalidFormatException 格式异常
*/
public static File replaceBookmarkWithImage(InputStream wordFile, InputStream imageFile, String outfileName, String bookmarkName) throws IOException {
byte[] imageBytes = readImageFile(imageFile);
try (XWPFDocument doc = new XWPFDocument(wordFile)) {
for (XWPFParagraph paragraph : doc.getParagraphs()) {
List<CTBookmark> bookmarkStartList = paragraph.getCTP().getBookmarkStartList();
for (CTBookmark bookmark : bookmarkStartList) {
if (bookmarkName.equals(bookmark.getName())) {
List<XWPFRun> runs = paragraph.getRuns();
XWPFRun run;
if (!runs.isEmpty()) {
run = runs.get(0);
} else {
run = paragraph.createRun();
}
// 设置图片大小 长宽比: 1.8586387435
run.addPicture(new ByteArrayInputStream(imageBytes), XWPFDocument.PICTURE_TYPE_PNG, "imageName.png", Units.toEMU(74), Units.toEMU(40));
}
}
}
FileUtil fileUtil = new FileUtil("sales-order-sign-file");
File outFile = fileUtil.createNewFile(outfileName);
try (FileOutputStream out = new FileOutputStream(outFile)) {
doc.write(out);
}
return outFile;
} catch (InvalidFormatException e) {
throw new KDBizException("无法解析文件 " + outfileName + " 格式:" + e.getMessage());
}
}
private static byte[] readImageFile(InputStream file) throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = file.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toByteArray();
}
}
2024-08-27 14:28:28 +08:00
public static File convertToPDF(File file) {
if (file == null) {
return null;
}
try {
FileInputStream fis = new FileInputStream(file);
XWPFDocument docx = new XWPFDocument(fis);
// 创建 PDF 文档
PDDocument pdf = new PDDocument();
PDPage page = new PDPage();
pdf.addPage(page);
// 获取 PDF 页面的内容流
PDPageContentStream contentStream = new PDPageContentStream(pdf, page);
// 设置字体和字号
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
// 遍历 docx 文档的段落和表格
List<XWPFParagraph> paragraphs = docx.getParagraphs();
List<XWPFTable> tables = docx.getTables();
for (XWPFParagraph paragraph : paragraphs) {
String text = paragraph.getText();
contentStream.beginText();
contentStream.newLineAtOffset(25, 700);
contentStream.showText(text);
contentStream.endText();
}
for (XWPFTable table : tables) {
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
String text = cell.getText();
contentStream.beginText();
contentStream.newLineAtOffset(25, 700);
contentStream.showText(text);
contentStream.endText();
}
}
}
FileUtil fileUtil = new FileUtil("sales-order-sign-file-pdf");
File outFile = fileUtil.createNewFile(changeFileExtension(file.getName(), "pdf"));
contentStream.close();
pdf.save(outFile);
pdf.close();
fis.close();
return outFile;
} catch (IOException e) {
throw new KDBizException("Word转换PDF失败" + e.getMessage());
}
}
public static String changeFileExtension(String fileName, String newExtension) {
String fileNameWithoutExtension = removeFileExtension(fileName);
return fileNameWithoutExtension + "." + newExtension;
}
private static String removeFileExtension(String fileName) {
int dotIndex = fileName.lastIndexOf(".");
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
return fileName.substring(0, dotIndex);
}
return fileName;
}
2024-08-26 09:19:12 +08:00
}