Thanks for the reply mark. I dont think i need to preserve text formatting, but i would like to try your code and see how it works. I think that would help me too.
I cant go the open office since my business requirement is to use microsoft word documents. I will be using this search and replace function in the same PC as that of the application. If you can send me that code, i will try and let u know how it works. Thanks Karthik On Fri, Aug 7, 2009 at 11:49 AM, MSB <[email protected]> wrote: > > Can I ask two questions please? > > Do you need to preserve the formatting applied to the text? If not, then I > think that somewhere I have a piece of HWPF code that does a search and > replace. I am not at all certain about the state of the code and cannot > remember if I hit the same problem as you - and I may well have - but I am > willing to look it out if you think it might help. > > Secondly, do you have to use HWPF/XWPF? The API is still immature and it is > really only suitable for realtively simple tasks. Better alternatives might > be OpenOffice which you can 'control' through it's UNO API or Word itself > that can be manipulated using OLE. You can ONLY use OLE if you are working > on a windows based PC and you have Word installed on that PC. OpenOffice is > more flexible but it still cannot be used - at least as far as I am aware - > as a document server, so it is best to have that application installed on > the PC you will be using for the search/replace operation. > > Yours > > Mark B > > > karthik-33 wrote: > > > > 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); > > > > } > > } > > > > } > > > > > > -- > View this message in context: > http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24867251.html > Sent from the POI - User mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > > -- karthik
