import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
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.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
//http://stackoverflow.com/questions/22268898/replacing-a-text-in-apache-poi-xwpf
public class XWPFUtils {
public static final Map<String,String> map=new HashMap<String, String>();
public static final List<String> tags=new ArrayList<String>();
public String[] getStringArray(int cnt) {
List<String> arr= new ArrayList<String>(Collections.nCopies(cnt, ""));
return (String[]) arr.toArray(new String[] {});
// return new String[cnt];
}
public static void clear() {
map.clear();
}
public static String add(String key, String value) {
return map.put(key,value);
}
public String[] getTags(String fnameIN, String ptn) {
XWPFDocument doc=null;
FileInputStream fis=null;
tags.clear();
Pattern pattern=Pattern.compile(ptn);
try {
fis=new FileInputStream(fnameIN);
doc = new XWPFDocument(OPCPackage.open(fis));
} catch (InvalidFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (XWPFParagraph p : doc.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String s=paragraphMatch(r,pattern);
if (s!=null) {tags.add(s);}
}
}
for (XWPFTable tbl : doc.getTables()) {
for (XWPFTableRow row : tbl.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph p1 : cell.getParagraphs()) {
for (XWPFRun r : p1.getRuns()) {
String s=paragraphMatch(r,pattern);
if (s!=null) {tags.add(s);}
}
}
}
}
}
}
try {
System.out.println("Closing input doc and stream...");
if (fis!= null)fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tags.toArray(new String[tags.size()]);
}
/**
* @param fnameIN: file name for source Word doc
* @param fnameOUT: file name for result Word doc
* uses map for word replacing
*/
public void replaceFromArray(String fnameIN, String fnameOUT) {
XWPFDocument doc=null;
FileInputStream fis=null;
try {
fis=new FileInputStream(fnameIN);
doc = new XWPFDocument(OPCPackage.open(fis));
} catch (InvalidFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (XWPFParagraph p : doc.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
paragraphReplace(r);
}
}
for (XWPFTable tbl : doc.getTables()) {
for (XWPFTableRow row : tbl.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph p1 : cell.getParagraphs()) {
for (XWPFRun r : p1.getRuns()) {
paragraphReplace(r);
}
}
}
}
}
}
try {
doc.write(new FileOutputStream(fnameOUT));
System.out.println("Done");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
System.out.println("Closing input doc and stream...");
//if (doc!=null)doc.close();
if (fis!= null)fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void replaceFromArray(String []arrFrom, String[] arrTo, String fnameIN, String fnameOUT) {
XWPFDocument doc=null;
FileInputStream fis=null;
try {
fis=new FileInputStream(fnameIN);
doc = new XWPFDocument(OPCPackage.open(fis));
} catch (InvalidFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (XWPFParagraph p : doc.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
paragraphReplace(arrFrom,arrTo,r);
}
}
for (XWPFTable tbl : doc.getTables()) {
for (XWPFTableRow row : tbl.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph p1 : cell.getParagraphs()) {
for (XWPFRun r : p1.getRuns()) {
paragraphReplace(arrFrom,arrTo,r);
}
}
}
}
}
}
try {
doc.write(new FileOutputStream(fnameOUT));
System.out.println("Done");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
System.out.println("Closing input doc and stream...");
//if (doc!=null)doc.close();
if (fis!= null)fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* if (pkg!=null)
try {
pkg.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/ }
}
private String paragraphMatch(XWPFRun r, Pattern pattern) {
String text = r.getText(0);
if (text != null) {
//Pattern ptn = Pattern.compile(pattern);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println(matcher.group(0));
return matcher.group(0);
}
}
return null;
}
private void paragraphReplace( String[] arrFrom, String[] arrTo, XWPFRun r) {
String text = r.getText(0);
if (text != null) {
for(int i=0; i<arrFrom.length;i++) {
if (arrFrom[i].length()>0) {
if (text.contains(arrFrom[i])) {
text = text.replace(arrFrom[i], arrTo[i]);
System.out.println("Replacement:"+arrFrom[i]+"|"+arrTo[i]);
r.setText(text, 0);
}
}
}
}
}
private void paragraphReplace(XWPFRun r) {
String text = r.getText(0);
if (text != null) {
for(Map.Entry<String, String> entry : map.entrySet()) {
if(entry.getKey().length()>0) {
if (text.contains(entry.getKey())) {
text=text.replace(entry.getKey(), entry.getValue());
System.out.println("Replacement:"+entry.getKey()+"|"+entry.getValue());
//r.setBold(true);
r.setText(text, 0);
}
}
}
}
}
}