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:cn.afterturn.easypoi.word.parse.ParseWord07.java

License:Apache License

/**
 * ??//from   www.ja  v  a2  s.  co m
 *
 * @param paragraph
 * @param map
 * @author JueYue
 * 2013-11-16
 */
private void parseThisParagraph(XWPFParagraph paragraph, Map<String, Object> map) throws Exception {
    XWPFRun run;
    XWPFRun currentRun = null;// run,?set,???
    String currentText = "";// ?text
    String text;
    Boolean isfinde = false;// ???{{
    List<Integer> runIndex = new ArrayList<Integer>();// ?run,
    for (int i = 0; i < paragraph.getRuns().size(); i++) {
        run = paragraph.getRuns().get(i);
        text = run.getText(0);
        if (StringUtils.isEmpty(text)) {
            continue;
        } // ""?
        if (isfinde) {
            currentText += text;
            if (currentText.indexOf(START_STR) == -1) {
                isfinde = false;
                runIndex.clear();
            } else {
                runIndex.add(i);
            }
            if (currentText.indexOf(END_STR) != -1) {
                changeValues(paragraph, currentRun, currentText, runIndex, map);
                currentText = "";
                isfinde = false;
            }
        } else if (text.indexOf(START_STR) >= 0) {// ?
            currentText = text;
            isfinde = true;
            currentRun = run;
        } else {
            currentText = "";
        }
        if (currentText.indexOf(END_STR) != -1) {
            changeValues(paragraph, currentRun, currentText, runIndex, map);
            isfinde = false;
        }
    }

}

From source file:colina.angel.controller.CreadorController.java

public void changeText(XWPFParagraph paragraph, String newText) {
    List<XWPFRun> runs = paragraph.getRuns();
    for (int i = runs.size() - 1; i > 0; i--) {
        paragraph.removeRun(i);/* www. j  av  a  2s .  c  om*/
    }
    XWPFRun run = runs.get(0);
    run.setText(newText, 0);

}

From source file:com.altar.worddocreader.WordDocReaderMain.java

public static XWPFDocument replaceText(XWPFDocument doc, HashMap<String, String> keys) throws Exception {
    String txt = "";
    int txtPosition = 0;
    String key = "";
    String val = "";
    for (XWPFParagraph p : doc.getParagraphs()) { //Dkmandaki her bir paragraf okumas yaplyor.
        for (XWPFRun run : p.getRuns()) { //paragraf iindeki satrlar okunuyor.
            txtPosition = run.getTextPosition();
            txt = run.getText(txtPosition);
            for (Map.Entry<String, String> entry : keys.entrySet()) { //keymap iinde gnderilen alanlar keymap'teki deerleri ile deitiriliyor.
                key = entry.getKey();//from  ww  w  .  j  a  va 2  s.c o m
                val = entry.getValue();
                if (txt != null && txt.indexOf(key) > -1) {
                    txt = txt.replace(key, val);
                    run.setText(txt, 0);
                }
            }
        }
    }

    return doc;

}

From source file:com.anphat.customer.controller.ExportContractToDocController.java

