I need to modify some templates that are in .doc format from Java , that is, by code I detect certain words of the document which I use as "variables" for later modify them by which I pass. I got through the POI library, access to that document, separate by paragraphs, detect where that word is to be modified, and change it at the code level (I can not change it in the word document). I leave the code that I have so far summarized, I would appreciate very much if someone can help me get that part that I need. Thanks in advance.
package PruebasWord;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
//ESTO LEE Y BUSCA PALABRAS O CONTENIDO POR LINEAS DENTRO DE UN ARCHIVO CON FORMATO DETERMINADO
//( COMO PUEDE SER UN WORD, PERO GUARDADO EN VERSION .doc )
public class prueba2 {
public static void main (String[] args) throws IOException {
//RUTA DONDE SE ENCUENTRA EL DOCUMENTO .doc
String fileName = "C:\Datos\Hola.doc";
//STRING QUE ALMACENARA EL CONTENIDO DE CADA PARRAFO DEL DOCUMENTO
String linea = "";
InputStream fis = new FileInputStream(fileName);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(fs);
//ASIGNA A LA VARIABLE range COMO VALOR EL RANGO DEL DOCUMENTO
Range range = doc.getRange();
//IMPRIME POR CONSOLA EL CONTENIDO DEL DOCUMENTO
System.out.println(doc.getDocumentText());
System.out.println("---------------------------");
//IMPRIME POR CONSOLA NUMERO DE PARRAFOS
System.out.println(range.numParagraphs());
System.out.println("---------------------------");
//BUCLE RECORRE EL DOCUMENTO POR PARRAFOS
for (int i=0; i<range.numParagraphs(); i++) {
//CREA EL PARRAFO DEL DOCUMENTO
Paragraph par = range.getParagraph(i);
//PASAMOS EL PARRAFO EN EL QUE NOS ENCONTRAMOS A UN STRING (linea)
linea = par.text().toString();
//IMPRIME EL CONTENIDO DEL PARRAFO
System.out.println(linea);
//DEVUELVE true SI LA PALABRA (Hola) SE ENCUENTRA DENTRO DEL PARRAFO
System.out.println(linea.indexOf("Hola") >= 0);
//SI EL PARRAFO CONTIENE (Hola) INDICA QUE AHÍ ESTÁ Y QUE CAMBIARÁ
if(par.text().toString().indexOf("Hola") >= 0) {
System.out.print(" Aquí Cambiará!! --> ");
}
//REEMPLAZA (SI CONTIENE) LA PALABRA (Hola) DE DICHO PARRAFO, POR LA PALABRA (Cambio)
par.replaceText("Hola", "Cambio");
//DEBE IMPRIMIR POR PANTALLA EL PARRAFO MODIFICADO SI SE DIO EL CASO..
System.out.println(par.text().toString());
} // fin for
//VOLVEMOS A IMPRIMIR EL DOCUMENTO POR CONSOLA Y COMPROBAMOS QUE SE HA MODIFICADO
System.out.println("---------------------------");
System.out.println(doc.getDocumentText().toString());
} //fin main
}