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

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

Introduction

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

Prototype

public void setText(String value, int pos) 

Source Link

Document

Sets the text of this text run in the

Usage

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);
                        }//from www . j  a  va  2  s .c om
                        //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 void doReplace(XWPFParagraph p, Map<String, String> data) {
    String pText = p.getText(); // complete paragraph as string
    if (pText.contains("${")) { // if paragraph does not include our pattern, ignore
        Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
        Matcher matcher = pattern.matcher(pText);
        while (matcher.find()) { // for all patterns in the paragraph
            String key = matcher.group(1); // extract key start and end pos
            int start = matcher.start(1);
            int end = matcher.end(1);
            String value = data.get(key);
            if (value == null) {
                value = "";
            }/*w w w.j av a2  s  . c om*/
            // get runs which contain the pattern
            SortedMap<Integer, XWPFRun> range = getPosToRuns(p).subMap(start - 2, true, end + 1, true);
            boolean isFoundDollarSign = false;
            boolean isFoundLeftBracket = false;
            boolean isFoundRightBracket = false;
            XWPFRun prevRun = null; // previous run handled in the loop
            XWPFRun found2Run = null; // run in which { was found
            int found2Pos = -1; // pos of { within above run
            for (XWPFRun run : range.values()) {
                if (run == prevRun) {
                    continue; // this run has already been handled
                }
                if (isFoundRightBracket) {
                    break; // done working on current key pattern
                }
                prevRun = run;
                for (int k = 0;; k++) { // iterate over texts of run r
                    if (isFoundRightBracket) {
                        break;
                    }
                    String txt = null;
                    try {
                        txt = run.getText(k); // note: should return null, but throws exception if the text does not exist
                    } catch (Exception e) {
                    }
                    if (txt == null) {
                        break; // no more texts in the run, exit loop
                    }
                    if (txt.contains("$") && !isFoundDollarSign) { // found $, replace it with value from data map
                        txt = txt.replaceFirst("\\$", value);
                        isFoundDollarSign = true;
                    }
                    if (txt.contains("{") && !isFoundLeftBracket && isFoundDollarSign) {
                        found2Run = run; // found { replace it with empty string and remember location
                        found2Pos = txt.indexOf('{');
                        txt = txt.replaceFirst("\\{", "");
                        isFoundLeftBracket = true;
                    }

                    // find } and set all chars between { and } to blank
                    if (isFoundDollarSign && isFoundLeftBracket && !isFoundRightBracket) {
                        if (txt.contains("}")) {
                            if (run == found2Run) { // complete pattern was within a single run
                                txt = txt.substring(0, found2Pos) + txt.substring(txt.indexOf('}'));
                            } else {
                                txt = txt.substring(txt.indexOf('}'));
                            }
                        } else if (run == found2Run) {
                            txt = txt.substring(0, found2Pos);
                        } else {
                            txt = ""; // run between { and }, set text to blank
                        }
                    }
                    if (txt.contains("}") && !isFoundRightBracket) {
                        txt = txt.replaceFirst("\\}", "");
                        isFoundRightBracket = true;
                    }
                    run.setText(txt, k);
                }
            }
        }
    }
}

From source file:com.deepoove.poi.policy.PictureRenderPolicy.java

License:Apache License

@Override
public void render(ElementTemplate runTemplateP, Object renderData, XWPFTemplate doc) {
    RunTemplate runTemplate = (RunTemplate) runTemplateP;
    XWPFRun run = runTemplate.getRun();
    if (renderData == null) {
        return;//from   w w w  .  java2 s  .  com
    }
    PictureRenderData pictureRenderData = null;
    if (renderData instanceof PictureRenderData) {
        pictureRenderData = (PictureRenderData) renderData;
    } else {
        logger.warn("error render data,should be pictureRenderData:" + renderData.getClass());
        return;
    }
    String blipId;
    try {
        byte[] data = pictureRenderData.getData();
        if (null == data) {
            FileInputStream is = new FileInputStream(pictureRenderData.getPath());
            blipId = doc.getXWPFDocument().addPictureData(is, suggestFileType(pictureRenderData.getPath()));
        } else {
            blipId = doc.getXWPFDocument().addPictureData(data, suggestFileType(pictureRenderData.getPath()));
        }
        doc.getXWPFDocument().addPicture(blipId,
                doc.getXWPFDocument().getNextPicNameNumber(suggestFileType(pictureRenderData.getPath())),
                pictureRenderData.getWidth(), pictureRenderData.getHeight(), run);
        run.setText("", 0);
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.deepoove.poi.policy.SelfRenderPolicy.java

License:Apache License

@Override
public void render(ElementTemplate runTemplateP, Object renderData, XWPFTemplate template) {
    RunTemplate runTemplate = (RunTemplate) runTemplateP;
    XWPFRun run = runTemplate.getRun();
    run.setText(runTemplate.getSource(), 0);
}

From source file:com.deepoove.poi.policy.TextRenderPolicy.java

License:Apache License

@Override
public void render(ElementTemplate runTemplateP, Object renderData, XWPFTemplate template) {
    RunTemplate runTemplate = (RunTemplate) runTemplateP;
    XWPFRun run = runTemplate.getRun();
    if (null == renderData) {
        //support String to set blank
        run.setText("", 0);
        return;/* www  . j a v  a2 s.  c o  m*/
    }

    TextRenderData textRenderData = null;
    if (renderData instanceof TextRenderData) {
        textRenderData = (TextRenderData) renderData;
    } else {
        textRenderData = new TextRenderData(String.valueOf(renderData));
    }
    String data = textRenderData.getText();
    styleRun(run, textRenderData.getStyle());
    if (null == data)
        data = "";

    if (data.contains(LINE_CHARACTOR)) {
        // String[] lines = data.split("\\n");
        int from = 0, end = 0;
        List<String> lines = new ArrayList<String>();
        while (-1 != (end = data.indexOf(LINE_CHARACTOR, from))) {
            lines.add(data.substring(from, end));
            from = end + LINE_CHARACTOR.length();
        }
        lines.add(data.substring(from));
        Object[] linesArray = lines.toArray();
        run.setText(linesArray[0].toString(), 0); // set first line into
        // XWPFRun
        for (int i = 1; i < linesArray.length; i++) {
            run.addBreak(); // add break and insert new text
            run.setText(linesArray[i].toString());
        }
    } else {
        run.setText(data, 0);
    }
}

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

License:Apache License

/**
 * running string Algorithm/*from  ww  w  .j ava2s .  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.foc.vaadin.gui.mswordGenerator.FocMSWordLabel.java

License:Apache License

@Override
public void write(IMSWordContainer container) {
    try {/*from   w  w w .  ja v  a 2 s  .c om*/
        XWPFParagraph paragraph = container.insertParagraph();
        XWPFRun run = paragraph.createRun();
        applyStyleToFocWordLabel(run, paragraph);

        //         FImageProperty imageProperty = (FImageProperty) getParent().getFocData().iFocData_getDataByPath(getXmlAttribute().getValue("name"));
        //         
        //         BufferedImage bufferedImage = imageProperty.getImageValue();
        //         ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
        //         ImageIO.write(bufferedImage, "png", imagebuffer);
        //
        //         InputStream inputStream = new ByteArrayInputStream(imagebuffer.toByteArray());
        //      addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
        //         run.addPicture(inputStream, 0, "temp", 20, 20);

        run.setText(value, 0);
    } catch (Exception e) {
        Globals.logException(e);
    }
}

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

