java 代码替换docx文档中的指定文字(文章主题,时间等),生成新的docx文档,再把文档转换成pdf。目的是在pdf中签名使用。运行有问题的留言.先删掉docx文件的ceshi2.docx和pdf文件下的pdf文件,分别运行两个java类,运行完了刷新就能看到结果了。项目代码
初始docx文件
生成新的docx文件后转成pdf结果:
代码下载地址:http://download.csdn.net/detail/qq_25122241/9730432
部分代码:
1.docx更改文本
public class Test {
public static void searchAndReplace(String srcPath, String destPath,
Map<String, String> map) {
try {
XWPFDocument document = new XWPFDocument(
POIXMLDocument.openPackage(srcPath));
Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
//String s = paragraph.getParagraphText();
Set<String> set = map.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
List<XWPFRun> run=paragraph.getRuns();
for(int i=0;i<run.size();i++)
{
if(run.get(i).getText(run.get(i).getTextPosition())!=null && run.get(i).getText(run.get(i).getTextPosition()).equals(key))
{
run.get(i).setText(map.get(key),0);
}
}
}
}
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
for (Entry<String, String> e : map.entrySet()) {
if (cell.getText().equals(e.getKey())) {
cell.removeParagraph(0);
cell.setText(e.getValue());
}
}
}
}
}
FileOutputStream outStream = null;
outStream = new FileOutputStream(destPath);
document.write(outStream);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("${name}", "姓名");
map.put("${tel}", "8886666");
map.put("${time}", new Date().toString());
String srcPath = System.getProperty("user.dir") + System.getProperty("file.separator") + "docx"+ System.getProperty("file.separator")+"ceshi.docx";
String destPath = System.getProperty("user.dir") + System.getProperty("file.separator") + "docx"+ System.getProperty("file.separator")+"ceshi2.docx";
searchAndReplace(srcPath, destPath, map);
}
}
2.docx转pdf
@SuppressWarnings({"deprecation" })
public class Docx2PDFConverter {
public static void main(String[] args) {
createPDF();
}
private static void createPDF() {
try {
long start = System.currentTimeMillis();
InputStream is = new FileInputStream(new File(
"docx"+ System.getProperty("file.separator") +"ceshi2.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
PdfSettings pdfSettings = new PdfSettings();
OutputStream out = new FileOutputStream(new File(
"pdf"+ System.getProperty("file.separator") +"ceshi3.pdf"));
PdfConversion converter = new Conversion(
wordMLPackage);
converter.output(out, pdfSettings);
System.err.println("Generate pdf with "
+ (System.currentTimeMillis() - start) + "ms");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
注意:因为需要的jar包比较多,所以复制代码肯定不能直接运行的,下载代码的读者看一下lib里的jar包是做docx文本替换的包,libs是docx转pdf的jar包,做了分开。老司机们,自己可以的话可以自己撸起来,只做借鉴,不足之处也别批评哈,哈哈哈