Created
August 18, 2014 16:16
-
-
Save mansueli/9f94f01e8d4760ff3db3 to your computer and use it in GitHub Desktop.
RTF exemplo em java (http://pt.stackoverflow.com/questions/29355/marcar-colorir-uma-string-para-escrever-no-arquivo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo; | |
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) | |
{ | |
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment