如何使用POI在MS字中突出显示Pargraph的文本
我正在为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; }
您可能需要保存要删除的运行的格式样式,以便使用旧格式进行新运行。
此外,如果您需要突出显示的字符串分布在多个运行中,则需要突出显示所有字符串,但核心方法是我发布的方法。
- 使用IKVM.Net将Apache POI .jar转换为.dll
- 更新Apache POI 4.0后 – 无法识别属性“http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit”
- 使用Apache POI创建.xlsx文件时的java.lang.NoClassDefFoundError
- 阿帕奇poi子弹和编号
- Apache POI – 如何使用选项保护工作表?
- 使用Tikajar子进行Mimetype检查
- 使用HSSF的Apache POI比XSSF快得多 – 下一步是什么?
- 如何使用Apache的POI以编程方式从Powerpoint演示文稿中读取图形值?
- apache poi excel大自动列宽