【问题标题】:highlighting subString in word突出显示word中的子字符串
【发布时间】:2013-10-21 20:19:10
【问题描述】:

我想使用 java 代码突出显示 word 文档中的字符串模式。我已经确定了 java 中的模式,现在我想在 word 文档中突出显示该模式。我想将字符串写入word文件并突出显示字符串中的模式。

我想要这样的输出。

【问题讨论】:

    标签: java regex ms-word highlight


    【解决方案1】:

    Sarah,下面应该做的是获取您的字符串,获取我们想要着色的单词的索引点(像以前一样),然后它将创建一个新的 ArrayList,其中将包含您的字符串的字母加上我们的单词想上色。例如,如果您的字符串是“abi​​gdog”,并且您想将颜色变大,我制作了一个 ArrayList,如下所示:

    element1: a
    element2: big
    element3: d
    element4: o
    element5: g
    

    在创建角色运行时,我们遍历 ArrayList,根据需要为相关元素/单词着色。

    public class SO4 {
    public static void main(String[] args) {
    
        String words = "adaahhhccadcdaccaaddadddcchah";  //String from doc
        String[] wordsToColour = {"ahhh", "acca", "adda", "hah" };   //Words we want to find
        ArrayList<Integer> points = new ArrayList<Integer>(); //Hold indexes of words
    
        //Get the points of the words to colour first and add them to points arrayList
        int j, k =0, count = 0;
        String checker = "";
        while(count < wordsToColour.length){ //For each search word
            j = 0; //start of search word
            k = wordsToColour[count].length();  //end of current search word
            while(k < words.length() + 1){ //we will search whole string
                checker = words.substring(j, k);  //String equal to a substring the same length as the word we are searching for 
                    if(checker.equals(wordsToColour[count])){ //If we find our word
                        points.add(Integer.valueOf(j)); //Store co-ordinates in arrayList, co-ordinate being the index at which the word is found
                        points.add(Integer.valueOf(k)); //Store co-ordinates in arrayList co-ordinate being the index at which the word is found
                    }
                j++;
                k++;
            }//Inner loop
            count++;
        }//Outer loop
    
        //Checking the arrayList to see if it contains our words. 
    /*        k=0; j=1;
         while(j < points.size()){  
             System.out.println(points.get(k) + " " + points.get(j) + " Word: " + words.substring(points.get(k), points.get(j)));
             k = (k+2);
            j = (j + 2);
         }   */
    
    
         //This will hold a reference to either a letter or a word to colour 
         ArrayList<String> wordsByElement = new ArrayList<String>(words.length());  
         k = 0; j = 0;
         String storage;  //Will hold reference to current substring
         while(j < words.length()){
             storage = words.substring(j, j+1); //Will iterate over each letter
             if(j == points.get(k)){ //If we hit a point stored in points array
                 //Use points in array to add substring word to wordsByElement ArrayList
                 wordsByElement.add(words.substring(points.get(k), points.get(k+1))); //Adding
                 j = points.get(k + 1) -1; //Set j to continue at the point after the word we just extracted 
                 k = (k + 2); //raise k to start point of next word we want
             }/*Inner if */ else { //If we didn't get a match
                 wordsByElement.add(storage); //add the letter
             }
         j++;
         }//While loop end
    
    
                 //This shows what is in the ArrayList and should show what I have done to this point
             for(String s : wordsByElement){
             System.out.println(s); //Checking what is in our ArrayList
         } 
    
          XWPFDocument newDoc = new XWPFDocument(); //Doc to write new doc to
          XWPFParagraph para = newDoc.createParagraph(); //Paragraph
          XWPFRun run = para.createRun();  //Where the text will be written from
         k=0;
    
         while(k < wordsByElement.size()){
             run = para.createRun();
             switch (wordsByElement.get(k)) {
            case "ahhh": //If word is ahhh
                run.setColor("ff5400"); //Set Color
                break;
            case "acca": //If word is acca
                run.setColor("a7bf42"); //set color
                break;
            case "adda": //So on
                run.setColor("7b8896"); //so forth
                break;
            case "hah":
                run.setColor("00adee");
                break;
            default: //If word is not one of the above
                run.setColor("000000"); //Color is black
                break;
            }
    
             run.setText((wordsByElement.get(k))); 
             k++;
         }
    
         try {
            newDoc.write(new FileOutputStream(new File("D:\\Users\\user277005\\Desktop\\testerFinished.docx")));
    
            //View our document
            if(Desktop.isDesktopSupported()){
                Desktop.getDesktop().open(new File("D:\\Users\\user277005\\Desktop\\testerFinished.docx"));
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        } //save
    
    }
    }
    

    祝你好运!

    【讨论】:

    • 我怎样才能制作出特定的欲望颜色?
    • @Sarah 我重新调整了我的答案以尝试为您提供更多帮助,看看是否有帮助。
    • @Sarah 我又做了一次 rejig =D 看起来有点吓人,但我已经尽可能多地发表评论。试一试,希望它会是您所需要的。
    • @Sarah Sarah,我已经为你重新设计了一遍,这次我把我之前的答案的一部分,并以不同的方式使用它。它应该让您指定要着色的单词以及所需的颜色。请试一试,如果可行,请为我的努力申请一个绿色勾号 =D
    • 这会改变文本的颜色,如何突出显示文本?
    【解决方案2】:

    如果您谈论的是 Office Word 文档,您应该查看Apache POI。它可以让您从 Java 编辑 MS Office 文件。

    【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多