POI高亮word文档中指定文本

    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream(new File("D:\\test.docx"));
        XWPFDocument doc = new XWPFDocument(is);

        for (XWPFParagraph paragraph : doc.getParagraphs()) {
            if (paragraph.getText().contains("想要高亮的内容")) {
                highLightParagraph(paragraph, "想要高亮的内容");
            }
        }

        File file = new File("D:\\test2.docx");
        FileOutputStream out = new FileOutputStream(file);
        doc.write(out);
        out.close();
        doc.close();
    }
    /**
     * 得到XWPFRun的CTRPr
     */
    public CTRPr getRunCTRPr(XWPFParagraph p, XWPFRun pRun) {
        CTRPr pRpr;
        if (pRun.getCTR() != null) {
            pRpr = pRun.getCTR().getRPr();
            if (pRpr == null) {
                pRpr = pRun.getCTR().addNewRPr();
            }
        } else {
            pRpr = p.getCTP().addNewR().addNewRPr();
        }
        return pRpr;
    }

    /**
     * 设置指定段落高亮
     */
    private void highLightParagraph(XWPFParagraph p, String content) {
        TextSegment segment = p.searchText(content, new PositionInParagraph());

        int beginRunIndex = segment.getBeginRun();
        int endRunIndex = segment.getEndRun();
        List<XWPFRun> runs = p.getRuns();

        if (beginRunIndex == endRunIndex) {
            XWPFRun run = runs.get(beginRunIndex);
            String runText = run.getText(0);
            if (runText.equals(content)) {
                highLight(p, run);
            } else {
                containsMatch(p, content, run, beginRunIndex);
            }
        } else {
            begin = runs.get(beginRunIndex);
            end = runs.get(endRunIndex);
            // 获得第一个run标签,匹配内容
            String beginText = getBeginString(begin.text(), content);
            // 获得最后一个run标签,匹配内容
            String endText = getEndString(end.text(), content);
            // 高亮中间的文本
            for (int i = beginRunIndex + 1; i < endRunIndex; i++) {
                XWPFRun run = runs.get(i);
                highLight(p, run);
            }
        }
    }

    private XWPFRun beginMatch(XWPFParagraph p, String content, XWPFRun run, int beginRunIndex) {
        String runText = run.text();
        if (runText.equals(content)) {
            highLight(p, run);
            return run;
        }
        run.setText(runText.replace(content, ""), 0);
        XWPFRun newRun = p.insertNewRun(beginRunIndex + 1);
        newRun.setText(content);
        highLight(p, newRun);
        return newRun;
    }

    private XWPFRun endMatch(XWPFParagraph p, String content, XWPFRun run, int endRunIndex) {
        String runText = run.text();
        if (runText.equals(content)) {
            highLight(p, run);
            return run;
        }
        run.setText(runText.replace(content, ""), 0);
        XWPFRun newRun = p.insertNewRun(endRunIndex);
        newRun.setText(content);
        highLight(p, newRun);
        return newRun;
    }

    private XWPFRun containsMatch(XWPFParagraph p, String content, XWPFRun run, int runIndex) {
        String runText = run.text();
        String[] arr = runText.split(content);

        run.setText(arr[0], 0);

        XWPFRun newRun = p.insertNewRun(runIndex + 1);
        newRun.setText(content);
        highLight(p, newRun);

        p.insertNewRun(runIndex + 2).setText(arr[1]);
        return newRun;
    }

    private void highLight(XWPFParagraph p, XWPFRun run) {
        CTRPr pRpr = getRunCTRPr(p, run);
        CTHighlight highlight = pRpr.isSetHighlight() ? pRpr
                .getHighlight() : pRpr.addNewHighlight();
        highlight.setVal(STHighlightColor.YELLOW);
    }

    public static String getBeginString(String begin, String content) {
        for (int i = 1; i < begin.length(); i++) {
            if (content.startsWith(begin)) {
                return begin;
            }
            begin = begin.substring(1);
        }
        return null;
    }

    public static String getEndString(String end, String content) {
        for (int i = end.length() - 1; i > 0; i--) {
            if (content.endsWith(end)) {
                return end;
            }
            end = end.substring(0, i);
        }
        return null;
    }
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值