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

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

Introduction

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

Prototype

@Deprecated
public XWPFRun(CTR r, XWPFParagraph p) 

Source Link

Usage

From source file:apachepoitest.XWPFParagraphClone.java

License:Apache License

/**
  * Identifies (in order) the parts of the paragraph /
  *  sub-paragraph that correspond to character text
  *  runs, and builds the appropriate runs for these.
  *///from  www  . ja v a 2 s .  co  m
private void buildRunsInOrderFromXml(XmlObject object) {
    XmlCursor c = object.newCursor();
    c.selectPath("child::*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTR) {
            XWPFRun r = new XWPFRun((CTR) o, this);
            runs.add(r);
            iruns.add(r);
        }
        if (o instanceof CTHyperlink) {
            CTHyperlink link = (CTHyperlink) o;
            for (CTR r : link.getRArray()) {
                XWPFHyperlinkRun hr = new XWPFHyperlinkRun(link, r, this);
                runs.add(hr);
                iruns.add(hr);
            }
        }
        if (o instanceof CTSdtBlock) {
            XWPFSDT cc = new XWPFSDT((CTSdtBlock) o, part);
            iruns.add(cc);
        }
        if (o instanceof CTSdtRun) {
            XWPFSDT cc = new XWPFSDT((CTSdtRun) o, part);
            iruns.add(cc);
        }
        if (o instanceof CTRunTrackChange) {
            for (CTR r : ((CTRunTrackChange) o).getRArray()) {
                XWPFRun cr = new XWPFRun(r, this);
                runs.add(cr);
                iruns.add(cr);
            }
        }
        if (o instanceof CTSimpleField) {
            for (CTR r : ((CTSimpleField) o).getRArray()) {
                XWPFRun cr = new XWPFRun(r, this);
                runs.add(cr);
                iruns.add(cr);
            }
        }
        if (o instanceof CTSmartTagRun) {
            // Smart Tags can be nested many times. 
            // This implementation does not preserve the tagging information
            buildRunsInOrderFromXml(o);
        }
    }
    c.dispose();
}

From source file:apachepoitest.XWPFParagraphClone.java

License:Apache License

/**
 * Appends a new run to this paragraph// w  w w.j  a va  2s . com
 *
 * @return a new text run
 */
public XWPFRun createRun() {
    XWPFRun xwpfRun = new XWPFRun(paragraph.addNewR(), this);
    runs.add(xwpfRun);
    iruns.add(xwpfRun);
    return xwpfRun;
}

From source file:apachepoitest.XWPFParagraphClone.java

License:Apache License

/**
 * insert a new Run in RunArray//w w w . j a  v  a  2s  .co m
 * @param pos
 * @return  the inserted run
 */
