Please correct me if iam sending this to a wrong distribution.
Iam trying to replace the text using replaceText method in Paragraph.
As i stated before 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.
Iam adding the code snippet for the replacement. Please correct its wrong.

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 != -)
        {
             p.replaceText(searchText,replaceTxt,offset);
        }
    }

}

On Thu, Jul 9, 2009 at 10:48 PM, karthik <[email protected]> wrote:

> Hi,
>
> I tried to replace the text using the function replaceText method in Range
> and its subclass Paragraph.
> It works fine when the length of the replacement text is equal to the
> length of the search text.
> When either of the lengths are not matching, the word document is getting
> corrupted.
> Can you suggest a way how to resolve this issue
>
> Thanks
> Karthik
>



-- 
karthik

Reply via email to