Mark (colorize) a string to write to the file

Asked

Viewed 1,276 times

3

My application searches for a few words in a file. When the program recognizes these words (strings), it should mark it. For example: change the font color or change the word background color.

Then I will write the same text, but with the font color of the different word. I will write in pdf or doc, which is easier to do this.

I do not know any function that does this and even if when writing the file I will be able to write with this detail.

I kept looking and I found class StyleContext but I have no idea how it works.

  • 3

    Java does not have native support for either DOC or PDF format, you should look for a library to solve your problem. Example: Apache POI for DOC and iText to PDF.

1 answer

3


If your input text is quite simple (I suppose it is because you want to color a String). I believe that the easiest option and that can still open in word without problems is to use RTF (Rich Text Format).

Although there is the native stand to the RTF, the support is quite limited. However the specification of the same is extremely simple and you can implement it on your own.

All you need to do is set the desired colors:

\\cores    \\ \cf1 = Preto #000000 \cf2 Vermelho #FF0000  \cf3 = Azul #3200FF 
{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red50\\green0\\blue255;}\n

Below is an example that does exactly what you said:
Download in gist

RTF.java

import java.io.FileNotFoundException;
import java.io.PrintWriter;


public class RTF {
    private StringBuilder texto;
    public String getTexto() {
        return texto.toString();
    }
    public void setTexto(String texto) {
        this.texto = criaRTF(texto);
    }
    public StringBuilder criaRTF(String text){
        StringBuilder arquivortf = new StringBuilder("{\\rtf1\\ansi\\deff0\n");
                                    // \cf1 = Preto (cor padrao)      ;\cf2 = vermelho        ;\cf3 =  Azul
        arquivortf.append("{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red50\\green0\\blue255;}\n");
        arquivortf.append(text);
        arquivortf.append("\n}");
        return arquivortf;
    }
    public void colorirTexto(String palavra)
    {
        //Colore com a cor Azul i.e \cf3
        String palavraColorida = "{\\cf3" + palavra + "}";
        int indice = texto.indexOf(palavra);
        while (indice != -1)
        {
            texto.replace(indice, indice + palavra.length(), palavraColorida);
            // vai ao fim da substituicao
            indice += palavraColorida.length();
            indice = texto.indexOf(palavra, indice);
        }
    }
    public void salvaRTF(String nomeArquivo){
        try {
            PrintWriter saida = new PrintWriter(nomeArquivo + ".rtf");
            saida.println(texto);
            saida.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}

Testartf.java

public class TestaRTF {
    public static void main(String[] args){
        String texto = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n" +
                        " Aenean commodo ligula eget dolor. Aenean massa. Cum\n"     +
                        " sociis natoque penatibus et magnis dis parturient montes,\n" + 
                        "nascetur ridiculus mus. Donec quam felis, ultricies nec,\n"   + 
                        "pellentesque eu, pretium quis, sem. Nulla consequat massa\n"  + 
                        " quis enim. Donec pede justo, fringilla vel, aliquet nec,";
        RTF rtf = new RTF();
        rtf.setTexto(texto);
        rtf.colorirTexto("quis");
        rtf.salvaRTF("arquivoColorido");
    }
}

If your input is more complex texts (i.e is not plain text) I suggest that as well as the Anthony said that uses libraries for DOC or PDF.

  • So.. my application has a very large string (it’s text inside the string) and I’m going through it word by word.. and when I find certain occurrences, I will mark (color) this word... Dae, at the end I will have a string equal to the input, except that some words will be "marked". And then yes I will put the string in a PDF or DOC file.

  • My entry is PDF, but I’ve handled it.. so the content stays in a string. I think this makes it easier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.