Java读写docx⽂件后直接转成PDF⽂件⽅法
1.读写docx⽂件。
需要读写docx⽂件需要⽤到poi包,等下会提供下载地址,在读写docx⽂件之前,需要对docx⽂件进⾏操作,打开docx⽂件后,再需要编辑的地⽅加上“:${key}”,再在java⽂件中通过map来对这个值进⾏编辑,⽐如map.put(key,value)
docx⽂件内操作(注意“:”这个符号是中⽂的,“${}”符号是英⽂的):
编号:${id}
甲⽅(借款⼈):___${jiaName}__________________
⾝份证号/统⼀社会信⽤代码:${jiaIDCard}
住所:${jarAddress}
通讯地址:${jarAddr}
⼄⽅(出借⼈):${yiName}
⾝份证号/统⼀社会信⽤代码:${yiIDCard}
住所:${yiAddress}
通讯地址:${yiAddr}
java⽂件中map的操作:
Map<String, Object> params = new HashMap<String, Object>();
params.put("id", "测试编号" );
params.put("jiaName", "甲⽅名称");
params.put("jiaIDCard", "甲⽅id号");
params.put("jarAddress", "甲⽅住宿");
params.put("jarAddr", "甲⽅地址");
params.put("yiName", "⼄⽅名称");
params.put("yiIDCard", "⼄⽅id号");
params.put("yiAddress", "⼄⽅住宿地址");
params.put("yiAddr", "⼄⽅地址");
有这两个后,接下来是将docx⽂件中的key,value进⾏替换
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import Matcher;
import Pattern;
import org.apache.actor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import topdf.StrToPDF;
public class InputDocx {
private static InputDocx inputDocx;
public static InputDocx getInputDocx() {
if (inputDocx == null)
inputDocx = new InputDocx();
return inputDocx;
public String DocxToString(Map<String, Object> params, String wordUrl) {
XWPFDocument doc = valueInputWord(params, wordUrl);
if (doc == null)return "";
return new XWPFWordExtractor(doc).getText();
}
public XWPFDocument valueInputWord(Map<String, Object> params, String wordUrl) { XWPFDocument doc;// word⽂档对象
try {
InputStream in_stream = new FileInputStream(wordUrl);// ⽂件流
doc = new XWPFDocument(in_stream);
replaceInPara(doc, params);
// OutputStream os = new FileOutputStream("E:\\writejiekuanxieyi.docx");
// doc.write(os);
closeStream(in_stream);
// closeStream(os);
return doc;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭输⼊流
*
* @param is
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
*
* @param os
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 替换段落⾥⾯的变量
*
* @param doc
* 要替换的⽂档
* @param params
* 参数mkdirs方法
private void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFParagraph> iterator = ParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = ();
replaceInPara(para, params);
}
}
/
**
* 替换段落⾥⾯的变量
*
* @param para
* 要替换的段落
* @param params
* 参数
*/
private void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
List<XWPFRun> runs;
Matcher matcher;
System.out.println("para::" + ParagraphText());
if (ParagraphText()).find()) {
runs = Runs();
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = (i);
String runText = String();
System.out.println("runText:1:" + runText);
matcher = matcher(runText);
if (matcher.find()) {
while ((matcher = matcher(runText)).find()) {
runText = placeFirst(String.(up(1))));
System.out.println("runText::" + runText);
}
// 直接调⽤XWPFRun的setText()⽅法设置⽂本时,在底层会重新创建⼀个XWPFRun,把⽂本附加在当前⽂本后⾯, // 所以我们不能直接设值,需要先删除当前run,然后再⾃⼰⼿动插⼊⼀个新的run。
para.insertNewRun(i).setText(runText);
}
}
}
}
/**
* 正则匹配字符串
*
* @param str
* @return
*/
private Matcher matcher(String str) {
Pattern pattern = Patternpile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
}
}
2.创建DocxToPdf类,写将XWPFDocument对象转成pdf⽂件的⽅法
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.verter.pdf.PdfConverter;
import org.apache.verter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class DocxToPdf {
public static void docxToPdf( XWPFDocument document,String outUrl ) throws Exception {
OutputStream outStream=getOutFileStream(outUrl);
PdfOptions options = ate();
}
protected static OutputStream getOutFileStream(String outputFilePath) throws IOException{
File outFile = new File(outputFilePath);
try{
//Make all directories up to specified
} catch (NullPointerException e){
//Ignore error since it means not parent directories
}
FileOutputStream oStream = new FileOutputStream(outFile);
return oStream;
}
}
3.将docx⽂件转成PDF⽂件的⽅法
public static void main(String[] args) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("id", "测试编号" );
params.put("jiaName", "甲⽅名称");
params.put("jiaIDCard", "甲⽅id号");
params.put("jarAddress", "甲⽅住宿");
params.put("jarAddr", "甲⽅地址");
params.put("yiName", "⼄⽅名称");
params.put("yiIDCard", "⼄⽅id号");
params.put("yiAddress", "⼄⽅住宿地址");
params.put("yiAddr", "⼄⽅地址");
XWPFDocument xWPFDocument= InputDocx().valueInputWord(params, "E:\\jiekuanxieyi.docx"); try {
DocxToPdf.docxToPdf(xWPFDocument, "E:\\writejiekuanxieyi.pdf");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.需要的jar包
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论