Example usage for org.apache.poi.xwpf.usermodel XWPFRun toString

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun toString

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns the string version of the text and the phonetic string

Usage

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   ww w  .  j  a  v  a  2s  . c  o  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.DocumentPropertyEnumerator.java

public static void showParagraphElementProperties(List<XWPFRun> rl) {
    System.out.println("\nELEMENTS: ");
    int counter = 1;
    for (XWPFRun r : rl) {
        if (r.toString().trim().length() > 0) {
            System.out.println("#" + counter++ + ": " + r.toString());
        } else {/*from w ww.j a va 2s  .  c  om*/
            //Ignore spaces, uncomment to display spaces and comment out "continue"
            //System.out.println("#" + counter++ + ": <SPACE>");
            continue;
        }
        if (r.getColor() != null) {
            System.out.println("COLOR: " + r.getColor());
        }
        if (r.getFontFamily() != null) {
            System.out.println("FONT: " + r.getFontFamily());
        }
        if (r.getFontSize() > 0) {
            System.out.println("FONT SIZE: " + r.getFontSize());
        }
        if (r.getPictureText().length() > 0) {
            System.out.println("PIC TEXT: " + r.getPictureText());
        }
        if (r.getTextPosition() > 0) {
            System.out.println("TEXT POS: " + r.getTextPosition());
        }
        if (r.isBold()) {
            System.out.println("BOLD: " + r.isBold());
        }
        if (r.isItalic()) {
            System.out.println("ITALIC: " + r.isItalic());
        }
        if (r.isStrike()) {
            System.out.println("STRIKETHROUGH: " + r.isStrike());
        }
        if (!r.getUnderline().toString().equals("NONE")) {
            System.out.println("UNDERLINE: " + r.getUnderline().toString());
        }
        if (!r.getSubscript().toString().equals("BASELINE")) {
            System.out.println("Subscript: " + r.getSubscript().toString());
        }
        System.out.println("");
    }
}

From source file:apachepoitest.XWPFParagraphClone.java

License:Apache License

/**
 * Returns the text of the paragraph, but not of any objects in the
 * paragraph// w  w w.  j  a v a 2s .  c o m
 */
public String getParagraphText() {
    StringBuffer out = new StringBuffer();
    for (XWPFRun run : runs) {
        out.append(run.toString());
    }
    return out.toString();
}

From source file:com.deepoove.poi.resolver.TemplateResolver.java

License:Apache License

private static void calcRunPosInParagraph(List<XWPFRun> runs, List<Pair<RunEdge, RunEdge>> pairs) {
    int size = runs.size(), pos = 0, calc = 0;
    Pair<RunEdge, RunEdge> pair = pairs.get(pos);
    RunEdge leftEdge = pair.getLeft();//from   w w  w.  j a v a  2s .  co m
    RunEdge rightEdge = pair.getRight();
    int leftInAll = leftEdge.getAllPos();
    int rightInAll = rightEdge.getAllPos();
    for (int i = 0; i < size; i++) {
        XWPFRun run = runs.get(i);
        String str = run.getText(0);
        if (null == str) {
            logger.warn("found the empty text run,may be produce bug:" + run);
            calc += run.toString().length();
            continue;
        }
        logger.debug(str);
        if (str.length() + calc < leftInAll) {
            calc += str.length();
            continue;
        }
        for (int j = 0; j < str.length(); j++) {
            if (calc + j == leftInAll) {
                leftEdge.setRunPos(i);
                leftEdge.setRunEdge(j);
                leftEdge.setText(str);
            }
            if (calc + j == rightInAll - 1) {
                rightEdge.setRunPos(i);
                rightEdge.setRunEdge(j);
                rightEdge.setText(str);

                if (pos == pairs.size() - 1)
                    break;
                pair = pairs.get(++pos);
                leftEdge = pair.getLeft();
                rightEdge = pair.getRight();
                leftInAll = leftEdge.getAllPos();
                rightInAll = rightEdge.getAllPos();
            }
        }
        calc += str.length();
    }
}

From source file:easyoffice.word.WordMaker.java

