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

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

Introduction

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

Prototype

public List<XWPFRun> getRuns() 

Source Link

Usage

From source file:javaapplication1.AnotherPOI.java

public static void replaceText(String findText, String replaceText) {
    try {/*from  ww  w . j  a v a2 s.c o m*/
        XWPFDocument doc = new XWPFDocument(OPCPackage.open("D:\\template.docx"));
        for (XWPFParagraph p : doc.getParagraphs()) {

            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    if (text != null && text.contains(findText)) {
                        text = text.replace(findText, replaceText);
                        r.setText(text, 0);
                    }
                }
            }
        }
        for (XWPFTable tbl : doc.getTables()) {
            for (XWPFTableRow row : tbl.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph p : cell.getParagraphs()) {
                        for (XWPFRun r : p.getRuns()) {
                            String text = r.getText(0);
                            if (text.contains(findText)) {
                                text = text.replace(findText, replaceText);
                                r.setText(text);
                            }
                        }
                    }
                }
            }
        }
        doc.write(new FileOutputStream("D:\\result.docx"));
    } catch (IOException ex) {
        Logger.getLogger(AnotherPOI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidFormatException ex) {
        Logger.getLogger(AnotherPOI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

License:Apache License

private void extractParagraph(XWPFParagraph paragraph, XHTMLContentHandler xhtml)
        throws SAXException, XmlException, IOException {
    // If this paragraph is actually a whole new section, then
    // it could have its own headers and footers
    // Check and handle if so
    XWPFHeaderFooterPolicy headerFooterPolicy = null;
    if (paragraph.getCTP().getPPr() != null) {
        CTSectPr ctSectPr = paragraph.getCTP().getPPr().getSectPr();
        if (ctSectPr != null) {
            headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
            extractHeaders(xhtml, headerFooterPolicy);
        }/* ww  w  .  j av  a2  s . c o m*/
    }

    // Is this a paragraph, or a heading?
    String tag = "p";
    String styleClass = null;
    if (paragraph.getStyleID() != null) {
        XWPFStyle style = styles.getStyle(paragraph.getStyleID());

        if (style != null && style.getName() != null) {
            TagAndStyle tas = WordExtractor.buildParagraphTagAndStyle(style.getName(),
                    paragraph.getPartType() == BodyType.TABLECELL);
            tag = tas.getTag();
            styleClass = tas.getStyleClass();
        }
    }

    if (styleClass == null) {
        xhtml.startElement(tag);
    } else {
        xhtml.startElement(tag, "class", styleClass);
    }

    // Output placeholder for any embedded docs:

    // TODO: replace w/ XPath/XQuery:
    for (XWPFRun run : paragraph.getRuns()) {
        XmlCursor c = run.getCTR().newCursor();
        c.selectPath("./*");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof CTObject) {
                XmlCursor c2 = o.newCursor();
                c2.selectPath("./*");
                while (c2.toNextSelection()) {
                    XmlObject o2 = c2.getObject();

                    XmlObject embedAtt = o2.selectAttribute(new QName("Type"));
                    if (embedAtt != null && embedAtt.getDomNode().getNodeValue().equals("Embed")) {
                        // Type is "Embed"
                        XmlObject relIDAtt = o2.selectAttribute(new QName(
                                "http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"));
                        if (relIDAtt != null) {
                            String relID = relIDAtt.getDomNode().getNodeValue();
                            AttributesImpl attributes = new AttributesImpl();
                            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
                            attributes.addAttribute("", "id", "id", "CDATA", relID);
                            xhtml.startElement("div", attributes);
                            xhtml.endElement("div");
                        }
                    }
                }
                c2.dispose();
            }
        }

        c.dispose();
    }

    // Attach bookmarks for the paragraph
    // (In future, we might put them in the right place, for now
    // we just put them in the correct paragraph)
    for (CTBookmark bookmark : paragraph.getCTP().getBookmarkStartList()) {
        xhtml.startElement("a", "name", bookmark.getName());
        xhtml.endElement("a");
    }

    TmpFormatting fmtg = new TmpFormatting(false, false);

    // Do the iruns
    for (IRunElement run : paragraph.getIRuns()) {
        if (run instanceof XWPFSDT) {
            fmtg = closeStyleTags(xhtml, fmtg);
            processSDTRun((XWPFSDT) run, xhtml);
            // for now, we're ignoring formatting in sdt
            // if you hit an sdt reset to false
            fmtg.setBold(false);
            fmtg.setItalic(false);
        } else {
            fmtg = processRun((XWPFRun) run, paragraph, xhtml, fmtg);
        }
    }
    closeStyleTags(xhtml, fmtg);

    // Now do any comments for the paragraph
    XWPFCommentsDecorator comments = new XWPFCommentsDecorator(paragraph, null);
    String commentText = comments.getCommentText();
    if (commentText != null && commentText.length() > 0) {
        xhtml.characters(commentText);
    }

    String footnameText = paragraph.getFootnoteText();
    if (footnameText != null && footnameText.length() > 0) {
        xhtml.characters(footnameText + "\n");
    }

    // Also extract any paragraphs embedded in text boxes:
    for (XmlObject embeddedParagraph : paragraph.getCTP().selectPath(
            "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' .//*/wps:txbx/w:txbxContent/w:p")) {
        extractParagraph(new XWPFParagraph(CTP.Factory.parse(embeddedParagraph.xmlText()), paragraph.getBody()),
                xhtml);
    }

    // Finish this paragraph
    xhtml.endElement(tag);

    if (headerFooterPolicy != null) {
        extractFooters(xhtml, headerFooterPolicy);
    }
}

From source file:msoffice.WriteinTemplate.java

public static void main(String[] args) throws FileNotFoundException, InvalidFormatException, IOException {

    FileInputStream fis = new FileInputStream("H:\\OFICIOTEMPLATE.docx");
    XWPFDocument doc = new XWPFDocument(fis);

    for (XWPFParagraph p : doc.getParagraphs()) {

        for (XWPFRun r : p.getRuns()) {
            //System.out.println(r);
            String text = r.getText(0);

            System.out.println(text);
            if (text.contains("unidad")) {
                text = text.replace("unidad", "CICTE/W-6.a/02.00");

                r.setText(text, 0);//from  w w  w. ja  v  a2s  .c o m
                System.out.println(text);
            }

            if (text.contains("#receptor#")) {
                text = text.replace("#receptor#",
                        "Gral Brig Jefe del Servicio de Material de Guerra del Ejrcito");

                r.setText(text, 0);
                System.out.println(text);
            }

            if (text.contains("#asunto#")) {
                text = text.replace("#asunto#",
                        "Sobre artculo de MG (Armamento) y apoyo de elemento tcnico.");

                r.setText(text, 0);
                System.out.println(text);
            }

            if (text.contains("#referencia#")) {
                text = text.replace("#referencia#", "Oficio N289/CICTE del 01 julio de 2015.");

                r.setText(text, 0);
                System.out.println(text);
            }

            if (text.contains("#cuerpo#")) {
                text = text.replace("#cuerpo#",
                        "Tengo el honor de dirigirme a Ud., para manifestarle que en relacin a la solicitud de prstamo de una (01) ametralladora BROWNING Cal .50 y la participacin del elemento tcnico Tco 2da MAM Pacheco Tejada Henry, para las pruebas del vehculo blindado OTORONGO, las cuales se han suspendido y sern reprogramadas.\n"
                                + "Asimismo, se informar de manera oportuna la fecha de realizacin de las pruebas del vehculo blindado OTORONGO, para poder contar con artculo de MG (Armamento) y apoyo de elemento tcnico solicitado.\n"
                                + "Hago propicia la oportunidad para expresarle a Ud. los sentimientos de mi especial consideracin y estima personal.");

                r.setText(text, 0);
                System.out.println(text);
            }

        }

    }
    doc.write(new FileOutputStream("output.docx"));

}

From source file:offishell.msoffice.WordTestHelper.java

License:MIT License

/**
 * <p>/*from   w w w  .  j  a  v a2  s  .co m*/
 * Assertion helper.
 * </p>
 * 
 * @param para
 * @param value
 */
public default void checkParagraph(XWPFParagraph para, Assertion value) {
    List<XWPFRun> runs = para.getRuns();

    assert runs != null;
    assert value.of(runs.size());

    for (XWPFRun run : runs) {
        assert value.of(run, this::checkRun);
    }
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>//from w ww  .j  a va2 s. com
 * Helper method to remove all comments from the specified paragraph.
 * </p>
 * 
 * @param paragraph A target paragraph.
 */
public static void clearComment(XWPFParagraph paragraph) {
    if (paragraph != null) {
        CTP pContext = paragraph.getCTP();

        for (int i = pContext.sizeOfCommentRangeStartArray() - 1; 0 <= i; i--) {
            pContext.removeCommentRangeStart(i);
        }

        for (int i = pContext.sizeOfCommentRangeEndArray() - 1; 0 <= i; i--) {
            pContext.removeCommentRangeEnd(i);
        }

        for (XWPFRun run : paragraph.getRuns()) {
            CTR rContext = run.getCTR();

            for (int i = rContext.sizeOfCommentReferenceArray() - 1; 0 <= i; i--) {
                rContext.removeCommentReference(i);
            }
        }
    }
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>/*  w  ww.  j  a v  a 2  s.c  o  m*/
 * Helper method to remove all text from the specified paragraph.
 * </p>
 * 
 * @param paragraph A target paragraph.
 */
public static void clearText(XWPFParagraph paragraph) {
    if (paragraph != null) {
        for (XWPFRun run : paragraph.getRuns()) {
            run.setText("", 0);
        }
    }
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>/*from w  ww  .j  a v a 2 s  .c o  m*/
 * Helper method to clone {@link XWPFParagraph}.
 * </p>
 * 
 * @param in
 * @param out
 * @param converter
 */
public static void copy(XWPFParagraph in, XWPFParagraph out, UnaryOperator<String> converter) {
    // copy context
    ppr(out).set(in.getCTP().getPPr());

    // copy(doc, out.getDocument(), doc.getStyles().getStyle(in.getStyleID()));

    // copy children
    for (XWPFRun inRun : in.getRuns()) {
        copy(inRun, out.createRun(), converter);
    }
}

From source file:org.apache.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java

License:Apache License

private void extractParagraph(XWPFParagraph paragraph, XWPFListManager listManager, XHTMLContentHandler xhtml)
        throws SAXException, XmlException, IOException {
    // If this paragraph is actually a whole new section, then
    //  it could have its own headers and footers
    // Check and handle if so
    XWPFHeaderFooterPolicy headerFooterPolicy = null;
    if (paragraph.getCTP().getPPr() != null) {
        CTSectPr ctSectPr = paragraph.getCTP().getPPr().getSectPr();
        if (ctSectPr != null) {
            headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
            extractHeaders(xhtml, headerFooterPolicy, listManager);
        }/*ww w. ja v a2 s  .c  o  m*/
    }

    // Is this a paragraph, or a heading?
    String tag = "p";
    String styleClass = null;
    if (paragraph.getStyleID() != null) {
        XWPFStyle style = styles.getStyle(paragraph.getStyleID());

        if (style != null && style.getName() != null) {
            TagAndStyle tas = WordExtractor.buildParagraphTagAndStyle(style.getName(),
                    paragraph.getPartType() == BodyType.TABLECELL);
            tag = tas.getTag();
            styleClass = tas.getStyleClass();
        }
    }

    if (styleClass == null) {
        xhtml.startElement(tag);
    } else {
        xhtml.startElement(tag, "class", styleClass);
    }

    writeParagraphNumber(paragraph, listManager, xhtml);
    // Output placeholder for any embedded docs:

    // TODO: replace w/ XPath/XQuery:
    for (XWPFRun run : paragraph.getRuns()) {
        XmlCursor c = run.getCTR().newCursor();
        c.selectPath("./*");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof CTObject) {
                XmlCursor c2 = o.newCursor();
                c2.selectPath("./*");
                while (c2.toNextSelection()) {
                    XmlObject o2 = c2.getObject();

                    XmlObject embedAtt = o2.selectAttribute(new QName("Type"));
                    if (embedAtt != null && embedAtt.getDomNode().getNodeValue().equals("Embed")) {
                        // Type is "Embed"
                        XmlObject relIDAtt = o2.selectAttribute(new QName(
                                "http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"));
                        if (relIDAtt != null) {
                            String relID = relIDAtt.getDomNode().getNodeValue();
                            AttributesImpl attributes = new AttributesImpl();
                            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
                            attributes.addAttribute("", "id", "id", "CDATA", relID);
                            xhtml.startElement("div", attributes);
                            xhtml.endElement("div");
                        }
                    }
                }
                c2.dispose();
            }
        }

        c.dispose();
    }

    // Attach bookmarks for the paragraph
    // (In future, we might put them in the right place, for now
    //  we just put them in the correct paragraph)
    for (int i = 0; i < paragraph.getCTP().sizeOfBookmarkStartArray(); i++) {
        CTBookmark bookmark = paragraph.getCTP().getBookmarkStartArray(i);
        xhtml.startElement("a", "name", bookmark.getName());
        xhtml.endElement("a");
    }

    TmpFormatting fmtg = new TmpFormatting(false, false);

    // Do the iruns
    for (IRunElement run : paragraph.getIRuns()) {
        if (run instanceof XWPFSDT) {
            fmtg = closeStyleTags(xhtml, fmtg);
            processSDTRun((XWPFSDT) run, xhtml);
            //for now, we're ignoring formatting in sdt
            //if you hit an sdt reset to false
            fmtg.setBold(false);
            fmtg.setItalic(false);
        } else {
            fmtg = processRun((XWPFRun) run, paragraph, xhtml, fmtg);
        }
    }
    closeStyleTags(xhtml, fmtg);

    // Now do any comments for the paragraph
    XWPFCommentsDecorator comments = new XWPFCommentsDecorator(paragraph, null);
    String commentText = comments.getCommentText();
    if (commentText != null && commentText.length() > 0) {
        xhtml.characters(commentText);
    }

    String footnameText = paragraph.getFootnoteText();
    if (footnameText != null && footnameText.length() > 0) {
        xhtml.characters(footnameText + "\n");
    }

    // Also extract any paragraphs embedded in text boxes:
    for (XmlObject embeddedParagraph : paragraph.getCTP().selectPath(
            "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' .//*/wps:txbx/w:txbxContent/w:p")) {
        extractParagraph(new XWPFParagraph(CTP.Factory.parse(embeddedParagraph.xmlText()), paragraph.getBody()),
                listManager, xhtml);
    }

    // Finish this paragraph
    xhtml.endElement(tag);

    if (headerFooterPolicy != null) {
        extractFooters(xhtml, headerFooterPolicy, listManager);
    }
}

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

License:Apache License

DocumentPosition searchParagraph(XWPFParagraph paragraph, int offset) throws BadLocationException {
    int runIndex = 0;
    for (XWPFRun run : paragraph.getRuns()) {
        DocumentPosition position = searchRun(run, offset);
        if (position != null) {
            position.positionInParagraph = runIndex;
            return position;
        }//from   w  ww.j  a  v a  2 s  .c  o  m
        runIndex++;
    }
    return null;
}

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

License:Apache License

protected void processParagraph(XWPFParagraph paragraph) throws BadLocationException {
    parAttrs = new SimpleAttributeSet();
    ParagraphAlignment alignment = paragraph.getAlignment();
    if (alignment == ParagraphAlignment.CENTER) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_CENTER);
    } else if (alignment == ParagraphAlignment.LEFT) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_LEFT);
    } else if (alignment == ParagraphAlignment.RIGHT) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_RIGHT);
    } else if (alignment == ParagraphAlignment.BOTH || alignment == ParagraphAlignment.DISTRIBUTE) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_JUSTIFIED);
    }//from  w w w .ja  v a 2s  . c o  m
    List<TabStop> tabs = new ArrayList<>();
    int leftIndentation = paragraph.getIndentationLeft();
    if (leftIndentation > 0) {
        float indentation = leftIndentation / INDENTS_MULTIPLIER;
        StyleConstants.setLeftIndent(parAttrs, indentation);
        /*TabStop stop = new TabStop(pos, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
         tabs.add(stop);*/
    }
    int rightIndentation = paragraph.getIndentationRight();
    if (rightIndentation > 0) {
        float indentation = rightIndentation / INDENTS_MULTIPLIER;
        StyleConstants.setLeftIndent(parAttrs, indentation);
        /*TabStop stop = new TabStop(pos, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
         tabs.add(stop);*/
    }
    /*TabSet tabSet = new TabSet(tabs.toArray(new TabStop[tabs.size()]));
     StyleConstants.setTabSet(parAttrs, tabSet);*/
    int firstLineIndentation = paragraph.getIndentationFirstLine();
    if (firstLineIndentation > 0) {
        float indentation = firstLineIndentation / INDENTS_MULTIPLIER;
        StyleConstants.setFirstLineIndent(parAttrs, indentation);
        /*TabStop stop = new TabStop(pos, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
         tabs.add(stop);*/
    }

    int spacingBefore = paragraph.getSpacingBefore();
    if (spacingBefore > 0) {
        int before = spacingBefore / INDENTS_MULTIPLIER;
        StyleConstants.setSpaceAbove(parAttrs, before);
    }
    int spacingAfter = paragraph.getSpacingAfter();
    if (spacingAfter > 0) {
        int after = spacingAfter / INDENTS_MULTIPLIER;
        StyleConstants.setSpaceAbove(parAttrs, after);
    }
    LineSpacingRule spacingLine = paragraph.getSpacingLineRule();
    if (spacingLine == LineSpacingRule.AT_LEAST || spacingLine == LineSpacingRule.AUTO) {
        float spacing = spacingLine.getValue() / 240;
        StyleConstants.setLineSpacing(parAttrs, spacing);
    }
    document.setParagraphAttributes(currentOffset, 1, parAttrs, true);
    for (XWPFRun run : paragraph.getRuns()) {
        processRun(run);
    }
}