|
@@ -4,6 +4,10 @@ import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
+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.xwpf.usermodel.*;
|
|
import org.apache.poi.xwpf.usermodel.*;
|
|
import org.docx4j.Docx4J;
|
|
import org.docx4j.Docx4J;
|
|
import org.docx4j.convert.out.FOSettings;
|
|
import org.docx4j.convert.out.FOSettings;
|
|
@@ -21,13 +25,14 @@ public class WordPoiExporter {
|
|
// String json = inputStreamToString(stream);
|
|
// String json = inputStreamToString(stream);
|
|
// JSONObject jsonObject = JSON.parseObject(json);
|
|
// JSONObject jsonObject = JSON.parseObject(json);
|
|
|
|
|
|
|
|
+ String pdfFilePath =System.getProperty("user.dir")+File.separator+ "download.pdf";
|
|
try {
|
|
try {
|
|
XWPFDocument doc = processTemplate(source, data);
|
|
XWPFDocument doc = processTemplate(source, data);
|
|
FileOutputStream out = new FileOutputStream(path);
|
|
FileOutputStream out = new FileOutputStream(path);
|
|
|
|
|
|
doc.write(out);
|
|
doc.write(out);
|
|
|
|
+ saveAsPdf(doc, pdfFilePath);
|
|
out.close();
|
|
out.close();
|
|
-// saveAsPdf(doc, FxyTool.class.getResource("/output.pdf").getPath());
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
@@ -69,7 +74,7 @@ public class WordPoiExporter {
|
|
}else if(keys.length==3){
|
|
}else if(keys.length==3){
|
|
JSONObject data1=data.getJSONObject(keys[1]);
|
|
JSONObject data1=data.getJSONObject(keys[1]);
|
|
String placeholder = "${" +key + "}";
|
|
String placeholder = "${" +key + "}";
|
|
- if (runText.contains(placeholder)) {
|
|
|
|
|
|
+ if (data1!=null && runText.contains(placeholder)) {
|
|
// 直接修改Run的文本(保留原格式)
|
|
// 直接修改Run的文本(保留原格式)
|
|
String value=data1.getString(keys[2]);
|
|
String value=data1.getString(keys[2]);
|
|
if(value==null)value="";
|
|
if(value==null)value="";
|
|
@@ -189,17 +194,26 @@ public class WordPoiExporter {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public static void convertWordToPdf(String inputFilePath, String outputFilePath) {
|
|
public static void convertWordToPdf(String inputFilePath, String outputFilePath) {
|
|
-// try (FileInputStream fis = new FileInputStream(inputFilePath);
|
|
|
|
-// XWPFDocument document = new XWPFDocument(fis);
|
|
|
|
-// FileOutputStream fos = new FileOutputStream(outputFilePath)) {
|
|
|
|
-//
|
|
|
|
-// PdfOptions options = PdfOptions.create();
|
|
|
|
-// PdfConverter.getInstance().convert(document, fos, options);
|
|
|
|
-// } catch (FileNotFoundException e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// }
|
|
|
|
|
|
+ try (FileInputStream fis = new FileInputStream(inputFilePath);
|
|
|
|
+ XWPFDocument wordDoc = new XWPFDocument(fis);
|
|
|
|
+ PDDocument pdfDoc = new PDDocument()) {
|
|
|
|
+
|
|
|
|
+ for (XWPFParagraph paragraph : wordDoc.getParagraphs()) {
|
|
|
|
+ PDPage page = new PDPage();
|
|
|
|
+ try ( PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page)) {
|
|
|
|
+ contentStream.beginText();
|
|
|
|
+ contentStream.setFont(PDType1Font.HELVETICA, 12);
|
|
|
|
+ contentStream.newLineAtOffset(100, 700);
|
|
|
|
+ contentStream.showText(paragraph.getText());
|
|
|
|
+ contentStream.endText();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pdfDoc.save(outputFilePath);
|
|
|
|
+ System.out.println("转换完成!");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
public static void convert(String inputPath, String outputPath) {
|
|
public static void convert(String inputPath, String outputPath) {
|
|
try {
|
|
try {
|
|
@@ -248,12 +262,13 @@ public class WordPoiExporter {
|
|
}
|
|
}
|
|
private static void saveAsPdf(XWPFDocument doc, String pdfPath) throws Exception {
|
|
private static void saveAsPdf(XWPFDocument doc, String pdfPath) throws Exception {
|
|
// 创建PDF文档
|
|
// 创建PDF文档
|
|
- /*try (PDDocument pdfDoc = new PDDocument()) {
|
|
|
|
|
|
+ try (PDDocument pdfDoc = new PDDocument()) {
|
|
PDPage page = new PDPage();
|
|
PDPage page = new PDPage();
|
|
pdfDoc.addPage(page);
|
|
pdfDoc.addPage(page);
|
|
|
|
|
|
// 写入内容
|
|
// 写入内容
|
|
- try ( PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page)) {
|
|
|
|
|
|
+ try {
|
|
|
|
+ PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page);
|
|
contentStream.setFont(PDType1Font.HELVETICA, 12);
|
|
contentStream.setFont(PDType1Font.HELVETICA, 12);
|
|
float margin = 50;
|
|
float margin = 50;
|
|
float yStart = page.getMediaBox().getHeight() - margin;
|
|
float yStart = page.getMediaBox().getHeight() - margin;
|
|
@@ -285,7 +300,9 @@ public class WordPoiExporter {
|
|
// 保存PDF
|
|
// 保存PDF
|
|
contentStream.close();
|
|
contentStream.close();
|
|
pdfDoc.save(pdfPath);
|
|
pdfDoc.save(pdfPath);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
}
|
|
}
|
|
- }*/
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|