License:Apache License

/**
 * ???/* w w  w  . ja  v a2s .  c o 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();
}

From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

private static void replaceParagraph(XWPFParagraph paragraph, String placeHolder, String replaceText) {
    for (XWPFRun r : paragraph.getRuns()) {
        String text = r.getText(r.getTextPosition());
        if (text != null && text.contains(placeHolder)) {
            text = text.replace(placeHolder, replaceText);
            r.setText(text, 0);
        }//from ww  w  .  j a  v  a  2s  .  c  o m
    }
}

From source file:DocxProcess.DocxTemplateReplacer.java

private void replaceParagraph(XWPFParagraph p, Map<String, String> data) {

    String pText = p.getText(); // complete paragraph as string
    if (pText.contains("${")) { // if paragraph does not include our pattern, ignore
        TreeMap<Integer, XWPFRun> posRuns = getPosToRuns(p);
        Pattern pat = Pattern.compile("\\$\\{(.+?)\\}");
        Matcher m = pat.matcher(pText);
        while (m.find()) { // for all patterns in the paragraph
            String g = m.group(1); // extract key start and end pos
            int s = m.start(1);
            int e = m.end(1);
            String key = g;/*from w w w. jav  a 2s . co m*/
            String x = data.get(key);
            if (x == null)
                x = "";
            SortedMap<Integer, XWPFRun> range = posRuns.subMap(s - 2, true, e + 1, true); // get runs which contain the pattern
            boolean found1 = false; // found $
            boolean found2 = false; // found {
            boolean found3 = false; // found }
            XWPFRun prevRun = null; // previous run handled in the loop
            XWPFRun found2Run = null; // run in which { was found
            int found2Pos = -1; // pos of { within above run
            for (XWPFRun r : range.values()) {
                if (r == prevRun)
                    continue; // this run has already been handled
                if (found3)
                    break; // done working on current key pattern
                prevRun = r;
                for (int k = 0;; k++) { // iterate over texts of run r
                    if (found3)
                        break;
                    String txt = null;
                    try {
                        txt = r.getText(k); // note: should return null, but throws exception if the text does not exist
                    } catch (Exception ex) {

                    }
                    if (txt == null)
                        break; // no more texts in the run, exit loop
                    if (txt.contains("$") && !found1) { // found $, replaceAll it with value from data map
                        txt = txt.replaceFirst("\\$", x);
                        found1 = true;
                    }
                    if (txt.contains("{") && !found2 && found1) {
                        found2Run = r; // found { replaceAll it with empty string and remember location
                        found2Pos = txt.indexOf('{');
                        txt = txt.replaceFirst("\\{", "");
                        found2 = true;
                    }
                    if (found1 && found2 && !found3) { // find } and set all chars between { and } to blank
                        if (txt.contains("}")) {
                            if (r == found2Run) { // complete pattern was within a single run
                                txt = txt.substring(0, found2Pos) + txt.substring(txt.indexOf('}'));
                            } else // pattern spread across multiple runs
                                txt = txt.substring(txt.indexOf('}'));
                        } else if (r == found2Run) // same run as { but no }, remove all text starting at {
                            txt = txt.substring(0, found2Pos);
                        else
                            txt = ""; // run between { and }, set text to blank
                    }
                    if (txt.contains("}") && !found3) {
                        txt = txt.replaceFirst("\\}", "");
                        found3 = true;
                    }
                    r.setText(txt, k);
                }
            }
        }
        //            System.out.println(p.getText());

    }

}