Compare commits

...

5 Commits

Author SHA1 Message Date
zzs01@yunemao.com
e861a3c62f Merge remote-tracking branch 'origin/dev-zzs' into test 2024-08-27 14:52:33 +08:00
zzs01@yunemao.com
c3f14bdc9f Merge remote-tracking branch 'origin/dev-zzs' into test 2024-08-27 14:51:05 +08:00
zzs01@yunemao.com
713cc279a8 wip 2024-08-27 14:50:37 +08:00
zzs01@yunemao.com
cf4598c101 wip 2024-08-27 14:48:49 +08:00
zzs01@yunemao.com
c30a97c291 wip 2024-08-27 14:28:28 +08:00
2 changed files with 77 additions and 3 deletions

View File

@ -122,6 +122,7 @@ public class SalesOrderAutoSignOp extends AbstractOperationServicePlugIn {
// 书签位置替换为图片书签不会删除 // 书签位置替换为图片书签不会删除
file = ContactSignUtils.replaceBookmarkWithImage(in, stream, filename, BOOKMARK); file = ContactSignUtils.replaceBookmarkWithImage(in, stream, filename, BOOKMARK);
// File pdf = ContactSignUtils.convertToPDF(file);
AttachmentUtil.uploadAttachment(appId, formId, String.valueOf(pkId), UPLOAD_ATTACHMENT_KEY, file.getName(), file.getPath()); AttachmentUtil.uploadAttachment(appId, formId, String.valueOf(pkId), UPLOAD_ATTACHMENT_KEY, file.getName(), file.getPath());
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);

View File

@ -2,11 +2,13 @@ package com.yem.wm.utils;
import com.yem.wm.es.encasement.util.FileUtil; import com.yem.wm.es.encasement.util.FileUtil;
import kd.bos.exception.KDBizException; import kd.bos.exception.KDBizException;
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;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import java.io.*; import java.io.*;
@ -86,4 +88,75 @@ public class ContactSignUtils {
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
} }
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;
}
} }