List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph searchText
public TextSegment searchText(String searched, PositionInParagraph startPos)
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;//from www . ja va2 s . 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:org.kino.server.api.contractgenerator.java
static private long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> xwpfParagraphs) { long count = 0; for (XWPFParagraph paragraph : xwpfParagraphs) { List<XWPFRun> runs = paragraph.getRuns(); for (Map.Entry<String, String> replPair : replacements.entrySet()) { String find = replPair.getKey(); String repl = replPair.getValue(); TextSegement found = paragraph.searchText(find, new PositionInParagraph()); if (found != null) { count++;/* ww w . j a v a2 s . c om*/ if (found.getBeginRun() == found.getEndRun()) { // whole search string is in one Run XWPFRun run = runs.get(found.getBeginRun()); String runText = run.getText(run.getTextPosition()); String replaced = runText.replace(find, repl); run.setText(replaced, 0); } 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(find, repl); // The first Run receives the replaced String of all connected Runs XWPFRun partOne = runs.get(found.getBeginRun()); 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); } } } } } return count; }