Java 如何使用POI突出显示MS word中Pargraph的文本

Java 如何使用POI突出显示MS word中Pargraph的文本,java,apache-poi,Java,Apache Poi,我正在为word文档开发一个比较工具,每当两个文档存在差异时,我都需要突出显示段落中的子字符串。当我尝试使用run突出显示时,它会突出显示整个段落,而不是子字符串 能否请您指导我们,如何为子字符串实现这一点。我也遇到了同样的问题。这里我发布了一个示例方法,其中突出显示了运行中包含的子字符串 private int highlightSubsentence(String sentence, XWPFParagraph p, int i) { //get the current run St

我正在为word文档开发一个比较工具,每当两个文档存在差异时,我都需要突出显示段落中的子字符串。当我尝试使用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;
}
您可能需要保存删除的管路的格式样式,以便使新管路具有旧格式

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

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

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);
}
需要时,我在新一轮中复制了旧样式

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);
    }
}
要添加明暗处理和高亮度,我使用了:

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);
}

我也有同样的问题。这里我发布了一个示例方法,其中突出显示了运行中包含的子字符串

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;
}
您可能需要保存删除的管路的格式样式,以便使新管路具有旧格式

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

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

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);
}
需要时,我在新一轮中复制了旧样式

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);
    }
}
要添加明暗处理和高亮度,我使用了:

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);
}

请参阅链接以创建新文档,我面临突出显示run属性的子字符串的问题。代码显示了如何突出显示run。您不能格式化run的一部分。首先必须创建一个仅包含子字符串的新run,然后才能格式化该run。在本文中,我给出了一个如何循环所有run并格式化其中一个的示例单个字符不同。这也需要每个单独的字符都在自己的运行中。这很简单,因为只有一个单独的字符需要格式化。只是为了让您知道您的要求有多复杂。请参阅链接是创建一个新文档,我面临的问题是突出显示运行属性的子字符串。代码显示s如何突出显示跑步。您不能格式化跑步的一部分。首先,您必须创建一个只包含子字符串的新跑步,然后才能格式化该跑步。在本文中,我举了一个示例,说明如何循环所有跑步并格式化一个不同的字符。这还需要每个字符都在自己的跑步中。a这很简单b因为只有一个字符需要格式化。这只是为了让你知道你的需求有多复杂。嗨,艾尔,你是如何复制样式的?我想不出来,我在这里的尝试都没有发现trickIt是很久以前的。我会搜索它。我更新了答案。希望有帮助。我无法编写所有代码,因为我太多了。如果有用的话,竖起大拇指!;)嗨,艾尔,你是怎么复制样式的?我想不出来,而且我在这里的尝试都没有复制过。trickIt是很久以前的事了。我会搜索它。我更新了答案。希望有帮助。我无法编写所有代码,因为它太多了。如果有帮助,竖起大拇指!;)