public void buildContractDetails(CustomerDTO customerDTO) {
    for (XWPFTable tbl : lstTable) {
        for (XWPFTableRow rowTbl : tbl.getRows()) {
            for (XWPFTableCell cellTbl : rowTbl.getTableCells()) {
                for (XWPFParagraph p : cellTbl.getParagraphs()) {
                    for (XWPFRun r : p.getRuns()) {
                        String text = r.getText(0);
                        //                            //Thoi gian
                        //                            if (text != null && DataUtil.isStringContainDateTime(text)) {
                        //                                text = DataUtil.replaceDateTime(text);
                        //                                r.setText(text, 0);
                        //                            }
                        //Ben giao
                        if (text != null && text.contains(Constants.REPORT.NAME)) {
                            text = text.replace(Constants.REPORT.NAME,
                                    DataUtil.getStringNullOrZero(customerDTO.getName().toUpperCase()));
                            r.setText(text, 0);
                        }/*  www .j  a va2  s  .  co  m*/
                        //Ma so thue
                        if (text != null && text.contains(Constants.REPORT.TAX_CODE)) {
                            text = text.replace(Constants.REPORT.TAX_CODE,
                                    DataUtil.getStringNullOrZero(customerDTO.getTaxCode()));
                            r.setText(text, 0);
                        }
                        //So dien thoai
                        if (text != null && text.contains(Constants.REPORT.TEL_NUMBER)) {
                            text = text.replace(Constants.REPORT.TEL_NUMBER,
                                    DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.TEL_NUMBER)));
                            r.setText(text, 0);
                        }
                        //Fax
                        if (text != null && text.contains(Constants.REPORT.FAX)) {
                            text = text.replace(Constants.REPORT.FAX,
                                    DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.FAX)));
                            r.setText(text, 0);
                        }
                        //Email
                        if (text != null && text.contains(Constants.REPORT.EMAIL)) {
                            text = text.replace(Constants.REPORT.EMAIL,
                                    DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL)));
                            r.setText(text, 0);
                        }
                        //Dia chi tru so
                        if (text != null && text.contains(Constants.REPORT.OFFICE_ADDRESS)) {
                            text = text.replace(Constants.REPORT.OFFICE_ADDRESS, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.OFFICE_ADDRESS)));
                            r.setText(text, 0);
                        }
                        //Dia chi giao dich
                        if (text != null && text.contains(Constants.REPORT.DEPLOY_ADDRESS)) {
                            text = text.replace(Constants.REPORT.DEPLOY_ADDRESS, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.DEPLOY_ADDRESS)));
                            r.setText(text, 0);
                        }
                        //Co quan thue
                        if (text != null && text.contains(Constants.REPORT.TAX_DEPARTMENT)) {
                            text = text.replace(Constants.REPORT.TAX_DEPARTMENT, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.TAX_DEPARTMENT)));
                            r.setText(text, 0);
                        }
                        //CMND
                        if (text != null && text.contains(Constants.REPORT.CMND)) {
                            text = text.replace(Constants.REPORT.CMND,
                                    DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.CMND)));
                            r.setText(text, 0);
                        }
                        //CMND
                        if (text != null && text.contains(Constants.REPORT.NGAY_CAP_CMND)) {
                            text = text.replace(Constants.REPORT.NGAY_CAP_CMND, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.NGAY_CAP_CMND)));
                            r.setText(text, 0);
                        }
                        //Nguoi dai dien
                        if (text != null && text.contains(Constants.REPORT.NGUOI_DAIDIEN)) {
                            text = text.replace(Constants.REPORT.NGUOI_DAIDIEN, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.NGUOI_DAIDIEN)));
                            r.setText(text, 0);
                        }
                        //Chuc vu Nguoi dai dien
                        if (text != null && text.contains(Constants.REPORT.CHUVU_NGUOI_DAIDIEN)) {
                            text = text.replace(Constants.REPORT.CHUVU_NGUOI_DAIDIEN, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.CHUVU_NGUOI_DAIDIEN)));
                            r.setText(text, 0);
                        }
                        //SDT Nguoi dai dien
                        if (text != null && text.contains(Constants.REPORT.SDT_NGUOI_DAIDIEN)) {
                            text = text.replace(Constants.REPORT.SDT_NGUOI_DAIDIEN, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.SDT_NGUOI_DAIDIEN)));
                            r.setText(text, 0);
                        }
                        //Email Nguoi dai dien
                        if (text != null && text.contains(Constants.REPORT.EMAIL_NGUOI_DAIDIEN)) {
                            text = text.replace(Constants.REPORT.EMAIL_NGUOI_DAIDIEN, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL_NGUOI_DAIDIEN)));
                            r.setText(text, 0);
                        }
                        //Nguoi dai dien
                        if (text != null && text.contains(Constants.REPORT.NGUOI_LIENHE)) {
                            text = text.replace(Constants.REPORT.NGUOI_LIENHE,
                                    DataUtil.getStringNullOrZero(mapValues.get(Constants.REPORT.NGUOI_LIENHE)));
                            r.setText(text, 0);
                        }
                        //Chuc vu Nguoi lien he
                        if (text != null && text.contains(Constants.REPORT.CHUCVU_NGUOI_LIENHE)) {
                            text = text.replace(Constants.REPORT.CHUCVU_NGUOI_LIENHE, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.CHUCVU_NGUOI_LIENHE)));
                            r.setText(text, 0);
                        }
                        //SDT Nguoi lien he
                        if (text != null && text.contains(Constants.REPORT.SDT_NGUOI_LIENHE)) {
                            text = text.replace(Constants.REPORT.SDT_NGUOI_LIENHE, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.SDT_NGUOI_LIENHE)));
                            r.setText(text, 0);
                        }
                        //Email Nguoi lien he
                        if (text != null && text.contains(Constants.REPORT.EMAIL_NGUOI_LIENHE)) {
                            text = text.replace(Constants.REPORT.EMAIL_NGUOI_LIENHE, DataUtil
                                    .getStringNullOrZero(mapValues.get(Constants.REPORT.EMAIL_NGUOI_LIENHE)));
                            r.setText(text, 0);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java

License:Open Source License

private NavigableMap<Integer, XWPFRun> getPosToRuns(XWPFParagraph paragraph) {
    int pos = 0;//from   w w w  .ja v  a  2 s .c o m
    NavigableMap<Integer, XWPFRun> map = new TreeMap<>();
    for (XWPFRun run : paragraph.getRuns()) {
        String runText = run.getText(run.getTextPosition());
        if (runText != null && runText.length() > 0) {
            for (int i = 0; i < runText.length(); i++) {
                map.put(pos + i, run);
            }
            pos += runText.length();
        }
    }
    return map;
}

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

License:Apache License

/**
 * running string Algorithm//from   ww w .j  av  a2  s.c o m
 * 
 * @param paragraph
 * @return
 */
public static List<RunTemplate> parseRun(XWPFParagraph paragraph) {
    List<XWPFRun> runs = paragraph.getRuns();
    if (null == runs || runs.isEmpty())
        return null;
    String text = paragraph.getText();
    logger.debug("Paragrah's text is:" + text);
    List<Pair<RunEdge, RunEdge>> pairs = new ArrayList<Pair<RunEdge, RunEdge>>();
    List<String> tags = new ArrayList<String>();
    calcTagPosInParagraph(text, pairs, tags);

    List<RunTemplate> rts = new ArrayList<RunTemplate>();
    if (pairs.isEmpty())
        return rts;
    RunTemplate runTemplate;
    calcRunPosInParagraph(runs, pairs);
    for (Pair<RunEdge, RunEdge> pai : pairs) {
        logger.debug(pai.getLeft().toString());
        logger.debug(pai.getRight().toString());
    }
    // split and merge
    Pair<RunEdge, RunEdge> pair2 = pairs.get(0);
    int length = pairs.size();
    int tagIndex = length;
    for (int n = length - 1; n >= 0; n--) {
        pair2 = pairs.get(n);
        RunEdge left2 = pair2.getLeft();
        RunEdge right2 = pair2.getRight();
        int left_r = left2.getRunPos();
        int right_r = right2.getRunPos();
        int runEdge = left2.getRunEdge();
        int runEdge2 = right2.getRunEdge();
        String text1 = runs.get(left_r).getText(0);
        String text2 = runs.get(right_r).getText(0);
        if (runEdge2 + 1 >= text2.length()) {
            if (left_r != right_r)
                paragraph.removeRun(right_r);
        } else {
            String substring = text2.substring(runEdge2 + 1, text2.length());
            if (left_r == right_r) {
                XWPFRun insertNewRun = paragraph.insertNewRun(right_r + 1);
                styleRun(insertNewRun, runs.get(right_r));
                insertNewRun.setText(substring, 0);
            } else
                runs.get(right_r).setText(substring, 0);
        }
        for (int m = right_r - 1; m > left_r; m--) {
            paragraph.removeRun(m);
        }
        if (runEdge <= 0) {
            runs.get(left_r).setText(tags.get(--tagIndex), 0);
            runTemplate = parseRun(runs.get(left_r));
        } else {
            String substring = text1.substring(0, runEdge);
            XWPFRun xwpfRun = runs.get(left_r);
            runs.get(left_r).setText(substring, 0);
            XWPFRun insertNewRun = paragraph.insertNewRun(left_r + 1);
            styleRun(insertNewRun, xwpfRun);
            insertNewRun.setText(tags.get(--tagIndex), 0);
            runTemplate = parseRun(runs.get(left_r + 1));
        }

        if (null != runTemplate) {
            rts.add(runTemplate);
        }
    }
    return rts;
}

From source file:com.deepoove.poi.template.run.RunTemplate.java

License:Apache License

public Integer getRunPos() {
    XWPFParagraph paragraph = (XWPFParagraph) run.getParent();
    List<XWPFRun> runs = paragraph.getRuns();
    for (int i = 0; i < runs.size(); i++) {
        if (run == runs.get(i)) {
            return i;
        }/*  w  ww . java 2s. c  o m*/
    }
    return null;
}

From source file:com.deepoove.poi.template.run.RunTemplate.java

License:Apache License

public XWPFRun getBeforeRun() {
    Integer runPos = getRunPos();
    if (null == runPos)
        return null;
    XWPFParagraph paragraph = (XWPFParagraph) run.getParent();
    return runPos == 0 ? null : paragraph.getRuns().get(runPos - 1);
}

From source file:com.deepoove.poi.template.run.RunTemplate.java

License:Apache License

public XWPFRun getAfterRun() {
    Integer runPos = getRunPos();
    if (null == runPos)
        return null;
    XWPFParagraph paragraph = (XWPFParagraph) run.getParent();
    return runPos == (paragraph.getRuns().size() - 1) ? null : paragraph.getRuns().get(runPos + 1);
}

From source file:com.qihang.winter.poi.word.parse.ParseWord07.java

License:Apache License

/**
 * ???/*w ww . j  a v a  2s  .  co  m*/
 * 
 * @param map
 * @author Zerrion
 * @date 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 WordImageEntity) {// 
        currentRun.setText("", 0);
        addAnImage((WordImageEntity) obj, currentRun);
    } else {
        currentText = obj.toString();
        currentRun.setText(currentText, 0);
    }
    for (int k = 0; k < runIndex.size(); k++) {
        paragraph.getRuns().get(runIndex.get(k)).setText("", 0);
    }
    runIndex.clear();
}