Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getParagraphText

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getParagraphText

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getParagraphText.

Prototype

public String getParagraphText() 

Source Link

Document

Returns the text of the paragraph, but not of any objects in the paragraph

Usage

From source file:apachepoitest.DocumentPropertyChecker.java

public static Map<String, HashMap> checkIfStringsExistInParagraph(XWPFParagraph p, List<String> sl) {
    Map<String, HashMap> results = new HashMap();
    for (String s : sl) {
        results.put(s, new HashMap());
        results.get(s).put("EXISTS", p.getParagraphText().contains(s));
    }//from w ww  .j  av  a  2s  . c o m
    return results;
}

From source file:apachepoitest.DocumentPropertyChecker.java

public static Map<String, HashMap> checkRunPropertiesOfParagraphs(List<XWPFParagraph> pl, ArrayList<String> sl,
        Map<String, String> properties) {
    Map<String, HashMap> results = new HashMap<>(), tempMap = new HashMap<>();
    ArrayList tempList;/*from w w  w  . ja  v a 2s . c o m*/
    String removeString = "";

    // Initialize results, strings which were not found in the document are left as EXISTS : false
    for (String s : sl) {
        results.put(s, new HashMap<>());
        results.get(s).put("EXISTS", false);
    }

    for (XWPFParagraph p : pl) {
        for (String s : sl) {
            tempMap = null;
            //Will fail on typos, but pass on extra elements before or after string of interest
            //Need to change for typo toleration and exactness?
            if (p.getParagraphText().contains(s)) {
                tempList = new ArrayList();
                tempList.add(s);
                tempMap = checkPropertiesofParagraphRuns(p, tempList, properties);
                results.put(s, tempMap.get(s));
                removeString = s;
                break;
            }
        }
        //Remove string if it has been evaluated
        if (tempMap != null) {
            sl.remove(removeString);
        }
    }
    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   w  w w . j av a  2  s.  c  om*/
        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.DocumentPropertyChecker.java

public static Map<String, HashMap> checkPropertiesOfParagraphs(List<XWPFParagraph> pl, ArrayList<String> sl,
        Map<String, String> properties) {
    Map<String, HashMap> results = new HashMap<>(), tempMap = new HashMap<>();
    ArrayList tempList;/*from w ww.  j a  va 2  s.  c  o m*/
    String removeString = "";

    // Initialize results, strings which were not found in the document are left as EXISTS : false
    for (String s : sl) {
        results.put(s, new HashMap<>());
        results.get(s).put("EXISTS", false);
    }

    for (XWPFParagraph p : pl) {
        for (String s : sl) {
            tempMap = null;
            //Will fail on typos, but pass on extra elements before or after string of interest
            //Need to change for typo toleration and exactness?
            if (p.getParagraphText().contains(s)) {
                tempMap = checkPropertiesofParagraph(p, s, properties);
                results.put(s, tempMap.get(s));
                removeString = s;
                break;
            }
        }
        //Remove string if it has been evaluated
        if (tempMap != null) {
            sl.remove(removeString);
        }
    }
    return results;
}

From source file:apachepoitest.DocumentPropertyChecker.java

public static Map<String, Object> checkPropertiesOfAllParagraphs(List<XWPFParagraph> pl,
        Map<String, String> properties) {
    Map<String, Object> results = new HashMap<>(), tempMap = new HashMap<>();
    ArrayList tempList;//w ww .j a  v a 2  s.  c om
    String removeString = "";

    int paragraph_count = 0;

    // Initialize results, properties which were not found in the document are left as 0
    for (String property : properties.keySet()) {
        results.put(property, 0);
    }

    for (XWPFParagraph p : pl) {
        if (p.getParagraphText().isEmpty()) {
            continue;
        }
        paragraph_count++;
        for (String property : properties.keySet()) {
            if (checkIfParagraphHasProperty(p, property, properties.get(property))) {
                results.put(property, (int) results.get(property) + 1);
            }
        }
    }
    for (String property : properties.keySet()) {
        results.put(property, results.get(property) + "/" + paragraph_count);
    }
    return results;
}

From source file:apachepoitest.DocumentPropertyChecker.java

public static Map<String, HashMap> checkIfStringExistsInParagraphs(List<XWPFParagraph> pl,
        ArrayList<String> sl) {
    Map<String, HashMap> result = new HashMap<>();
    List<String> removeStrings = new ArrayList();
    // Initialize results, properties which were not found in the document are left as 0
    for (String s : sl) {
        result.put(s, new HashMap());
        result.get(s).put("EXISTS", "0/0");
    }/*www  .  jav a  2  s . c  o  m*/
    for (XWPFParagraph p : pl) {
        if (p.getParagraphText().isEmpty()) {
            continue;
        }
        if (removeStrings.isEmpty()) {
            removeStrings = new ArrayList();
        }
        for (String s : sl) {
            if (p.getParagraphText().contains(s)) {
                result.get(s).put("EXISTS", "1/1");
                removeStrings.add(s);
            }
        }
        for (String s : removeStrings) {
            sl.remove(s);
        }
    }
    return result;
}

From source file:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphProperties(List<XWPFParagraph> lp) {
    int i1 = 1;//  ww w .j a va 2s  . c  om
    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:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphPropertiesOnly(List<XWPFParagraph> lp) {
    int i1 = 1;/* w ww  . 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("STYLE: " + p.getBody().getXWPFDocument().getStyles().getStyle(p.getStyleID()));

            XWPFParagraphClone pc;
            pc = new XWPFParagraphClone(p.getCTP(), p.getBody());

            System.out.println("SPACING VALUE: " + pc.getCTSpacing(false).getLine().floatValue() / 240);
            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>");
        }
    }
}

From source file:com.foc.msword.WordTemplateFillerResource.java

License:Apache License

private void fillParagraph(ExtendedWordDocument xWord, XWPFParagraph para) {
    if (para != null) {
        boolean replaceDone = true;
        while (replaceDone) {
            replaceDone = false;/*from   w  ww. ja v a 2s. c  o m*/

            String paragraphText = para.getParagraphText();
            if (paragraphText != null && (paragraphText.contains("$F{") || paragraphText.contains("$P{"))) {
                int dollarIndex = paragraphText.indexOf("$F{");
                int pIndex = paragraphText.indexOf("$P{");
                if (pIndex >= 0 && (pIndex < dollarIndex || dollarIndex < 0)) {
                    dollarIndex = pIndex;
                }

                if (dollarIndex >= 0) {
                    int endAcccolade = paragraphText.indexOf("}", dollarIndex);
                    if (endAcccolade >= 0) {
                        String toReplace = paragraphText.substring(dollarIndex, endAcccolade + 1);
                        String replaceWith = dataDictionary.resolveExpression(getFocData(), toReplace, true);
                        xWord.replaceInParagraph(para, toReplace, replaceWith);
                        replaceDone = true;
                    }
                }
            }
        }
    }
}

