Compare commits
5 Commits
32671a6e5a
...
e861a3c62f
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e861a3c62f | ||
![]() |
c3f14bdc9f | ||
![]() |
713cc279a8 | ||
![]() |
cf4598c101 | ||
![]() |
c30a97c291 |
@ -122,6 +122,7 @@ public class SalesOrderAutoSignOp extends AbstractOperationServicePlugIn {
|
||||
|
||||
// 书签位置替换为图片(书签不会删除)
|
||||
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());
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
|
@ -2,11 +2,13 @@ package com.yem.wm.utils;
|
||||
|
||||
import com.yem.wm.es.encasement.util.FileUtil;
|
||||
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.util.Units;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
|
||||
|
||||
import java.io.*;
|
||||
@ -86,4 +88,75 @@ public class ContactSignUtils {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user