doc - Relace HWPFDocument paragraph text using java results strange output -


i require replace hwpfdocument paragraph text of .doc file if contains particular text using java. replaces text. process writes output text in strange way. please me rectify issue. code snippet used:

public static hwpfdocument processchange(hwpfdocument doc) {     try     {         range range = doc.getrange();         (int = 0; < range.numparagraphs(); i++)         {             paragraph paragraph = range.getparagraph(i);             if (paragraph.text().contains("place holder"))             {                 string text = paragraph.text();                 paragraph.replacetext(text, "*******");              }         }     }     catch (exception ex)     {         ex.printstacktrace();     }     return doc; } 

input:

place holder  textvalue1 textvalue2 textvalue3 

output:

*******textvalue1 textvalue1 textvalue2 textvalue3 

the hwpf library not in perfect state changing / writing .doc files. (at least @ last time looked. time ago developed custom variant of hwpf client - among many other things - provides correct replace , save operations, library not publicly available.)

if absolutely must use .doc files , java may away replacing strings of same length. instance "12345" -> "abc__" (_ being spaces or whatever works you). might make sense find absolute location of replaced string in doc file (using hwpf) , changing in doc file directly (without using hwpf).

word file format complicated , "doing right" not trivial task. unless willing spend many man months, not possible fix part of library saving works. many data structures must handled precisely , single "slip up" lets word crash on generated output file.