Hi Mark,
I have copied the code you provided as test unit (for the simplification, I
only use the first method to do my test - public void
searchAndReplace(String inputFilename, String outputFilename, String
searchTerm, String replacementText) ), but still
no luck to solve the corrupt problem. I opened the corrupt file in editPlus,
I can see the tokens have been changed. However, I cannot open the file as
Word any more.
My test is running on Windows XP, the library I used is
apache-poi-3.7-beta1.
Do you get any chance to test that?
Thank you very much,
Jingmei
MSB 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://old.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p29356490.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]