List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun getText
public String getText(int pos)
From source file:b01.officeLink.ExtendedWordDocument.java
License:Apache License
public boolean replaceInParagraph(XWPFParagraph para, String toReplace, String replaceWith) { boolean didReplace = false; if (para != null && toReplace != null && replaceWith != null) { // setOrientation(para); List<XWPFRun> runs = para.getRuns(); TextSegement found = para.searchText(toReplace, new PositionInParagraph()); if (found != null) { if (found.getBeginRun() == found.getEndRun()) { // whole search string is in one Run XWPFRun run = runs.get(found.getBeginRun()); String runText = run.getText(run.getTextPosition()); //Support of Enter to transform it to a line break //------------------------------------------------ String replaced = runText.replace(toReplace, replaceWith); replaceInRun(run, replaced); //------------------------------------------------ // run.setText(replaced, 0); //------------------------------------------------ didReplace = true;// w w w. j a v a 2s . co m } else { // The search string spans over more than one Run // Put the Strings together StringBuilder b = new StringBuilder(); for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) { XWPFRun run = runs.get(runPos); b.append(run.getText(run.getTextPosition())); } String connectedRuns = b.toString(); String replaced = connectedRuns.replace(toReplace, replaceWith); // The first Run receives the replaced String of all connected Runs XWPFRun partOne = runs.get(found.getBeginRun()); //Support of Enter to transform it to a line break //------------------------------------------------ replaceInRun(partOne, replaced);//replaceWith //partOne.setText(replaced, 0); //------------------------------------------------ // Removing the text in the other Runs. for (int runPos = found.getBeginRun() + 1; runPos <= found.getEndRun(); runPos++) { XWPFRun partNext = runs.get(runPos); partNext.setText("", 0); } didReplace = true; } } } return didReplace; }
From source file:biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.java
License:Apache License
public int processBookmarks2Run(XWPFRun runCurrent, List<IDocumentBookmark> arrBookmarks) { String strText = runCurrent.getText(0); if (strText != null) { for (IDocumentBookmark bmCurrent : arrBookmarks) { String strValue = bmCurrent.getValue(); strValue = strValue == null ? "" : strValue; if (bmCurrent.getName() != null) { strText = strText.replaceAll("<<" + bmCurrent.getName() + ">>", strValue); }/* www .j a v a 2 s .co m*/ } } runCurrent.setText(strText, 0); return 1; }
From source file:ch.admin.searchreplace.SearchReplace.java
License:Apache License
/** * Called to perform the search and replace operation. This method will begin with the first character run in * the first paragraph of text and process each. through to the end of the document. The process is essentially * this; * Get a paragraph from the document. * Get the runs of text from that paragraph. * Enumerate through * the keys in the search text/replacement term Hashtable and check to see whether the search tearm (the key) * can be found in the run. If it can be found, replace the search term with it's associated replacement text * and write the results back into the run. * Store the Word document away agin following processing. Note that * there is currently no provision made for writing the resulting document away under a different file name. * This would be a trivial change to make however. * //from w w w . j av a2s .com * @throws java.io.FileNotFoundException Thrown if a problem occurs in the underlying file system whilst trying * to open a FileOutputStream attached to the Word document. * @throws java.io.IOException Thrown if a problem occurs in the underlying file system whilst trying to store * the Word document away. */ public void searchReplace() throws FileNotFoundException, IOException { // Recover an Iterator to step through the XWPFParagraph objects // 'contained' with the Word document. Iterator<XWPFParagraph> parasIter = this.doc.getParagraphsIterator(); Iterator<XWPFRun> runsIter = null; Enumeration<String> srchTermEnum = null; XWPFParagraph para = null; XWPFRun run = null; List<XWPFRun> runsList = null; String searchTerm = null; String runText = null; String replacementText = null; OutputStream os = null; // Step through the XWPFParagraph object one at a time. while (parasIter.hasNext()) { para = parasIter.next(); // Recover a List of XWPFRun objects from the XWPFParagraph and an // Iterator from this List. runsList = para.getRuns(); runsIter = runsList.iterator(); // Use the Iterator to step through the XWPFRun objects. while (runsIter.hasNext()) { // Get a run and it's text run = runsIter.next(); runText = run.getText(0); // Recover an Enumeration object 'containing' the set of // search terms and then step through these terms looking // for match in the run's text srchTermEnum = this.srTerms.keys(); while (srchTermEnum.hasMoreElements()) { searchTerm = srchTermEnum.nextElement(); // If a match is found, use the replacement text to substitute // for the search term and update the runs text. if (runText.contains(searchTerm)) { replacementText = this.srTerms.get(searchTerm); // Note the use of the replaceAll() method here. It will, // obviously, replace all occurrences of the search term // with the replacement text. This may not be what is // required but is easy to modify by changing the logic // slightly and using the replaceFirst() or replace() // methods. runText = runText.replaceAll(searchTerm, replacementText); run.setText(runText, 0); } } } } os = new FileOutputStream(this.sourceFile); this.doc.write(os); }
From source file:ch.admin.searchreplace.SearchReplaceTerms.java
License:Apache License
/** * Konsolidiert die Runs eines Paragraphen in einen. Ansonsten koennen Texte nicht sauber * ersetzt werden, bzw. werden nicht gefunden, weil ueber mehrere Runs verteilt. * @param para Paragraph//from w w w . jav a 2 s.co m * @return Konsolidierter Run */ private static XWPFRun consolidateRuns(XWPFParagraph para) { StringBuffer text = new StringBuffer(); int count = 0; for (XWPFRun r : para.getRuns()) { text.append(r.getText(0)); count++; } for (int i = count - 1; i >= 1; i--) para.removeRun(i); if (count == 0) return (null); XWPFRun r = para.getRuns().get(0); r.setText(text.toString(), 0); return (r); }
From source file:ch.admin.searchreplace.SearchReplaceTerms.java
License:Apache License
/** * @param srTerms//w w w .j av a 2 s. c o m * @param r */ private static void searchReplace(HashMap<String, String> srTerms, XWPFRun r) { String text = r.getText(0); System.out.println(text); for (Map.Entry<String, String> sr : srTerms.entrySet()) { if (text.contains(sr.getKey())) { text = text.replace(sr.getKey(), sr.getValue()); r.setText(text, 0); } // Vergleich mit 1. Buchstaben in Kleinbuchstaben String search2 = sr.getKey().substring(0, 1).toLowerCase() + sr.getKey().substring(1); if (text.contains(search2)) { text = text.replace(search2, sr.getValue().substring(0, 1).toLowerCase() + sr.getValue().substring(1)); r.setText(text, 0); } } }
From source file:cn.afterturn.easypoi.word.parse.ParseWord07.java
License:Apache License
/** * ??// w w w .j av a 2s.c om * * @param paragraph * @param map * @author JueYue * 2013-11-16 */ private void parseThisParagraph(XWPFParagraph paragraph, Map<String, Object> map) throws Exception { XWPFRun run; XWPFRun currentRun = null;// run,?set,??? String currentText = "";// ?text String text; Boolean isfinde = false;// ???{{ List<Integer> runIndex = new ArrayList<Integer>();// ?run, for (int i = 0; i < paragraph.getRuns().size(); i++) { run = paragraph.getRuns().get(i); text = run.getText(0); if (StringUtils.isEmpty(text)) { continue; } // ""? if (isfinde) { currentText += text; if (currentText.indexOf(START_STR) == -1) { isfinde = false; runIndex.clear(); } else { runIndex.add(i); } if (currentText.indexOf(END_STR) != -1) { changeValues(paragraph, currentRun, currentText, runIndex, map); currentText = ""; isfinde = false; } } else if (text.indexOf(START_STR) >= 0) {// ? currentText = text; isfinde = true; currentRun = run; } else { currentText = ""; } if (currentText.indexOf(END_STR) != -1) { changeValues(paragraph, currentRun, currentText, runIndex, map); isfinde = false; } } }
From source file:com.altar.worddocreader.WordDocReaderMain.java
public static XWPFDocument replaceText(XWPFDocument doc, HashMap<String, String> keys) throws Exception { String txt = ""; int txtPosition = 0; String key = ""; String val = ""; for (XWPFParagraph p : doc.getParagraphs()) { //Dkmandaki her bir paragraf okumas yaplyor. for (XWPFRun run : p.getRuns()) { //paragraf iindeki satrlar okunuyor. txtPosition = run.getTextPosition(); txt = run.getText(txtPosition); for (Map.Entry<String, String> entry : keys.entrySet()) { //keymap iinde gnderilen alanlar keymap'teki deerleri ile deitiriliyor. key = entry.getKey();//from w w w . j a v a2 s . c o m val = entry.getValue(); if (txt != null && txt.indexOf(key) > -1) { txt = txt.replace(key, val); run.setText(txt, 0); } } } } return doc; }
From source file:com.anphat.customer.controller.ExportContractToDocController.java
public void buildContractDetails(CustomerDTO customerDTO) { for (XWPFTable tbl : lstTable) { for (XWPFTableRow rowTbl : tbl.getRows()) { for (XWPFTableCell cellTbl : rowTbl.getTableCells()) { for (XWPFParagraph p : cellTbl.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); // //Thoi gian // if (text != null && DataUtil.isStringContainDateTime(text)) { // text = DataUtil.replaceDateTime(text); // r.setText(text, 0); // } //Ben giao if (text != null && text.contains(Constants.REPORT.NAME)) { text = text.replace(Constants.REPORT.NAME, DataUtil.getStringNullOrZero(customerDTO.getName().toUpperCase())); r.setText(text, 0); }/*from www. j a v a 2 s. c om*/ //Ma so thue if (text != null && text.contains(Constants.REPORT.TAX_CODE)) { text = text.replace(Constants.REPORT.TAX_CODE, DataUtil.getStringNullOrZero(customerDTO.getTaxCode())); r.setText(text, 0); } //So dien thoai if (text != null && text.contains(Constants.REPORT.TEL_NUMBER)) { text = text.replace(Constants.REPORT.TEL_NUMBER, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.TEL_NUMBER))); r.setText(text, 0); } //Fax if (text != null && text.contains(Constants.REPORT.FAX)) { text = text.replace(Constants.REPORT.FAX, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.FAX))); r.setText(text, 0); } //Email if (text != null && text.contains(Constants.REPORT.EMAIL)) { text = text.replace(Constants.REPORT.EMAIL, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL))); r.setText(text, 0); } //Dia chi tru so if (text != null && text.contains(Constants.REPORT.OFFICE_ADDRESS)) { text = text.replace(Constants.REPORT.OFFICE_ADDRESS, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.OFFICE_ADDRESS))); r.setText(text, 0); } //Dia chi giao dich if (text != null && text.contains(Constants.REPORT.DEPLOY_ADDRESS)) { text = text.replace(Constants.REPORT.DEPLOY_ADDRESS, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.DEPLOY_ADDRESS))); r.setText(text, 0); } //Co quan thue if (text != null && text.contains(Constants.REPORT.TAX_DEPARTMENT)) { text = text.replace(Constants.REPORT.TAX_DEPARTMENT, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.TAX_DEPARTMENT))); r.setText(text, 0); } //CMND if (text != null && text.contains(Constants.REPORT.CMND)) { text = text.replace(Constants.REPORT.CMND, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.CMND))); r.setText(text, 0); } //CMND if (text != null && text.contains(Constants.REPORT.NGAY_CAP_CMND)) { text = text.replace(Constants.REPORT.NGAY_CAP_CMND, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.NGAY_CAP_CMND))); r.setText(text, 0); } //Nguoi dai dien if (text != null && text.contains(Constants.REPORT.NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.NGUOI_DAIDIEN))); r.setText(text, 0); } //Chuc vu Nguoi dai dien if (text != null && text.contains(Constants.REPORT.CHUVU_NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.CHUVU_NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.CHUVU_NGUOI_DAIDIEN))); r.setText(text, 0); } //SDT Nguoi dai dien if (text != null && text.contains(Constants.REPORT.SDT_NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.SDT_NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.SDT_NGUOI_DAIDIEN))); r.setText(text, 0); } //Email Nguoi dai dien if (text != null && text.contains(Constants.REPORT.EMAIL_NGUOI_DAIDIEN)) { text = text.replace(Constants.REPORT.EMAIL_NGUOI_DAIDIEN, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL_NGUOI_DAIDIEN))); r.setText(text, 0); } //Nguoi dai dien if (text != null && text.contains(Constants.REPORT.NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.NGUOI_LIENHE, DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.NGUOI_LIENHE))); r.setText(text, 0); } //Chuc vu Nguoi lien he if (text != null && text.contains(Constants.REPORT.CHUCVU_NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.CHUCVU_NGUOI_LIENHE, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.CHUCVU_NGUOI_LIENHE))); r.setText(text, 0); } //SDT Nguoi lien he if (text != null && text.contains(Constants.REPORT.SDT_NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.SDT_NGUOI_LIENHE, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.SDT_NGUOI_LIENHE))); r.setText(text, 0); } //Email Nguoi lien he if (text != null && text.contains(Constants.REPORT.EMAIL_NGUOI_LIENHE)) { text = text.replace(Constants.REPORT.EMAIL_NGUOI_LIENHE, DataUtil .getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL_NGUOI_LIENHE))); r.setText(text, 0); } } } } } } }
From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java
License:Open Source License
private void doReplace(XWPFParagraph p, Map<String, String> data) { String pText = p.getText(); // complete paragraph as string if (pText.contains("${")) { // if paragraph does not include our pattern, ignore Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}"); Matcher matcher = pattern.matcher(pText); while (matcher.find()) { // for all patterns in the paragraph String key = matcher.group(1); // extract key start and end pos int start = matcher.start(1); int end = matcher.end(1); String value = data.get(key); if (value == null) { value = ""; }/*w w w. j av a2 s . co m*/ // get runs which contain the pattern SortedMap<Integer, XWPFRun> range = getPosToRuns(p).subMap(start - 2, true, end + 1, true); boolean isFoundDollarSign = false; boolean isFoundLeftBracket = false; boolean isFoundRightBracket = false; XWPFRun prevRun = null; // previous run handled in the loop XWPFRun found2Run = null; // run in which { was found int found2Pos = -1; // pos of { within above run for (XWPFRun run : range.values()) { if (run == prevRun) { continue; // this run has already been handled } if (isFoundRightBracket) { break; // done working on current key pattern } prevRun = run; for (int k = 0;; k++) { // iterate over texts of run r if (isFoundRightBracket) { break; } String txt = null; try { txt = run.getText(k); // note: should return null, but throws exception if the text does not exist } catch (Exception e) { } if (txt == null) { break; // no more texts in the run, exit loop } if (txt.contains("$") && !isFoundDollarSign) { // found $, replace it with value from data map txt = txt.replaceFirst("\\$", value); isFoundDollarSign = true; } if (txt.contains("{") && !isFoundLeftBracket && isFoundDollarSign) { found2Run = run; // found { replace it with empty string and remember location found2Pos = txt.indexOf('{'); txt = txt.replaceFirst("\\{", ""); isFoundLeftBracket = true; } // find } and set all chars between { and } to blank if (isFoundDollarSign && isFoundLeftBracket && !isFoundRightBracket) { if (txt.contains("}")) { if (run == found2Run) { // complete pattern was within a single run txt = txt.substring(0, found2Pos) + txt.substring(txt.indexOf('}')); } else { txt = txt.substring(txt.indexOf('}')); } } else if (run == found2Run) { txt = txt.substring(0, found2Pos); } else { txt = ""; // run between { and }, set text to blank } } if (txt.contains("}") && !isFoundRightBracket) { txt = txt.replaceFirst("\\}", ""); isFoundRightBracket = true; } run.setText(txt, k); } } } } }
From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java
License:Open Source License
private NavigableMap<Integer, XWPFRun> getPosToRuns(XWPFParagraph paragraph) { int pos = 0;/*from w ww . j a va2 s . c o m*/ NavigableMap<Integer, XWPFRun> map = new TreeMap<>(); for (XWPFRun run : paragraph.getRuns()) { String runText = run.getText(run.getTextPosition()); if (runText != null && runText.length() > 0) { for (int i = 0; i < runText.length(); i++) { map.put(pos + i, run); } pos += runText.length(); } } return map; }