private static void replaceText(XWPFDocument doc, HashMap<String, String> data) {

    Set<String> keySet = data.keySet();

    for (String key : keySet) {
        for (XWPFParagraph p : doc.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();

            for (XWPFRun run : runs) {
                if (run.toString().toLowerCase().equals(key)) {
                    run.setText(data.get(key), 0);
                }//from   w  w  w.ja  va  2s . c o  m
            }
        }
    }
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java

License:Apache License

private TmpFormatting processRun(XWPFRun run, XWPFParagraph paragraph, XHTMLContentHandler xhtml,
        TmpFormatting tfmtg) throws SAXException, XmlException, IOException {
    // True if we are currently in the named style tag:
    if (run.isBold() != tfmtg.isBold()) {
        if (tfmtg.isItalic()) {
            xhtml.endElement("i");
            tfmtg.setItalic(false);//  ww w .j  a  va  2 s .com
        }
        if (run.isBold()) {
            xhtml.startElement("b");
        } else {
            xhtml.endElement("b");
        }
        tfmtg.setBold(run.isBold());
    }

    if (run.isItalic() != tfmtg.isItalic()) {
        if (run.isItalic()) {
            xhtml.startElement("i");
        } else {
            xhtml.endElement("i");
        }
        tfmtg.setItalic(run.isItalic());
    }

    boolean addedHREF = false;
    if (run instanceof XWPFHyperlinkRun) {
        XWPFHyperlinkRun linkRun = (XWPFHyperlinkRun) run;
        XWPFHyperlink link = linkRun.getHyperlink(document);
        if (link != null && link.getURL() != null) {
            xhtml.startElement("a", "href", link.getURL());
            addedHREF = true;
        } else if (linkRun.getAnchor() != null && linkRun.getAnchor().length() > 0) {
            xhtml.startElement("a", "href", "#" + linkRun.getAnchor());
            addedHREF = true;
        }
    }

    xhtml.characters(run.toString());

    // If we have any pictures, output them
    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        if (paragraph.getDocument() != null) {
            XWPFPictureData data = picture.getPictureData();
            if (data != null) {
                AttributesImpl attr = new AttributesImpl();

                attr.addAttribute("", "src", "src", "CDATA", "embedded:" + data.getFileName());
                attr.addAttribute("", "alt", "alt", "CDATA", picture.getDescription());

                xhtml.startElement("img", attr);
                xhtml.endElement("img");
            }
        }
    }

    if (addedHREF) {
        xhtml.endElement("a");
    }

    return tfmtg;
}

From source file:org.articleEditor.insertContent.POIDocxReader.java

License:Apache License

protected void processRun(XWPFRun run) throws BadLocationException {
    charAttrs = new SimpleAttributeSet();

    if (run.getFontSize() > 0) {
        int size = run.getFontSize();
        StyleConstants.setFontSize(charAttrs, size);
    }//from  w ww.j ava 2 s .co  m
    StyleConstants.setBold(charAttrs, run.isBold());
    StyleConstants.setItalic(charAttrs, run.isItalic());
    StyleConstants.setStrikeThrough(charAttrs, run.isStrike());
    boolean underlined = run.getUnderline() != UnderlinePatterns.NONE;
    StyleConstants.setUnderline(charAttrs, underlined);
    VerticalAlign verticalAlignment = run.getSubscript();
    if (verticalAlignment == VerticalAlign.SUBSCRIPT) {
        StyleConstants.setSubscript(parAttrs, true);
    } else if (verticalAlignment == VerticalAlign.SUPERSCRIPT) {
        StyleConstants.setSuperscript(parAttrs, true);
    } else {
        StyleConstants.setSubscript(parAttrs, false);
        StyleConstants.setSuperscript(parAttrs, false);
    }
    if (run.getFontFamily() != null) {
        StyleConstants.setFontFamily(charAttrs, run.getFontFamily());
    }
    if (run.getColor() != null) {
        String name = run.getColor();
        if (!name.toLowerCase().equals("auto")) {
            Color color = Color.decode("#" + name);
            StyleConstants.setForeground(charAttrs, color);
        }
    }

    // Not working
    if (run.getCTR().getRPr() != null && run.getCTR().getRPr().getHighlight() != null) {
        STHighlightColor.Enum colorEnum = run.getCTR().getRPr().getHighlight().getVal();
        Color color = decodeHighlightName(colorEnum);
        StyleConstants.setBackground(charAttrs, color);
    }

    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        processPicture(picture);
    }
    String text = run.toString();
    document.insertString(currentOffset, text, charAttrs);
    currentOffset += text.length();
}