public XWPFRun insertNewRun(int pos) {
    if (pos >= 0 && pos <= paragraph.sizeOfRArray()) {
        CTR ctRun = paragraph.insertNewR(pos);
        XWPFRun newRun = new XWPFRun(ctRun, this);

        // To update the iruns, find where we're going
        // in the normal runs, and go in there
        int iPos = iruns.size();
        if (pos < runs.size()) {
            XWPFRun oldAtPos = runs.get(pos);
            int oldAt = iruns.indexOf(oldAtPos);
            if (oldAt != -1) {
                iPos = oldAt;
            }
        }
        iruns.add(iPos, newRun);

        // Runs itself is easy to update
        runs.add(pos, newRun);

        return newRun;
    }
    return null;
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

private void visitRuns(XWPFParagraph paragraph, T paragraphContainer) throws Exception {
    boolean fldCharTypeParsing = false;
    boolean pageNumber = false;
    String url = null;//from   ww  w .ja  va2  s .  com
    List<XmlObject> rListAfterSeparate = null;

    CTP ctp = paragraph.getCTP();
    XmlCursor c = ctp.newCursor();
    c.selectPath("child::*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTR) {
            /*
             * Test if it's : <w:r> <w:rPr /> <w:fldChar w:fldCharType="begin" /> </w:r>
             */
            CTR r = (CTR) o;
            STFldCharType.Enum fldCharType = XWPFRunHelper.getFldCharType(r);
            if (fldCharType != null) {
                if (fldCharType.equals(STFldCharType.BEGIN)) {
                    process(paragraph, paragraphContainer, pageNumber, url, rListAfterSeparate);
                    fldCharTypeParsing = true;
                    rListAfterSeparate = new ArrayList<XmlObject>();
                    pageNumber = false;
                    url = null;
                } else if (fldCharType.equals(STFldCharType.END)) {

                    process(paragraph, paragraphContainer, pageNumber, url, rListAfterSeparate);
                    fldCharTypeParsing = false;
                    rListAfterSeparate = null;
                    pageNumber = false;
                    processingTotalPageCountField = false;
                    url = null;
                }
            } else {
                if (fldCharTypeParsing) {
                    String instrText = XWPFRunHelper.getInstrText(r);
                    if (instrText != null) {
                        if (StringUtils.isNotEmpty(instrText)) {
                            // test if it's <w:r><w:instrText>PAGE</w:instrText></w:r>
                            boolean instrTextPage = XWPFRunHelper.isInstrTextPage(instrText);
                            if (!instrTextPage) {

                                // test if it's <w:r><w:instrText>NUMPAGES</w:instrText></w:r>
                                processingTotalPageCountField = XWPFRunHelper.isInstrTextNumpages(instrText);
                                if (!totalPageFieldUsed) {
                                    totalPageFieldUsed = true;
                                }

                                // test if it's <w:instrText>HYPERLINK
                                // "http://code.google.com/p/xdocrepor"</w:instrText>
                                String instrTextHyperlink = XWPFRunHelper.getInstrTextHyperlink(instrText);
                                if (instrTextHyperlink != null) {
                                    // test if it's <w:instrText>HYPERLINK \l _Toc29586</w:instrText>
                                    if (instrTextHyperlink.startsWith("\\l ")) {
                                        url = "#" + instrTextHyperlink.substring(3);
                                    } else {
                                        url = instrTextHyperlink;
                                    }
                                }
                            } else {
                                pageNumber = true;
                            }
                        }
                    } else {
                        rListAfterSeparate.add(r);
                    }
                } else {
                    XWPFRun run = new XWPFRun(r, paragraph);
                    visitRun(run, false, null, paragraphContainer);
                }
            }
        } else {
            if (fldCharTypeParsing) {
                rListAfterSeparate.add(o);
            } else {
                visitRun(paragraph, o, paragraphContainer);
            }
        }
    }
    c.dispose();
    process(paragraph, paragraphContainer, pageNumber, url, rListAfterSeparate);
    fldCharTypeParsing = false;
    rListAfterSeparate = null;
    pageNumber = false;
    url = null;
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

private void process(XWPFParagraph paragraph, T paragraphContainer, boolean pageNumber, String url,
        List<XmlObject> rListAfterSeparate) throws Exception {
    if (rListAfterSeparate != null) {
        for (XmlObject oAfterSeparate : rListAfterSeparate) {
            if (oAfterSeparate instanceof CTR) {
                CTR ctr = (CTR) oAfterSeparate;
                XWPFRun run = new XWPFRun(ctr, paragraph);
                visitRun(run, pageNumber, url, paragraphContainer);
            } else {
                visitRun(paragraph, oAfterSeparate, paragraphContainer);
            }/* w  w w  .j  av a 2  s. c  om*/
        }
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

private void visitRun(XWPFParagraph paragraph, XmlObject o, T paragraphContainer) throws Exception {
    if (o instanceof CTHyperlink) {
        CTHyperlink link = (CTHyperlink) o;
        String anchor = link.getAnchor();
        String href = null;//from   w w  w  .j a  v  a  2s . co m
        // Test if the is an id for hyperlink
        String hyperlinkId = link.getId();
        if (StringUtils.isNotEmpty(hyperlinkId)) {
            XWPFHyperlink hyperlink = document.getHyperlinkByID(hyperlinkId);
            href = hyperlink != null ? hyperlink.getURL() : null;
        }
        for (CTR r : link.getRList()) {
            XWPFRun run = new XWPFHyperlinkRun(link, r, paragraph);
            visitRun(run, false, href != null ? href : "#" + anchor, paragraphContainer);
        }
    } else if (o instanceof CTSdtRun) {
        CTSdtContentRun run = ((CTSdtRun) o).getSdtContent();
        for (CTR r : run.getRList()) {
            XWPFRun ru = new XWPFRun(r, paragraph);
            visitRun(ru, false, null, paragraphContainer);
        }
    } else if (o instanceof CTRunTrackChange) {
        for (CTR r : ((CTRunTrackChange) o).getRList()) {
            XWPFRun run = new XWPFRun(r, paragraph);
            visitRun(run, false, null, paragraphContainer);
        }
    } else if (o instanceof CTSimpleField) {
        CTSimpleField simpleField = (CTSimpleField) o;
        String instr = simpleField.getInstr();
        // 1) test if it's page number
        // <w:fldSimple w:instr=" PAGE \* MERGEFORMAT "> <w:r> <w:rPr> <w:noProof/>
        // </w:rPr> <w:t>- 1 -</w:t> </w:r> </w:fldSimple>
        boolean fieldPageNumber = XWPFRunHelper.isInstrTextPage(instr);
        String fieldHref = null;
        if (!fieldPageNumber) {
            // not page number, test if it's hyperlink :
            // <w:instrText>HYPERLINK "http://code.google.com/p/xdocrepor"</w:instrText>
            fieldHref = XWPFRunHelper.getInstrTextHyperlink(instr);
        }
        for (CTR r : simpleField.getRList()) {
            XWPFRun run = new XWPFRun(r, paragraph);
            visitRun(run, fieldPageNumber, fieldHref, paragraphContainer);
        }
    } else if (o instanceof CTSmartTagRun) {
        // Smart Tags can be nested many times.
        // This implementation does not preserve the tagging information
        // buildRunsInOrderFromXml(o);
    } else if (o instanceof CTBookmark) {
        CTBookmark bookmark = (CTBookmark) o;
        visitBookmark(bookmark, paragraph, paragraphContainer);
    }
}