I have microsoft office 2007 and while saving the document, i save it as
microsoft 2003 document.
Iam trying to replace the text using replaceText method in Paragraph.
It works fine when the replacement text and search text are of equal length.
It corrupts the document, when the length of the string is either greater or
less.
If anyone has gone through the issue and resolved or have any idea. Please
let me know, it will be useful for me..
Iam not sure what is causing the problem to corrupt the document

Code is:

String replaceTxt = "Replacement";
String searchText = "Orginial";
POIFSFileSystem ps = new POIFSFileSystem (new
FileInputStream("C:/Document.doc"));
HWPFDocument doc = new HWPFDocument ();
Range range = doc.getRange();
for(int x=0;x<range.numSections();x++)
{
    Section s = range.getSection(x);
    for(int y=0;y<s.numParagraphs();y++)
    {
        Paragraph p = s.getParagraph(y);
        String paraText = p.text();
        int offset = paraText.indexOf(searchText );
        if(offset != -1)
        {
             p.replaceText(searchText,replaceTxt,offset);

        }
    }

}

Reply via email to