中突出显示段落的文本

中突出显示段落的文本

本文介绍了如何使用 POI 在 MS word 中突出显示段落的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 word 文档开发一个比较工具,每当文档中存在差异时,我需要突出显示段落中的子字符串.当我尝试使用 run 突出显示时,它突出显示整个段落而不是子字符串.

I am developing a compare tool for the word document, whenever there is difference in both the document i need to highlight the substring in the paragraph.When i try to highlight using run, its highlighting whole paragraph instead of the sub string.

能否请您指导我们,我如何为子字符串实现这一点.

Can you please guide us, how can i achieve this for a substring.

推荐答案

我遇到了同样的问题.在这里,我发布了一个示例方法,您可以在其中突出显示运行中包含的子字符串.

I had the same problem. Here I post a sample method where you highlight a substring contained in a run.

private int highlightSubsentence(String sentence, XWPFParagraph p, int i) {
    //get the current run Style - here I might need to save the current style
    XWPFRun currentRun = p.getRuns().get(i);
    String currentRunText = currentRun.text();
    int sentenceLength = sentence.length();
    int sentenceBeginIndex = currentRunText.indexOf(sentence);
    int addedRuns = 0;
    p.removeRun(i);
    //Create, if necessary, a run before the highlight part
    if (sentenceBeginIndex > 0) {
        XWPFRun before = p.insertNewRun(i);
        before.setText(currentRunText.substring(0, sentenceBeginIndex));
        //here I might need to re-introduce the style of the deleted run
        addedRuns++;
    }

    // highlight the interesting part
    XWPFRun sentenceRun = p.insertNewRun(i + addedRuns);
    sentenceRun.setText(currentRunText.substring(sentenceBeginIndex, sentenceBeginIndex + sentenceLength));
    currentStyle.copyStyle(sentenceRun);
    CTShd cTShd = sentenceRun.getCTR().addNewRPr().addNewShd();
    cTShd.setFill("00FFFF");

    //Create, if necessary, a run after the highlight part
    if (sentenceBeginIndex + sentenceLength != currentRunText.length()) {
        XWPFRun after = p.insertNewRun(i + addedRuns + 1);
        after.setText(currentRunText.substring(sentenceBeginIndex + sentenceLength));
        //here I might need to re-introduce the style of the deleted run
        addedRuns++;
    }
    return addedRuns;
}

您可能需要保存您删除的运行的格式样式,以便新运行具有旧格式.

You might need to save the formatting style of the run you delete in order to have the new runs with the old formatting.

此外,如果您需要突出显示的字符串分布在多个运行中,则您需要突出显示所有这些,但核心方法是我发布的方法.

Also, if the string you need to highlight is spread over more than one run, you will need to highlight all of them, but the core method is the one I posted.

关于风格问题:我有一个类 Style 将旧 Run 的所有样式保存在私有字段中(对于相应的类,您可以查看 XWPFRun 返回的内容.这些是我需要的子样式.还有其他显然我没有涵盖

On the Style Question:I had a class Style that saved all the Styles of the old Run in private fields (for the respective classes you can look at what XWPFRun returns.These are the sub-styles that I needed. There are others obviously I didn't cover

Style(XWPFRun run, XWPFDefaultRunStyle defaultRunStyle) {
    fontSize = run.getFontSize();
    fontFamily = run.getFontFamily();
    bold = run.isBold();
    italic = run.isItalic();
    strike = run.isStrikeThrough();
    underline = run.getUnderline();
    color = run.getColor();
    shadingColor = getShadeColor(run);
    highlightColor = getHighlightedColor(run);
}

我在需要时在新运行中复制了旧样式.

I copied the old style in the new run when needed.

public void copyStyle(XWPFRun newRun) {
    if (fontSize != -1) {
        newRun.setFontSize(fontSize);
    }
    newRun.setFontFamily(fontFamily);
    newRun.setBold(bold);
    newRun.setItalic(italic);
    newRun.setStrikeThrough(strike);
    newRun.setColor(color);
    newRun.setUnderline(underline);
    if (shadingColor != null) {
        addShading(newRun, shadingColor);
    }
    if (highlightColor != null) {
        addHighlight(newRun, highlightColor);
    }
}

添加阴影和高光已经存在:

To add Shading and Highligh already present I used:

public static void addHighlight(XWPFRun run, STHighlightColor.Enum hexColor) {
    if (run.getCTR().getRPr() == null) {
        run.getCTR().addNewRPr();
    }
    if (run.getCTR().getRPr().getHighlight() == null) {
        run.getCTR().getRPr().addNewHighlight();
    }
    run.getCTR().getRPr().getHighlight().setVal(hexColor);
}

public static void addShading(XWPFRun run, Object hexColor) {
    if (run.getCTR().getRPr() == null) {
        run.getCTR().addNewRPr();
    }
    if (run.getCTR().getRPr().getShd() == null) {
        run.getCTR().getRPr().addNewShd();
    }
    run.getCTR().getRPr().getShd().setFill(hexColor);
}

这篇关于如何使用 POI 在 MS word 中突出显示段落的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:23