|
@@ -4,21 +4,31 @@ import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.pdfbox.pdmodel.font.FontMapper;
|
|
|
import org.apache.poi.xwpf.usermodel.*;
|
|
|
+import org.docx4j.Docx4J;
|
|
|
+import org.docx4j.convert.out.FOSettings;
|
|
|
+import org.docx4j.fonts.IdentityPlusMapper;
|
|
|
+import org.docx4j.fonts.Mapper;
|
|
|
+import org.docx4j.fonts.PhysicalFont;
|
|
|
+import org.docx4j.fonts.PhysicalFonts;
|
|
|
+import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URL;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public class WordPoiExporter {
|
|
|
|
|
|
public static void init(String source,String path,JSONObject data) throws IOException {
|
|
|
- InputStream stream = WordPoiExporter.class.getResourceAsStream("/foundation_report_template.json");
|
|
|
- String json = inputStreamToString(stream);
|
|
|
- JSONObject jsonObject = JSON.parseObject(json);
|
|
|
+// InputStream stream = WordPoiExporter.class.getResourceAsStream("/foundation_report_template.json");
|
|
|
+// String json = inputStreamToString(stream);
|
|
|
+// JSONObject jsonObject = JSON.parseObject(json);
|
|
|
|
|
|
try {
|
|
|
- XWPFDocument doc = processTemplate(source, jsonObject);
|
|
|
+ XWPFDocument doc = processTemplate(source, data);
|
|
|
FileOutputStream out = new FileOutputStream(path);
|
|
|
|
|
|
doc.write(out);
|
|
@@ -56,15 +66,20 @@ public class WordPoiExporter {
|
|
|
if(keys.length==2){
|
|
|
String placeholder = "${" +key + "}";
|
|
|
if (runText.contains(placeholder)) {
|
|
|
+ System.out.println(placeholder);
|
|
|
+ String value=data.getString(keys[1]);
|
|
|
+ if(value==null)value="";
|
|
|
// 直接修改Run的文本(保留原格式)
|
|
|
- run.setText(runText.replace(placeholder, data.getString(keys[1])), 0);
|
|
|
+ run.setText(runText.replace(placeholder, value), 0);
|
|
|
}
|
|
|
}else if(keys.length==3){
|
|
|
JSONObject data1=data.getJSONObject(keys[1]);
|
|
|
String placeholder = "${" +key + "}";
|
|
|
if (runText.contains(placeholder)) {
|
|
|
// 直接修改Run的文本(保留原格式)
|
|
|
- run.setText(runText.replace(placeholder, data1.getString(keys[2])), 0);
|
|
|
+ String value=data1.getString(keys[2]);
|
|
|
+ if(value==null)value="";
|
|
|
+ run.setText(runText.replace(placeholder, value), 0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -89,11 +104,16 @@ public class WordPoiExporter {
|
|
|
for (XWPFParagraph p : cell.getParagraphs()) {
|
|
|
String text = p.getText();
|
|
|
if (text.contains("${tableRow:")) {
|
|
|
- String placeholder = text.substring(11, text.length()-1);
|
|
|
+ String key=text.substring(text.indexOf("${")+2,text.indexOf("}"));
|
|
|
+ String[] keys=key.split(":");
|
|
|
// 找到表格占位行
|
|
|
// 获取原行(这里选择第一行作为示例)
|
|
|
- JSONArray tableData = jsonObject.getJSONArray(placeholder);
|
|
|
- addTableRows(tbl, tableData,row);
|
|
|
+ JSONObject obj = jsonObject.getJSONObject(keys[1]);
|
|
|
+ System.out.println(JSONObject.toJSONString(keys));
|
|
|
+ JSONArray tableData = obj.getJSONArray(keys[2]);
|
|
|
+ if (tableData != null && tableData.size() > 0) {
|
|
|
+ addTableRows(tbl, tableData, row);
|
|
|
+ }
|
|
|
tbl.removeRow(tbl.getRows().indexOf(row));
|
|
|
} else {
|
|
|
replaceTextWithFormat(p, jsonObject);
|
|
@@ -187,7 +207,32 @@ public class WordPoiExporter {
|
|
|
// throw new RuntimeException(e);
|
|
|
// }
|
|
|
}
|
|
|
+ public static void convert(String inputPath, String outputPath) {
|
|
|
+ try {
|
|
|
+ // 创建字体映射器
|
|
|
+ Mapper fontMapper = new IdentityPlusMapper();
|
|
|
+
|
|
|
+// // 字体文件的 URI,可以是本地文件的 URI 或者网络文件的 URI
|
|
|
+// String fontFilePath = "C:\\Windows\\Fonts\\微软雅黑.ttf"; // 替换为实际的字体文件 URI
|
|
|
+// URI fontUri = new URI(fontFilePath);
|
|
|
+//
|
|
|
+// PhysicalFonts.addPhysicalFont(fontUri);
|
|
|
+// URI fontUri1 = new URI("C:\\Windows\\Fonts\\思源黑体.ttf");
|
|
|
+//
|
|
|
+// PhysicalFonts.addPhysicalFont(fontUri1);
|
|
|
+
|
|
|
+ WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(inputPath));
|
|
|
+ FOSettings foSettings = Docx4J.createFOSettings();
|
|
|
+ foSettings.setWmlPackage(wordMLPackage);
|
|
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(outputPath)) {
|
|
|
+ Docx4J.toPDF(wordMLPackage, fos);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
private static void saveAsPdf(XWPFDocument doc, String pdfPath) throws Exception {
|
|
|
// 创建PDF文档
|
|
|
/*try (PDDocument pdfDoc = new PDDocument()) {
|