List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns
public List<XWPFRun> getRuns()
From source file:apachepoitest.DocumentPropertyChecker.java
public static Map<String, HashMap> checkPropertiesofParagraphRuns(XWPFParagraph p, ArrayList<String> sl, Map<String, String> properties) { List<XWPFRun> rl = p.getRuns(); Map<String, HashMap> results; //Check first if elements in sl are in p results = checkIfStringsExistInParagraph(p, sl); //Initialize counts to 0 for (String s : sl) { for (String property : properties.keySet()) { results.get(s).put(property, 0); }/*from w ww. jav a 2 s .co m*/ } //For each existing string, for (XWPFRun r : rl) { //Skip run if empty string if (r.toString().isEmpty()) { continue; } for (String s : sl) { //Skip string if it does't exist if (results.get(s).get("EXISTS").equals(true)) { //For each property, check if it applies to the run for (String property : properties.keySet()) { if (checkIfRunHasProperty(r, property, properties.get(property))) { results.get(s).put(property, (int) results.get(s).get(property) + 1); } } } } } //Count only runs which are not empty for scoring int total_runs = 0; for (XWPFRun r : rl) { if (!r.toString().isEmpty()) { total_runs++; } } //Transform results to score for (String s : sl) { for (String property : properties.keySet()) { results.get(s).put(property, Integer.toString((int) results.get(s).get(property)) + "/" + total_runs); } } return results; }
From source file:apachepoitest.DocumentPropertyChecker.java
public static Map<String, HashMap> checkPropertiesofParagraph(XWPFParagraph p, String s, Map<String, String> properties) { List<XWPFRun> rl = p.getRuns(); Map<String, HashMap> results = new HashMap(); results.put(s, new HashMap()); //Check first if elements in sl are in p if (p.getParagraphText().contains(s)) { results.get(s).put("EXISTS", true); } else {/*from ww w .j av a 2 s. c o m*/ results.get(s).put("EXISTS", false); return results; } //Initialize counts to 0 for (String property : properties.keySet()) { results.get(s).put(property, "0/1"); } //For each existing string, for (String property : properties.keySet()) { if (checkIfParagraphHasProperty(p, property, properties.get(property))) { results.get(s).put(property, "1/1"); } } return results; }
From source file:apachepoitest.DocumentPropertyEnumerator.java
public static void showParagraphProperties(List<XWPFParagraph> lp) { int i1 = 1;/*from w w w . j a v a 2s . c o m*/ for (XWPFParagraph p : lp) { //System.out.println(p.getStyleID() + " " + sl1.getStyle(p.getStyleID()).getCTStyle().xmlText()); System.out.println("____________________________________"); if (p.getParagraphText().trim().length() > 0) { System.out.println("\n#" + i1++ + " LINE: " + p.getParagraphText()); System.out.println("ALIGNMENT: " + p.getAlignment().toString()); //Uncomment to display other properties /* System.out.println("BORDER BETWEEN: " + p.getBorderBetween().toString()); System.out.println("BORDER BOTTOM: " + p.getBorderBottom().toString()); System.out.println("BORDER LEFT: " + p.getBorderLeft().toString()); System.out.println("BORDER RIGHT: " + p.getBorderRight().toString()); System.out.println("BORDER TOP: " + p.getBorderTop().toString()); System.out.println("BODY ELEMENT TYPE: " + p.getElementType().toString()); System.out.println("FOOTNOTE: " + p.getFootnoteText()); System.out.println("INDENTATION 1ST LINE: " + p.getIndentationFirstLine()); System.out.println("INDENTATION HANGING: " + p.getIndentationHanging()); System.out.println("INDENTATION LEFT: " + p.getIndentationLeft()); System.out.println("INDENTATION RIGHT: " + p.getIndentationRight()); System.out.println("NUMBERING FORMAT: " + p.getNumFmt()); System.out.println("NUMERIC STYLE ILVL: " + p.getNumIlvl()); System.out.println("SPACING AFTER: " + p.getSpacingAfter()); System.out.println("SPACING AFTER LINES: " + p.getSpacingAfterLines()); System.out.println("SPACING BEFORE: " + p.getSpacingBefore()); System.out.println("SPACING BEFORE LINES: " + p.getSpacingBeforeLines()); System.out.println("SPACING LINE RULE: " + p.getSpacingLineRule()); System.out.println("VERTICAL ALIGNMENT: " + p.getVerticalAlignment()); */ } // can also use .searchText to look for a string else { // Uncomment to display lines //System.out.println("\n#" + i1++ + " LINE: <SPACE>"); } showParagraphElementProperties(p.getRuns()); } }
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 w w w.ja va 2 s.c o 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
private void processBookmarks2Paragraph(List<IDocumentBookmark> arrBookmarks, XWPFParagraph paraCurrent) { for (XWPFRun runCurrent : paraCurrent.getRuns()) { processBookmarks2Run(runCurrent, arrBookmarks); }/*from w ww. ja va 2 s . co m*/ }
From source file:biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.java
License:Apache License
public static void setDocCellValue(XWPFTable dxTable, int nRow, int nCol, Object objValue, int maxRow, boolean isHeader) { try {/*from w ww . j a va 2s .co m*/ if (dxTable.getRow(nRow) == null) { // DEFINIE MAX VALUE! while (dxTable.getRow(nRow) == null && dxTable.getRows().size() < maxRow) { dxTable.createRow(); // rowHasChanged = true; } } if (dxTable.getRow(nRow) != null) { if (dxTable.getRow(nRow).getCell(nCol) == null) { // CHECK MAX COL while (dxTable.getRow(nRow).getCell(nCol) == null && dxTable.getRow(nRow).getTableCells().size() < 50) { dxTable.getRow(nRow).addNewTableCell(); } } // dxTable.getRow(nRow).getCell(nCol).setText("" + // objValue.toString()); for (XWPFParagraph paraCurrent : dxTable.getRow(nRow).getCell(nCol).getParagraphs()) { if (paraCurrent.getRuns().size() == 0) { XWPFRun runCurrent = paraCurrent.createRun(); if (isHeader) runCurrent.setBold(true); runCurrent.setText("" + objValue.toString()); } else { for (XWPFRun runCurrent : paraCurrent.getRuns()) { if (isHeader) runCurrent.setBold(true); runCurrent.setText("" + objValue.toString()); } } } } else { System.out.println("Still null: " + nRow + " MaxRow = " + maxRow); } } catch (Exception e) { e.printStackTrace(); } }
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 a va2s. 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 .j a v a2 s .c o 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:cn.afterturn.easypoi.word.parse.excel.ExcelEntityParse.java
License:Apache License
/** * word?/*w w w .j a v a2 s .c om*/ * * @param row * @param value */ private void setWordText(XWPFTableRow row, Object value) { XWPFTableCell cell = row.createCell(); List<XWPFParagraph> paragraphs = cell.getParagraphs(); XWPFParagraph paragraph = null; XWPFRun run = null; if (paragraphs != null && paragraphs.size() > 0) { paragraph = paragraphs.get(0); } else { paragraph = row.createCell().addParagraph(); } List<XWPFRun> runs = paragraph.getRuns(); if (runs != null && runs.size() > 0) { run = runs.get(0); } else { run = paragraph.createRun(); } if (value instanceof ImageEntity) { ExcelMapParse.addAnImage((ImageEntity) value, run); } else { PoiPublicUtil.setWordText(run, value == null ? "" : value.toString()); } }
From source file:cn.afterturn.easypoi.word.parse.ParseWord07.java
License:Apache License
/** * ???/*from www. j av a 2 s . co m*/ * * @param map * @author JueYue * 2013-11-16 */ private void changeValues(XWPFParagraph paragraph, XWPFRun currentRun, String currentText, List<Integer> runIndex, Map<String, Object> map) throws Exception { Object obj = PoiPublicUtil.getRealValue(currentText, map); if (obj instanceof ImageEntity) {// currentRun.setText("", 0); ExcelMapParse.addAnImage((ImageEntity) obj, currentRun); } else { currentText = obj.toString(); PoiPublicUtil.setWordText(currentRun, currentText); } for (int k = 0; k < runIndex.size(); k++) { paragraph.getRuns().get(runIndex.get(k)).setText("", 0); } runIndex.clear(); }