From source file:com.foc.msword.WordTemplateFillerResource.java

License:Apache License

private void fillTable(ExtendedWordDocument xWord, XWPFTable xwpfTable) {
    List<XWPFTableRow> tableRows = xwpfTable.getRows();

    XWPFTableRow rowToDuplicate = null;//w  ww  . j a v a  2  s.c o m
    String propertyName = null;
    FocList slaveList = null;

    //First we check if this row is to be duplicated [*]
    for (int r = 0; r < tableRows.size() && slaveList == null; r++) {
        XWPFTableRow xwpfTableRow = tableRows.get(r);

        List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
        for (XWPFTableCell xwpfTableCell : tableCells) {
            List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
            if (paragraphs != null) {
                for (int p = 0; p < paragraphs.size() && slaveList == null; p++) {
                    XWPFParagraph para = paragraphs.get(p);
                    String paragraphText = para.getParagraphText();
                    int listStartIndex = paragraphText.indexOf("_LIST[*].");
                    int dollarIndex = paragraphText.indexOf("$F{");
                    if (dollarIndex >= 0 && listStartIndex > dollarIndex) {
                        propertyName = paragraphText.substring(dollarIndex + 3, listStartIndex + 5);
                        Object res = getFocData().iFocData_getDataByPath(propertyName);
                        if (res instanceof FocList) {
                            slaveList = (FocList) res;
                            rowToDuplicate = xwpfTableRow;
                        }
                    }
                }
            }
        }
    }

    if (slaveList != null) {
        for (int i = 1; i < slaveList.size(); i++) {
            // Copying a existing table row? 
            CTRow ctRow = CTRow.Factory.newInstance();
            ctRow.set(rowToDuplicate.getCtRow());
            XWPFTableRow newRow = new XWPFTableRow(ctRow, xwpfTable);

            replaceCellTextStartWithIndex(xWord, rowToDuplicate, newRow, propertyName, i);

            xwpfTable.addRow(newRow);
        }

        replaceCellTextStartWithIndex(xWord, rowToDuplicate, rowToDuplicate, propertyName, 0);

    } else {
        tableRows = xwpfTable.getRows();
        for (int r = 0; r < tableRows.size(); r++) {
            XWPFTableRow xwpfTableRow = tableRows.get(r);
            List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
            for (XWPFTableCell xwpfTableCell : tableCells) {
                List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
                if (paragraphs != null) {
                    for (XWPFParagraph para : paragraphs) {
                        fillParagraph(xWord, para);
                    }
                }
            }
        }
    }
}