You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by karthik <ka...@gmail.com> on 2009/08/07 15:26:32 UTC

Replace Text Problem (Document Corrupt) - POI HWPFDocument

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);

        }
    }

}

Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by Jingmei Fan <ja...@hotmail.com>.
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
I tried to post again yesterday but Nabble refused to connect and accept the
message for some reason.

What I wanted to say was, do not bother with that piece of code. It exhibits
the old problem where an exception will be thrown if the replacement term is
longer than the serach term; trying to replace 'test' for example with
'tested' will break the code.

I am going to carry on looking because I am certian that I had some code
that used a more 'primitive' approach and it did seem to work. If I remember
correctly, I grabbed the text of the Pargraph into a String and then used
java code to perform any serach and replace operations; then, if any
replecamenets hade been made, I simply over-wrote the complete contents of
the Pragraph. There are still a couple of DVD's of code that I need to lok
through and if I find it, I wil post. You could always try to put something
together yourself to test the theory if you have the time. Do no know how
familiar you are with String handling is java but the indexOf() and
substring() methods formed the basis of the technique.

Yours

Mark B

PS. The only way to replace terms if they spill across CharacterRun
boundaries would be to take a step up the class hierarchy and operate on the
Paragraph object.


karthik-33 wrote:
> 
> Hi Mark, Thanks for sending this program.
> I tried this program with POI 3.2 final version, which iam currently
> using.
> CharaterRun doesnt behave consistently the same way, sometimes it splits
> the
> paragraph text into more number of Character run, sometimes it doesnt
> split
> and i see the whole paragraph text in one character run. So the search
> text
> is not getting replaced.
> Is there anyway to solve this issue?
> On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk> wrote:
> 
>>
>> Here is the HWPF based code that I put together to play around with. It
>> was
>> written a very long time ago so I am not sure what testing I undertook
>> and
>> exactly what the results were but I have run it this morning just to
>> ensure
>> that
>> it does not crash the PC and all seems to be well. This section has been
>> cut
>> from a much larger class that is full of other test code that I play
>> around
>> with peridocically. Everything you need is there I believe but on the
>> off-chance
>> that it calls another method whose source I have neglected to include,
>> just
>> drop
>> an email to the list please.
>>
>> Currently, I am running POI version 3.5 beta 7 on a PC operating under
>> Windows XP SP2. Office 2007 is installed now and it seems able to open
>> the
>> files this code produces quite happily. In the back of my mind, I seem to
>> remember that the files produced by some search and replace code I put
>> together could be opened but not modified; I tested that problem this
>> morning
>> and the files this code produces seem fine, I can open them, make changes
>> and
>> then save the results again. But do please be prepared for problems like
>> that.
>>
>> Again, can I emphasise this is test code; it is scruffy and there are
>> going
>> to
>> be variables I put in there so that I could monitor the progress of the
>> code
>> by dumping messages to the screen. As you go through, if something seems
>> to
>> be superfluous, then this is likely the reason and you can comment it out
>> or
>> delete it.
>>
>> Good luck and I do hope it all works. If you have any problems, just drop
>> a
>> message onto the list.
>>
>> Yours
>>
>> Mark B
>>
>>
>> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
>> import org.apache.poi.hwpf.HWPFDocument;
>> import org.apache.poi.hwpf.usermodel.Range;
>> import org.apache.poi.hwpf.usermodel.Paragraph;
>> import org.apache.poi.hwpf.usermodel.CharacterRun;
>>
>> import java.io.File;
>> import java.io.FileOutputStream;
>> import java.io.FileInputStream;
>> import java.io.BufferedOutputStream;
>> import java.io.BufferedInputStream;
>> import java.util.HashMap;
>> import java.util.Iterator;
>>
>> /**
>>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
>>  * CONDITIONS OF ANY KIND, either express or implied. It is not intended
>> to
>>  * be used in a 'production' environment without undergoing rigorous
>> testing.
>>  *
>>  * With that out of the way, an instance of this class can be used to
>> search
>> for
>>  * and replace Strings of text within a Word document. To see how the
>> code
>> may
>>  * be used, look into the main() method for examples.
>>  *
>>  * Note the replacements made by the code contained within this class
>> ignore
>>  * any formatting that may have been applied to the text that is
>> replaced.
>> That
>>  * is to say that if the text was originally formatted to use the Arial
>> font,
>>  * was sized to 24 points, emboldened, underlined and red in colour, then
>> all
>>  * of this will be lost if it is replaced. Further if any text is
>> replaced
>> in a
>>  * Paragraph, all the formatting applied to that Paragraph's contents is
>> likely
>>  * to be lost.
>>  *
>>  * @author Mark Beardsley [msb at apache.org]
>>  * @version 1.00 8th August 2009 (cannot remember when originally put
>> together)
>>  */
>> public class SearchReplace {
>>
>>
>>        /**
>>         * Search for and replace a single occurrence of a string of text
>> within a
>>         * Word document.
>>         *
>>         * Note that no checks are made on the parameter's values; that is
>> to say
>>         * that the file named in the InputFilename parameter will not be
>> checked
>>         * to ensure the file exists and neither of the searchTerm nor
>>         * replacementTerm parameters will be checked to ensure they are
>> not
>> null.
>>         * Also, note that I have never tested passing the same String to
>> the
>>         * inputFilename and outputFilename parameters but cannot see why
>> that
>>         * should not be possible.
>>         *
>>         * @param inputFilename An instance of the String class that
>> encapsulates
>>         *                      the name of and path to a Word document
>> which is
>>         *                      in the binary (OLE2CDF) format. The
>> contents
>> of
>> this
>>         *                      document will be searched for occurrences
>> of
>> the
>>         *                      search term.
>>         * @param outputFilename An instance of the String class that
>> encapsulates
>>         *                       the name of and path to a Word document
>> which is
>>         *                       in the binary (OLE2CDF) format. This
>> document will
>>         *                       contain the results of the search and
>> replace
>>         *                       operation.
>>         * @param searchTerm An instance of the String class that
>> encapsulates a
>>         *                   series of characters, a word or words. The
>> document
>>         *                   will be searched for occurrences of this
>> String.
>>         * @param replacementTerm An instance of the String class that
>> contains a
>>         *                        series of characters, a word or words.
>> The
>> String
>>         *                        encapsulated by the searchTerm parameter
>> will be
>>         *                        replaced by the 'contents' of this
>> parameter.
>>         *
>>         */
>>        public void searchAndReplace(String inputFilename,
>>                                 String outputFilename,
>>                                 String searchTerm,
>>                                 String replacementText) {
>>
>>        File inputFile = null;
>>        File outputFile = null;
>>        FileInputStream fileIStream = null;
>>        FileOutputStream fileOStream = null;
>>        BufferedInputStream bufIStream = null;
>>        BufferedOutputStream bufOStream = null;
>>        POIFSFileSystem fileSystem = null;
>>        HWPFDocument document = null;
>>        Range docRange = null;
>>        Paragraph paragraph = null;
>>        CharacterRun charRun = null;
>>        int numParagraphs = 0;
>>        int numCharRuns = 0;
>>        String text = null;
>>
>>        try {
>>            // Create an instance of the POIFSFileSystem class and
>>            // attach it to the Word document using an InputStream.
>>            inputFile = new File(inputFilename);
>>            fileIStream = new FileInputStream(inputFile);
>>            bufIStream = new BufferedInputStream(fileIStream);
>>            fileSystem = new POIFSFileSystem(bufIStream);
>>            document = new HWPFDocument(fileSystem);
>>
>>            // Get the overall Range object for the document. Note the
>>            // use of the getRange() method and not the getOverallRange()
>>            // method, this is just historic - when the code was
>> originally
>>            // written, I do not believe the latter method was part of the
>> API.
>>            docRange = document.getRange();
>>
>>            // Get the number of Paragraph(s) in the overall range and
>> iterate
>>            // through them
>>            numParagraphs = docRange.numParagraphs();
>>            for(int i = 0; i < numParagraphs; i++) {
>>
>>                // Get a Paragraph and recover the text from it. This step
>> is
>> far from
>>                // necessary and I think I only got the text so that I
>> could
>> print
>>                // it to screen as a diagnostic check to ensure that the
>> Paragraph
>>                // contained the text I was searching for. Experiment with
>> this.
>>                paragraph = docRange.getParagraph(i);
>>                text = paragraph.text();
>>
>>                // Get the number of CharacterRuns in the Paragraph
>>                numCharRuns = paragraph.numCharacterRuns();
>>                for(int j = 0; j < numCharRuns; j++) {
>>
>>                        // Get a character run and recover it's text -
>> note
>> that
>>                        // the same text variable is used as for the
>> Paragraph
>> above.
>>                        // So, it MUST be safe to remove the text =
>> paragraph.text()
>>                        // line above.
>>                    charRun = paragraph.getCharacterRun(j);
>>                    text = charRun.text();
>>
>>                    // Check to see if the text of the CharacterRun
>> contains
>> the
>>                    // search term. If it does, find out where that term
>> starts
>>                    // and call the replaceText() method passing the
>> index.
>>                    // Maybe this is the key difference between what we
>> are
>>                    // doing.
>>                    if(text.contains(searchTerm)) {
>>                        int start = text.indexOf(searchTerm);
>>                        charRun.replaceText(searchTerm, replacementText,
>> start);
>>                    }
>>                }
>>            }
>>
>>            // Close the InputStream
>>            bufIStream.close();
>>            bufIStream = null;
>>
>>            // Open an OutputStream and write the document away.
>>            outputFile = new File(outputFilename);
>>            fileOStream = new FileOutputStream(outputFile);
>>            bufOStream = new BufferedOutputStream(fileOStream);
>>
>>            document.write(bufOStream);
>>
>>        }
>>        catch(Exception ex) {
>>            System.out.println("Caught an: " + ex.getClass().getName());
>>            System.out.println("Message: " + ex.getMessage());
>>            System.out.println("Stacktrace follows.............");
>>            ex.printStackTrace(System.out);
>>        }
>>        finally {
>>            if(bufOStream != null) {
>>                try {
>>                    //bufOStream.flush();
>>                    bufOStream.close();
>>                    bufOStream = null;
>>                }
>>                catch(Exception ex) {
>>
>>                }
>>            }
>>            if(bufIStream != null) {
>>                try {
>>                    bufIStream.close();
>>                    bufIStream = null;
>>                }
>>                catch(Exception ex) {
>>                    // I G N O R E //
>>                }
>>            }
>>        }
>>
>>    }
>>
>>    /**
>>         * Search for and replace a single occurrence of a string of text
>> within a
>>         * Word document.
>>         *
>>         * Note that no checks are made on the parameter's values; that is
>> to say
>>         * that the file named in the InputFilename parameter will not be
>> checked
>>         * to ensure the file exists and neither of the searchTerm nor
>>         * replacementTerm pare,eters will be checked to ensure they are
>> not
>> null.
>>         * Also, note that I have never tested passing the same String to
>> the
>>         * inputFilename and outputFilename parameters but cannot see why
>> that
>>         * should not be possible.
>>         *
>>         * @param inputFilename An instance of the String class that
>> encapsulates
>>         *                      the name of and path to a Word document
>> which is
>>         *                      in the binary (OLE2CDF) format. The
>> contents
>> of
>> this
>>         *                      document will be searched for occurrences
>> of
>> the
>>         *                      search term.
>>         * @param outputFilename An instance of the String class that
>> encapsulates
>>         *                       the name of and path to a Word document
>> which is
>>         *                       in the binary (OLE2CDF) format. This
>> document will
>>         *                       contain the results of the search and
>> replace
>>         *                       operation.
>>         * @param replacements An instance of the java.util.HashMap class
>> that
>>         *                     contains a series of key, value pairs. Each
>> key
>>         *                     is an instance of the String class that
>> encapsulates
>>         *                     a series of characters, a word or words
>> that
>> the
>>         *                     code will search for and the accompanying
>> value is
>>         *                     also an instance of the String class that
>> likewise
>>         *                     encapsulates a series of characters, a word
>> or
>> words.
>>         *                     The 'contents' of the value's String will
>> be
>> used to
>>         *                     replace the contents of the key's String if
>> an
>>         *                     occurrence of the latter is found.
>>         */
>>    public void searchAndReplace(String inputFilename,
>>                                 String outputFilename,
>>                                 HashMap<String, String> replacements) {
>>
>>        File inputFile = null;
>>        File outputFile = null;
>>        FileInputStream fileIStream = null;
>>        FileOutputStream fileOStream = null;
>>        BufferedInputStream bufIStream = null;
>>        BufferedOutputStream bufOStream = null;
>>        POIFSFileSystem fileSystem = null;
>>        HWPFDocument document = null;
>>        Range docRange = null;
>>        Paragraph paragraph = null;
>>        CharacterRun charRun = null;
>>        Set<String> keySet = null;
>>        Iterator<String> keySetIterator = null;
>>        int numParagraphs = 0;
>>        int numCharRuns = 0;
>>        String text = null;
>>        String key = null;
>>        String value = null;
>>
>>        try {
>>            // Create an instance of the POIFSFileSystem class and
>>            // attach it to the Word document using an InputStream.
>>            inputFile = new File(inputFilename);
>>            fileIStream = new FileInputStream(inputFile);
>>            bufIStream = new BufferedInputStream(fileIStream);
>>            fileSystem = new POIFSFileSystem(bufIStream);
>>            document = new HWPFDocument(fileSystem);
>>
>>                        // Get a reference to the overall Range for the
>> document
>>                        // and discover how many Paragraphs objects there
>> are
>>                        // in the document.
>>            docRange = document.getRange();
>>            numParagraphs = docRange.numParagraphs();
>>
>>            // Recover a Set of the keys in the HashMap
>>            keySet = replacements.keySet();
>>
>>            // Step through each Paragraph
>>            for(int i = 0; i < numParagraphs; i++) {
>>                paragraph = docRange.getParagraph(i);
>>                // This line can almost certainly be removed - see
>>                // the comments in the method above.
>>                text = paragraph.text();
>>
>>                // Get the number of CharacterRuns in the Paragraph
>>                // and step through each one.
>>                numCharRuns = paragraph.numCharacterRuns();
>>                for(int j = 0; j < numCharRuns; j++) {
>>                    charRun = paragraph.getCharacterRun(j);
>>
>>                    // Get the text from the CharacterRun and recover an
>>                    // Iterator to step through the Set of keys.
>>                    text = charRun.text();
>>                    keySetIterator = keySet.iterator();
>>                    while(keySetIterator.hasNext()) {
>>
>>                        // Get the key - which is also the search term -
>> and
>>                        // check to see if it can be found within the
>>                        // CharacterRuns text.
>>                        key = keySetIterator.next();
>>                        if(text.contains(key)) {
>>
>>                                // If the search term was found in the
>> text,
>> get
>> the
>>                                // matching value from the HashMap, find
>> out
>> whereabouts
>>                                // in the CharacterRuns text the search
>> term
>> is
>>                                // and call the replaceText() method to
>> substitute
>>                                // the replacement term for the search
>> term.
>>                            value = replacements.get(key);
>>                            int start = text.indexOf(key);
>>                            charRun.replaceText(key, value, start);
>>
>>                            // Note that this code was added to test
>> whether
>>                            // it was possible to replace multiple
>> occurrences
>>                            // of the search term. I cannot remember if I
>> tested
>>                            // it but believe that it did work; either
>> way,
>>                            // it could be tested now and if succeeds,
>> then
>> the
>>                            // searchAndReplace() method above could be
>> modified
>>                            // to include this.
>>                            docRange = document.getRange();
>>                            paragraph = docRange.getParagraph(i);
>>                            charRun = paragraph.getCharacterRun(j);
>>                            text = charRun.text();
>>                        }
>>                    }
>>                }
>>            }
>>
>>            // Close the InputStream
>>            bufIStream.close();
>>            bufIStream = null;
>>
>>            // Open an OutputStream and save the modified document away.
>>            outputFile = new File(outputFilename);
>>            fileOStream = new FileOutputStream(outputFile);
>>            bufOStream = new BufferedOutputStream(fileOStream);
>>            document.write(bufOStream);
>>        }
>>        catch(Exception ex) {
>>            System.out.println("Caught an: " + ex.getClass().getName());
>>            System.out.println("Message: " + ex.getMessage());
>>            System.out.println("Stacktrace follows.............");
>>            ex.printStackTrace(System.out);
>>        }
>>        finally {
>>            if(bufIStream != null) {
>>                try {
>>                    bufIStream.close();
>>                    bufIStream = null;
>>                }
>>                catch(Exception ex) {
>>                    // I G N O R E //
>>                }
>>            }
>>            if(bufOStream != null) {
>>                try {
>>                    bufOStream.flush();
>>                    bufOStream.close();
>>                    bufOStream = null;
>>                }
>>                catch(Exception ex) {
>>
>>                }
>>            }
>>        }
>>
>>    }
>>
>>        /**
>>         * The main entry point to the program demonstrating how the code
>> may
>>         * be utilised.
>>         *
>>         * @param args An array of type String containing argumnets passed
>> to the
>>         *             program on execution.
>>         */
>>        public static void main(String[] args) {
>>                SearchReplace replacer = new SearchReplace();
>>
>>                // To serach for and replace single items. Note, the code
>> has not, at
>>                // least as far as I can remember, been tested by passing
>> the same
>>                // file to both the searchTerm and replacementTerm
>> parameters. It ought
>>                // to work but has NOT been tested I believe.
>>                replacer.searchAndReplace("Document.doc",            //
>> Source Document
>>                        "Replaced Document.doc",                        
>> //
>> Result Document
>>                        "search term",                                  
>> //
>> Search term
>>                        "replacement term");                            
>> //
>> Replacement term
>>
>>                // To search for and replace a series of items
>>                HashMap<String, String> searchTerms = new HashMap<String,
>> String>();
>>                searchTerms.put("search term 1", "replacement term 1");
>>                searchTerms.put("search term 2", "replacement term 2");
>>                searchTerms.put("search term 3", "replacement term 3");
>>                searchTerms.put("search term 4", "replacement term 4");
>>
>>                replacer.searchAndReplace("Document.doc",    // Source
>> Document
>>                        "Replaced Document.doc",                 // Result
>> Document
>>                        searchTerms)                             //
>> Search/replacement items
>>         }
>> }
>>
>>
>>
>> karthik-33 wrote:
>> >
>> > 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 <ma...@tiscali.co.uk> 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: user-unsubscribe@poi.apache.org
>> >> For additional commands, e-mail: user-help@poi.apache.org
>> >>
>> >>
>> >
>> >
>> > --
>> > karthik
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
>>  Sent from the POI - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
> 
> 
> -- 
> karthik
> 
> 

-- 
View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24884574.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
I am sorry to say that both OLE and it's more recent cousin COM are Windows
only techniques; neither can be used on the AS400. Your best bet would be to
concentrate of patching HWPF I think as it will run on any java virtual
machine. I have not investigated the problem myself but it seems as though
the Range class is unable to determine the bounds of the text correctly and
think that the start and end points either of the existsing text or the
replacement text have become corrupted. There is a method called something
like sanityCheck() and that seems to be where the confusion arises. I think
that if you could fix this, then you may be able to run with the first chunk
of code I posted; the bit that gets the overall Range object for the
document, from that all of the Paragraph objects and then steps through each
of these and iterates through their CharacterRun(s) to perform the
replacement operation.

For a couple of days now, I have been meaning to mention a thought I had
about the problem you encountered using CharacterRun(s) as opposed to
Paragraph(s) in the search and replace operation; if you remember, you had
multiple word search terms and they were split between CharacterRun
instances. All you would need to do IMO is to change the logic of the search
and replace code slightly. If it finds the first word of the search term in
a CharacterUn, then get the following character run and test to see if it
contains the second or subsequent words in the search term. If it does then
you can simply replace each part of the search term as appropriate. This
should hold true even if you find that the search trem is split over many
CharacterRun(s) I think.

As always, if you need any help, just drop a message onto the list; that
goes for devloping as well as using HWPF.

Yours

Mark B


karthik-33 wrote:
> 
> Thanks Mark. You have been very helpful in assisting me.
> I would defintely give a try to SWT/OLE and let you know how it works.
> I have one question though regarding SWT/OLE, iam going to deploy this
> java
> code in AS400 system.
> We have java installed in AS400 system and POI works in AS400 like
> windows.
> But not sure for this SWT/OLE. Do you have an idea on this or i can try it
> and see.
> 
> I like to download the code and try it. I have to install SVN and
> meanwhile
> i will try with
> SWT/OLE. I will let u know how it goes.
> 
> Thanks
> Karthik
> 
> On Mon, Aug 10, 2009 at 2:07 AM, MSB <ma...@tiscali.co.uk> wrote:
> 
>>
>> HWPF is still very immature and I do not think that with the project in
>> it's
>> current state there is a way to do any better than we already have,
>> sorry.
>> With regard to the SWT/OLE technique, can I ask did you download from
>> Rapidshare the example I posted? If so, then you need to unzip the
>> archive
>> into a folder and open the file called readme.rtf. It contains
>> instructions
>> telling you how to proceed. Before going any further, I do need to stress
>> again that OLE will only work if you are running on a Windpws PC and a
>> copy
>> of Word is installed onto the machine where you will be running the code.
>>
>> Off of the top of my head, the first thing to do is unzip the SWT archive
>> -
>> is has a name something like swt-3.2.3-win32.zip and contains two files
>> you
>> are interested in. The first is the java achive which is called swt.jar.
>> Extract that into a folder and place it on your classpath so the code I
>> sent
>> will compile and run. Secondly, you need to extract a dll and place it
>> into
>> the system32 subfolder of your windows folder on the computers hard
>> drive.
>> Again, I cannot remember the name of the dll exactly but think it will be
>> something like this - swt-win32-3236.dll.
>>
>> Now, you are ready to compile and run the file of code that is also
>> contained in the archive. Extract it to a folder somewhere on you
>> computer,
>> open it using whichever editor you like to use, make sure that the
>> -classpath environment variable is set so that it can access the swt.jar
>> archive and then you should be able to compile and run the code. Usually,
>> I
>> place some code into the main() method that explains what to do and how
>> to
>> run the class and if I remember correctly, that class is very simple to
>> use
>> and you may be able to deduce from the code that is there how to search
>> for
>> and replace strings of text on Word documents. At this stage, do not
>> worry
>> at all about how the class does what it does. There is a vile mixture of
>> VBA
>> and java in there which will be hard to decipher until you are a little
>> more
>> familiar with the OLE technique. The class should run and ought to
>> successfully search for and replace items in a Word document. It will
>> seem
>> to run slowly. This is because in the background, it is loading Word,
>> locating the file, perfroming the search and replace operation, saving
>> the
>> file away again and then closing Word down.
>>
>> As to patching HWPF, the first thing you need to do is figure out where
>> the
>> problem is and the best way to accomplish this is to try to replace a
>> string
>> of text with one that is far longer. HWPF should respond by throwing an
>> exception and the stacktrace will help you to identify which class and
>> which
>> method is the source of that exception. Next, have a look at this page -
>> http://poi.apache.org/getinvolved/index.html - it explains a little of
>> how
>> to get involved with developing the API. It asumes that you will commit
>> your
>> changes to the project - which you are not of course forced to do - but
>> is
>> a
>> good place to start. Then, as suggested on that page, use SVN
>> (subversion)
>> to checkout a copy of the project's code and see if you can figure out
>> where
>> the changes need to be made and, if so, modify your checked out copy of
>> the
>> project's code. Once you get to that poiont, you can think about whether
>> or
>> not you wish to submit the changes as a patch and, if so, the easiest way
>> to
>> do this would be to post to the list again saying that you have a patch
>> you
>> would like to commit. Someone will respond and help you to commit the
>> patch
>> to the project.
>>
>> Sorry to say that I am not the best person to talk you through the
>> process
>> of developing and commiting patches as I am still just working through
>> that
>> myself. If you do want to work on HWPF after looking at the code then I
>> would suggest contacting Nick Burch - nick@apache.org - the project chair
>> and asking him how to proceed.
>>
>> Yours
>>
>> Mark B
>>
>>
>> karthik-33 wrote:
>> >
>> > Mark, Thanks for trying out. I tried the same way you did...
>> > Can you suggest me a way to resolve this..
>> > What is the option one to try patch HWPF.. May be i can try if you have
>> > any
>> > new way to approach this problem...
>> > And the option two, SWT/OLE iam new to it and i dont know how to work
>> on
>> > that..
>> >
>> > Thanks
>> > Karthik
>> > On Sun, Aug 9, 2009 at 6:07 AM, MSB <ma...@tiscali.co.uk> wrote:
>> >
>> >>
>> >> This morning, I had the chance to try what I spoke of in my previous
>> >> email
>> >> -
>> >> using Java to replace text in a copy of the Paragraph's text and then
>> >> replacing all of the text in the Paragraph with the modified copy. It
>> >> worked
>> >> but only up to a point; if the replacement text was exactly the same
>> >> length
>> >> as the search term, this technique worked but any differences in
>> length
>> >> rendered the resulting file corrupt; and I am guessing that this is
>> the
>> >> same
>> >> problem as you originally encountered.
>> >>
>> >> So, I think I can conclude that if you are trying to replace the
>> search
>> >> term
>> >> with a String of text that is longer than it, you will run into
>> problems.
>> >> As
>> >> long as the replacement is shorter than the search term - or at most
>> the
>> >> same length - then the previous piece of code I posted seems to work
>> well
>> >> enough.
>> >>
>> >> To my mind then you have two options. Option 1 would be to patch HWPF
>> so
>> >> that it will work as you wish - the API is very immature and has not
>> been
>> >> the focus of the same sort of development effort as has HSSF for
>> example.
>> >> Option 2 is to use an alternative such as SWT/OLE or OpenOffice. The
>> >> limitation with the OpenOffice approach is that whilst it can read
>> >> OpenXML
>> >> documents - Office 2007 and beyond with the .docx or similar extension
>> -
>> >> it
>> >> cannot save a document in this format.
>> >>
>> >> Sorry for the bad news.
>> >>
>> >> Yours
>> >>
>> >> Mark B
>> >>
>> >>
>> >> karthik-33 wrote:
>> >> >
>> >> > Hi Mark, Thanks for sending this program.
>> >> > I tried this program with POI 3.2 final version, which iam currently
>> >> > using.
>> >> > CharaterRun doesnt behave consistently the same way, sometimes it
>> >> splits
>> >> > the
>> >> > paragraph text into more number of Character run, sometimes it
>> doesnt
>> >> > split
>> >> > and i see the whole paragraph text in one character run. So the
>> search
>> >> > text
>> >> > is not getting replaced.
>> >> > Is there anyway to solve this issue?
>> >> > On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk>
>> wrote:
>> >> >
>> >> >>
>> >> >> Here is the HWPF based code that I put together to play around
>> with.
>> >> It
>> >> >> was
>> >> >> written a very long time ago so I am not sure what testing I
>> undertook
>> >> >> and
>> >> >> exactly what the results were but I have run it this morning just
>> to
>> >> >> ensure
>> >> >> that
>> >> >> it does not crash the PC and all seems to be well. This section has
>> >> been
>> >> >> cut
>> >> >> from a much larger class that is full of other test code that I
>> play
>> >> >> around
>> >> >> with peridocically. Everything you need is there I believe but on
>> the
>> >> >> off-chance
>> >> >> that it calls another method whose source I have neglected to
>> include,
>> >> >> just
>> >> >> drop
>> >> >> an email to the list please.
>> >> >>
>> >> >> Currently, I am running POI version 3.5 beta 7 on a PC operating
>> under
>> >> >> Windows XP SP2. Office 2007 is installed now and it seems able to
>> open
>> >> >> the
>> >> >> files this code produces quite happily. In the back of my mind, I
>> seem
>> >> to
>> >> >> remember that the files produced by some search and replace code I
>> put
>> >> >> together could be opened but not modified; I tested that problem
>> this
>> >> >> morning
>> >> >> and the files this code produces seem fine, I can open them, make
>> >> changes
>> >> >> and
>> >> >> then save the results again. But do please be prepared for problems
>> >> like
>> >> >> that.
>> >> >>
>> >> >> Again, can I emphasise this is test code; it is scruffy and there
>> are
>> >> >> going
>> >> >> to
>> >> >> be variables I put in there so that I could monitor the progress of
>> >> the
>> >> >> code
>> >> >> by dumping messages to the screen. As you go through, if something
>> >> seems
>> >> >> to
>> >> >> be superfluous, then this is likely the reason and you can comment
>> it
>> >> out
>> >> >> or
>> >> >> delete it.
>> >> >>
>> >> >> Good luck and I do hope it all works. If you have any problems,
>> just
>> >> drop
>> >> >> a
>> >> >> message onto the list.
>> >> >>
>> >> >> Yours
>> >> >>
>> >> >> Mark B
>> >> >>
>> >> >>
>> >> >> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
>> >> >> import org.apache.poi.hwpf.HWPFDocument;
>> >> >> import org.apache.poi.hwpf.usermodel.Range;
>> >> >> import org.apache.poi.hwpf.usermodel.Paragraph;
>> >> >> import org.apache.poi.hwpf.usermodel.CharacterRun;
>> >> >>
>> >> >> import java.io.File;
>> >> >> import java.io.FileOutputStream;
>> >> >> import java.io.FileInputStream;
>> >> >> import java.io.BufferedOutputStream;
>> >> >> import java.io.BufferedInputStream;
>> >> >> import java.util.HashMap;
>> >> >> import java.util.Iterator;
>> >> >>
>> >> >> /**
>> >> >>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
>> >> >>  * CONDITIONS OF ANY KIND, either express or implied. It is not
>> >> intended
>> >> >> to
>> >> >>  * be used in a 'production' environment without undergoing
>> rigorous
>> >> >> testing.
>> >> >>  *
>> >> >>  * With that out of the way, an instance of this class can be used
>> to
>> >> >> search
>> >> >> for
>> >> >>  * and replace Strings of text within a Word document. To see how
>> the
>> >> >> code
>> >> >> may
>> >> >>  * be used, look into the main() method for examples.
>> >> >>  *
>> >> >>  * Note the replacements made by the code contained within this
>> class
>> >> >> ignore
>> >> >>  * any formatting that may have been applied to the text that is
>> >> >> replaced.
>> >> >> That
>> >> >>  * is to say that if the text was originally formatted to use the
>> >> Arial
>> >> >> font,
>> >> >>  * was sized to 24 points, emboldened, underlined and red in
>> colour,
>> >> then
>> >> >> all
>> >> >>  * of this will be lost if it is replaced. Further if any text is
>> >> >> replaced
>> >> >> in a
>> >> >>  * Paragraph, all the formatting applied to that Paragraph's
>> contents
>> >> is
>> >> >> likely
>> >> >>  * to be lost.
>> >> >>  *
>> >> >>  * @author Mark Beardsley [msb at apache.org]
>> >> >>  * @version 1.00 8th August 2009 (cannot remember when originally
>> put
>> >> >> together)
>> >> >>  */
>> >> >> public class SearchReplace {
>> >> >>
>> >> >>
>> >> >>        /**
>> >> >>         * Search for and replace a single occurrence of a string of
>> >> text
>> >> >> within a
>> >> >>         * Word document.
>> >> >>         *
>> >> >>         * Note that no checks are made on the parameter's values;
>> that
>> >> is
>> >> >> to say
>> >> >>         * that the file named in the InputFilename parameter will
>> not
>> >> be
>> >> >> checked
>> >> >>         * to ensure the file exists and neither of the searchTerm
>> nor
>> >> >>         * replacementTerm parameters will be checked to ensure they
>> >> are
>> >> >> not
>> >> >> null.
>> >> >>         * Also, note that I have never tested passing the same
>> String
>> >> to
>> >> >> the
>> >> >>         * inputFilename and outputFilename parameters but cannot
>> see
>> >> why
>> >> >> that
>> >> >>         * should not be possible.
>> >> >>         *
>> >> >>         * @param inputFilename An instance of the String class that
>> >> >> encapsulates
>> >> >>         *                      the name of and path to a Word
>> document
>> >> >> which is
>> >> >>         *                      in the binary (OLE2CDF) format. The
>> >> >> contents
>> >> >> of
>> >> >> this
>> >> >>         *                      document will be searched for
>> >> occurrences
>> >> >> of
>> >> >> the
>> >> >>         *                      search term.
>> >> >>         * @param outputFilename An instance of the String class
>> that
>> >> >> encapsulates
>> >> >>         *                       the name of and path to a Word
>> >> document
>> >> >> which is
>> >> >>         *                       in the binary (OLE2CDF) format.
>> This
>> >> >> document will
>> >> >>         *                       contain the results of the search
>> and
>> >> >> replace
>> >> >>         *                       operation.
>> >> >>         * @param searchTerm An instance of the String class that
>> >> >> encapsulates a
>> >> >>         *                   series of characters, a word or words.
>> The
>> >> >> document
>> >> >>         *                   will be searched for occurrences of
>> this
>> >> >> String.
>> >> >>         * @param replacementTerm An instance of the String class
>> that
>> >> >> contains a
>> >> >>         *                        series of characters, a word or
>> >> words.
>> >> >> The
>> >> >> String
>> >> >>         *                        encapsulated by the searchTerm
>> >> parameter
>> >> >> will be
>> >> >>         *                        replaced by the 'contents' of this
>> >> >> parameter.
>> >> >>         *
>> >> >>         */
>> >> >>        public void searchAndReplace(String inputFilename,
>> >> >>                                 String outputFilename,
>> >> >>                                 String searchTerm,
>> >> >>                                 String replacementText) {
>> >> >>
>> >> >>        File inputFile = null;
>> >> >>        File outputFile = null;
>> >> >>        FileInputStream fileIStream = null;
>> >> >>        FileOutputStream fileOStream = null;
>> >> >>        BufferedInputStream bufIStream = null;
>> >> >>        BufferedOutputStream bufOStream = null;
>> >> >>        POIFSFileSystem fileSystem = null;
>> >> >>        HWPFDocument document = null;
>> >> >>        Range docRange = null;
>> >> >>        Paragraph paragraph = null;
>> >> >>        CharacterRun charRun = null;
>> >> >>        int numParagraphs = 0;
>> >> >>        int numCharRuns = 0;
>> >> >>        String text = null;
>> >> >>
>> >> >>        try {
>> >> >>            // Create an instance of the POIFSFileSystem class and
>> >> >>            // attach it to the Word document using an InputStream.
>> >> >>            inputFile = new File(inputFilename);
>> >> >>            fileIStream = new FileInputStream(inputFile);
>> >> >>            bufIStream = new BufferedInputStream(fileIStream);
>> >> >>            fileSystem = new POIFSFileSystem(bufIStream);
>> >> >>            document = new HWPFDocument(fileSystem);
>> >> >>
>> >> >>            // Get the overall Range object for the document. Note
>> the
>> >> >>            // use of the getRange() method and not the
>> >> getOverallRange()
>> >> >>            // method, this is just historic - when the code was
>> >> >> originally
>> >> >>            // written, I do not believe the latter method was part
>> of
>> >> the
>> >> >> API.
>> >> >>            docRange = document.getRange();
>> >> >>
>> >> >>            // Get the number of Paragraph(s) in the overall range
>> and
>> >> >> iterate
>> >> >>            // through them
>> >> >>            numParagraphs = docRange.numParagraphs();
>> >> >>            for(int i = 0; i < numParagraphs; i++) {
>> >> >>
>> >> >>                // Get a Paragraph and recover the text from it.
>> This
>> >> step
>> >> >> is
>> >> >> far from
>> >> >>                // necessary and I think I only got the text so that
>> I
>> >> >> could
>> >> >> print
>> >> >>                // it to screen as a diagnostic check to ensure that
>> >> the
>> >> >> Paragraph
>> >> >>                // contained the text I was searching for.
>> Experiment
>> >> with
>> >> >> this.
>> >> >>                paragraph = docRange.getParagraph(i);
>> >> >>                text = paragraph.text();
>> >> >>
>> >> >>                // Get the number of CharacterRuns in the Paragraph
>> >> >>                numCharRuns = paragraph.numCharacterRuns();
>> >> >>                for(int j = 0; j < numCharRuns; j++) {
>> >> >>
>> >> >>                        // Get a character run and recover it's text
>> -
>> >> >> note
>> >> >> that
>> >> >>                        // the same text variable is used as for the
>> >> >> Paragraph
>> >> >> above.
>> >> >>                        // So, it MUST be safe to remove the text =
>> >> >> paragraph.text()
>> >> >>                        // line above.
>> >> >>                    charRun = paragraph.getCharacterRun(j);
>> >> >>                    text = charRun.text();
>> >> >>
>> >> >>                    // Check to see if the text of the CharacterRun
>> >> >> contains
>> >> >> the
>> >> >>                    // search term. If it does, find out where that
>> >> term
>> >> >> starts
>> >> >>                    // and call the replaceText() method passing the
>> >> >> index.
>> >> >>                    // Maybe this is the key difference between what
>> we
>> >> >> are
>> >> >>                    // doing.
>> >> >>                    if(text.contains(searchTerm)) {
>> >> >>                        int start = text.indexOf(searchTerm);
>> >> >>                        charRun.replaceText(searchTerm,
>> >> replacementText,
>> >> >> start);
>> >> >>                    }
>> >> >>                }
>> >> >>            }
>> >> >>
>> >> >>            // Close the InputStream
>> >> >>            bufIStream.close();
>> >> >>            bufIStream = null;
>> >> >>
>> >> >>            // Open an OutputStream and write the document away.
>> >> >>            outputFile = new File(outputFilename);
>> >> >>            fileOStream = new FileOutputStream(outputFile);
>> >> >>            bufOStream = new BufferedOutputStream(fileOStream);
>> >> >>
>> >> >>            document.write(bufOStream);
>> >> >>
>> >> >>        }
>> >> >>        catch(Exception ex) {
>> >> >>            System.out.println("Caught an: " +
>> >> ex.getClass().getName());
>> >> >>            System.out.println("Message: " + ex.getMessage());
>> >> >>            System.out.println("Stacktrace follows.............");
>> >> >>            ex.printStackTrace(System.out);
>> >> >>        }
>> >> >>        finally {
>> >> >>            if(bufOStream != null) {
>> >> >>                try {
>> >> >>                    //bufOStream.flush();
>> >> >>                    bufOStream.close();
>> >> >>                    bufOStream = null;
>> >> >>                }
>> >> >>                catch(Exception ex) {
>> >> >>
>> >> >>                }
>> >> >>            }
>> >> >>            if(bufIStream != null) {
>> >> >>                try {
>> >> >>                    bufIStream.close();
>> >> >>                    bufIStream = null;
>> >> >>                }
>> >> >>                catch(Exception ex) {
>> >> >>                    // I G N O R E //
>> >> >>                }
>> >> >>            }
>> >> >>        }
>> >> >>
>> >> >>    }
>> >> >>
>> >> >>    /**
>> >> >>         * Search for and replace a single occurrence of a string of
>> >> text
>> >> >> within a
>> >> >>         * Word document.
>> >> >>         *
>> >> >>         * Note that no checks are made on the parameter's values;
>> that
>> >> is
>> >> >> to say
>> >> >>         * that the file named in the InputFilename parameter will
>> not
>> >> be
>> >> >> checked
>> >> >>         * to ensure the file exists and neither of the searchTerm
>> nor
>> >> >>         * replacementTerm pare,eters will be checked to ensure they
>> >> are
>> >> >> not
>> >> >> null.
>> >> >>         * Also, note that I have never tested passing the same
>> String
>> >> to
>> >> >> the
>> >> >>         * inputFilename and outputFilename parameters but cannot
>> see
>> >> why
>> >> >> that
>> >> >>         * should not be possible.
>> >> >>         *
>> >> >>         * @param inputFilename An instance of the String class that
>> >> >> encapsulates
>> >> >>         *                      the name of and path to a Word
>> document
>> >> >> which is
>> >> >>         *                      in the binary (OLE2CDF) format. The
>> >> >> contents
>> >> >> of
>> >> >> this
>> >> >>         *                      document will be searched for
>> >> occurrences
>> >> >> of
>> >> >> the
>> >> >>         *                      search term.
>> >> >>         * @param outputFilename An instance of the String class
>> that
>> >> >> encapsulates
>> >> >>         *                       the name of and path to a Word
>> >> document
>> >> >> which is
>> >> >>         *                       in the binary (OLE2CDF) format.
>> This
>> >> >> document will
>> >> >>         *                       contain the results of the search
>> and
>> >> >> replace
>> >> >>         *                       operation.
>> >> >>         * @param replacements An instance of the java.util.HashMap
>> >> class
>> >> >> that
>> >> >>         *                     contains a series of key, value
>> pairs.
>> >> Each
>> >> >> key
>> >> >>         *                     is an instance of the String class
>> that
>> >> >> encapsulates
>> >> >>         *                     a series of characters, a word or
>> words
>> >> >> that
>> >> >> the
>> >> >>         *                     code will search for and the
>> >> accompanying
>> >> >> value is
>> >> >>         *                     also an instance of the String class
>> >> that
>> >> >> likewise
>> >> >>         *                     encapsulates a series of characters,
>> a
>> >> word
>> >> >> or
>> >> >> words.
>> >> >>         *                     The 'contents' of the value's String
>> >> will
>> >> >> be
>> >> >> used to
>> >> >>         *                     replace the contents of the key's
>> String
>> >> if
>> >> >> an
>> >> >>         *                     occurrence of the latter is found.
>> >> >>         */
>> >> >>    public void searchAndReplace(String inputFilename,
>> >> >>                                 String outputFilename,
>> >> >>                                 HashMap<String, String>
>> replacements)
>> >> {
>> >> >>
>> >> >>        File inputFile = null;
>> >> >>        File outputFile = null;
>> >> >>        FileInputStream fileIStream = null;
>> >> >>        FileOutputStream fileOStream = null;
>> >> >>        BufferedInputStream bufIStream = null;
>> >> >>        BufferedOutputStream bufOStream = null;
>> >> >>        POIFSFileSystem fileSystem = null;
>> >> >>        HWPFDocument document = null;
>> >> >>        Range docRange = null;
>> >> >>        Paragraph paragraph = null;
>> >> >>        CharacterRun charRun = null;
>> >> >>        Set<String> keySet = null;
>> >> >>        Iterator<String> keySetIterator = null;
>> >> >>        int numParagraphs = 0;
>> >> >>        int numCharRuns = 0;
>> >> >>        String text = null;
>> >> >>        String key = null;
>> >> >>        String value = null;
>> >> >>
>> >> >>        try {
>> >> >>            // Create an instance of the POIFSFileSystem class and
>> >> >>            // attach it to the Word document using an InputStream.
>> >> >>            inputFile = new File(inputFilename);
>> >> >>            fileIStream = new FileInputStream(inputFile);
>> >> >>            bufIStream = new BufferedInputStream(fileIStream);
>> >> >>            fileSystem = new POIFSFileSystem(bufIStream);
>> >> >>            document = new HWPFDocument(fileSystem);
>> >> >>
>> >> >>                        // Get a reference to the overall Range for
>> the
>> >> >> document
>> >> >>                        // and discover how many Paragraphs objects
>> >> there
>> >> >> are
>> >> >>                        // in the document.
>> >> >>            docRange = document.getRange();
>> >> >>            numParagraphs = docRange.numParagraphs();
>> >> >>
>> >> >>            // Recover a Set of the keys in the HashMap
>> >> >>            keySet = replacements.keySet();
>> >> >>
>> >> >>            // Step through each Paragraph
>> >> >>            for(int i = 0; i < numParagraphs; i++) {
>> >> >>                paragraph = docRange.getParagraph(i);
>> >> >>                // This line can almost certainly be removed - see
>> >> >>                // the comments in the method above.
>> >> >>                text = paragraph.text();
>> >> >>
>> >> >>                // Get the number of CharacterRuns in the Paragraph
>> >> >>                // and step through each one.
>> >> >>                numCharRuns = paragraph.numCharacterRuns();
>> >> >>                for(int j = 0; j < numCharRuns; j++) {
>> >> >>                    charRun = paragraph.getCharacterRun(j);
>> >> >>
>> >> >>                    // Get the text from the CharacterRun and
>> recover
>> >> an
>> >> >>                    // Iterator to step through the Set of keys.
>> >> >>                    text = charRun.text();
>> >> >>                    keySetIterator = keySet.iterator();
>> >> >>                    while(keySetIterator.hasNext()) {
>> >> >>
>> >> >>                        // Get the key - which is also the search
>> term
>> >> -
>> >> >> and
>> >> >>                        // check to see if it can be found within
>> the
>> >> >>                        // CharacterRuns text.
>> >> >>                        key = keySetIterator.next();
>> >> >>                        if(text.contains(key)) {
>> >> >>
>> >> >>                                // If the search term was found in
>> the
>> >> >> text,
>> >> >> get
>> >> >> the
>> >> >>                                // matching value from the HashMap,
>> >> find
>> >> >> out
>> >> >> whereabouts
>> >> >>                                // in the CharacterRuns text the
>> search
>> >> >> term
>> >> >> is
>> >> >>                                // and call the replaceText() method
>> to
>> >> >> substitute
>> >> >>                                // the replacement term for the
>> search
>> >> >> term.
>> >> >>                            value = replacements.get(key);
>> >> >>                            int start = text.indexOf(key);
>> >> >>                            charRun.replaceText(key, value, start);
>> >> >>
>> >> >>                            // Note that this code was added to test
>> >> >> whether
>> >> >>                            // it was possible to replace multiple
>> >> >> occurrences
>> >> >>                            // of the search term. I cannot remember
>> if
>> >> I
>> >> >> tested
>> >> >>                            // it but believe that it did work;
>> either
>> >> >> way,
>> >> >>                            // it could be tested now and if
>> succeeds,
>> >> >> then
>> >> >> the
>> >> >>                            // searchAndReplace() method above could
>> be
>> >> >> modified
>> >> >>                            // to include this.
>> >> >>                            docRange = document.getRange();
>> >> >>                            paragraph = docRange.getParagraph(i);
>> >> >>                            charRun = paragraph.getCharacterRun(j);
>> >> >>                            text = charRun.text();
>> >> >>                        }
>> >> >>                    }
>> >> >>                }
>> >> >>            }
>> >> >>
>> >> >>            // Close the InputStream
>> >> >>            bufIStream.close();
>> >> >>            bufIStream = null;
>> >> >>
>> >> >>            // Open an OutputStream and save the modified document
>> >> away.
>> >> >>            outputFile = new File(outputFilename);
>> >> >>            fileOStream = new FileOutputStream(outputFile);
>> >> >>            bufOStream = new BufferedOutputStream(fileOStream);
>> >> >>            document.write(bufOStream);
>> >> >>        }
>> >> >>        catch(Exception ex) {
>> >> >>            System.out.println("Caught an: " +
>> >> ex.getClass().getName());
>> >> >>            System.out.println("Message: " + ex.getMessage());
>> >> >>            System.out.println("Stacktrace follows.............");
>> >> >>            ex.printStackTrace(System.out);
>> >> >>        }
>> >> >>        finally {
>> >> >>            if(bufIStream != null) {
>> >> >>                try {
>> >> >>                    bufIStream.close();
>> >> >>                    bufIStream = null;
>> >> >>                }
>> >> >>                catch(Exception ex) {
>> >> >>                    // I G N O R E //
>> >> >>                }
>> >> >>            }
>> >> >>            if(bufOStream != null) {
>> >> >>                try {
>> >> >>                    bufOStream.flush();
>> >> >>                    bufOStream.close();
>> >> >>                    bufOStream = null;
>> >> >>                }
>> >> >>                catch(Exception ex) {
>> >> >>
>> >> >>                }
>> >> >>            }
>> >> >>        }
>> >> >>
>> >> >>    }
>> >> >>
>> >> >>        /**
>> >> >>         * The main entry point to the program demonstrating how the
>> >> code
>> >> >> may
>> >> >>         * be utilised.
>> >> >>         *
>> >> >>         * @param args An array of type String containing argumnets
>> >> passed
>> >> >> to the
>> >> >>         *             program on execution.
>> >> >>         */
>> >> >>        public static void main(String[] args) {
>> >> >>                SearchReplace replacer = new SearchReplace();
>> >> >>
>> >> >>                // To serach for and replace single items. Note, the
>> >> code
>> >> >> has not, at
>> >> >>                // least as far as I can remember, been tested by
>> >> passing
>> >> >> the same
>> >> >>                // file to both the searchTerm and replacementTerm
>> >> >> parameters. It ought
>> >> >>                // to work but has NOT been tested I believe.
>> >> >>                replacer.searchAndReplace("Document.doc",
>>  //
>> >> >> Source Document
>> >> >>                        "Replaced Document.doc",
>> >> >> //
>> >> >> Result Document
>> >> >>                        "search term",
>> >> >> //
>> >> >> Search term
>> >> >>                        "replacement term");
>> >> >> //
>> >> >> Replacement term
>> >> >>
>> >> >>                // To search for and replace a series of items
>> >> >>                HashMap<String, String> searchTerms = new
>> >> HashMap<String,
>> >> >> String>();
>> >> >>                searchTerms.put("search term 1", "replacement term
>> 1");
>> >> >>                searchTerms.put("search term 2", "replacement term
>> 2");
>> >> >>                searchTerms.put("search term 3", "replacement term
>> 3");
>> >> >>                searchTerms.put("search term 4", "replacement term
>> 4");
>> >> >>
>> >> >>                replacer.searchAndReplace("Document.doc",    //
>> Source
>> >> >> Document
>> >> >>                        "Replaced Document.doc",                 //
>> >> Result
>> >> >> Document
>> >> >>                        searchTerms)                             //
>> >> >> Search/replacement items
>> >> >>         }
>> >> >> }
>> >> >>
>> >> >>
>> >> >>
>> >> >> karthik-33 wrote:
>> >> >> >
>> >> >> > 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 <ma...@tiscali.co.uk>
>> >> 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: user-unsubscribe@poi.apache.org
>> >> >> >> For additional commands, e-mail: user-help@poi.apache.org
>> >> >> >>
>> >> >> >>
>> >> >> >
>> >> >> >
>> >> >> > --
>> >> >> > karthik
>> >> >> >
>> >> >> >
>> >> >>
>> >> >> --
>> >> >> View this message in context:
>> >> >>
>> >>
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
>> >> >>  Sent from the POI - User mailing list archive at Nabble.com.
>> >> >>
>> >> >>
>> >> >>
>> ---------------------------------------------------------------------
>> >> >> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> >> >> For additional commands, e-mail: user-help@poi.apache.org
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >> > --
>> >> > karthik
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24885699.html
>> >> Sent from the POI - User mailing list archive at Nabble.com.
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> >> For additional commands, e-mail: user-help@poi.apache.org
>> >>
>> >>
>> >
>> >
>> > --
>> > karthik
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24894775.html
>> Sent from the POI - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
> 
> 
> -- 
> karthik
> 
> 

-- 
View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24912385.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by karthik <ka...@gmail.com>.
Thanks Mark. You have been very helpful in assisting me.
I would defintely give a try to SWT/OLE and let you know how it works.
I have one question though regarding SWT/OLE, iam going to deploy this java
code in AS400 system.
We have java installed in AS400 system and POI works in AS400 like windows.
But not sure for this SWT/OLE. Do you have an idea on this or i can try it
and see.

I like to download the code and try it. I have to install SVN and meanwhile
i will try with
SWT/OLE. I will let u know how it goes.

Thanks
Karthik

On Mon, Aug 10, 2009 at 2:07 AM, MSB <ma...@tiscali.co.uk> wrote:

>
> HWPF is still very immature and I do not think that with the project in
> it's
> current state there is a way to do any better than we already have, sorry.
> With regard to the SWT/OLE technique, can I ask did you download from
> Rapidshare the example I posted? If so, then you need to unzip the archive
> into a folder and open the file called readme.rtf. It contains instructions
> telling you how to proceed. Before going any further, I do need to stress
> again that OLE will only work if you are running on a Windpws PC and a copy
> of Word is installed onto the machine where you will be running the code.
>
> Off of the top of my head, the first thing to do is unzip the SWT archive -
> is has a name something like swt-3.2.3-win32.zip and contains two files you
> are interested in. The first is the java achive which is called swt.jar.
> Extract that into a folder and place it on your classpath so the code I
> sent
> will compile and run. Secondly, you need to extract a dll and place it into
> the system32 subfolder of your windows folder on the computers hard drive.
> Again, I cannot remember the name of the dll exactly but think it will be
> something like this - swt-win32-3236.dll.
>
> Now, you are ready to compile and run the file of code that is also
> contained in the archive. Extract it to a folder somewhere on you computer,
> open it using whichever editor you like to use, make sure that the
> -classpath environment variable is set so that it can access the swt.jar
> archive and then you should be able to compile and run the code. Usually, I
> place some code into the main() method that explains what to do and how to
> run the class and if I remember correctly, that class is very simple to use
> and you may be able to deduce from the code that is there how to search for
> and replace strings of text on Word documents. At this stage, do not worry
> at all about how the class does what it does. There is a vile mixture of
> VBA
> and java in there which will be hard to decipher until you are a little
> more
> familiar with the OLE technique. The class should run and ought to
> successfully search for and replace items in a Word document. It will seem
> to run slowly. This is because in the background, it is loading Word,
> locating the file, perfroming the search and replace operation, saving the
> file away again and then closing Word down.
>
> As to patching HWPF, the first thing you need to do is figure out where the
> problem is and the best way to accomplish this is to try to replace a
> string
> of text with one that is far longer. HWPF should respond by throwing an
> exception and the stacktrace will help you to identify which class and
> which
> method is the source of that exception. Next, have a look at this page -
> http://poi.apache.org/getinvolved/index.html - it explains a little of how
> to get involved with developing the API. It asumes that you will commit
> your
> changes to the project - which you are not of course forced to do - but is
> a
> good place to start. Then, as suggested on that page, use SVN (subversion)
> to checkout a copy of the project's code and see if you can figure out
> where
> the changes need to be made and, if so, modify your checked out copy of the
> project's code. Once you get to that poiont, you can think about whether or
> not you wish to submit the changes as a patch and, if so, the easiest way
> to
> do this would be to post to the list again saying that you have a patch you
> would like to commit. Someone will respond and help you to commit the patch
> to the project.
>
> Sorry to say that I am not the best person to talk you through the process
> of developing and commiting patches as I am still just working through that
> myself. If you do want to work on HWPF after looking at the code then I
> would suggest contacting Nick Burch - nick@apache.org - the project chair
> and asking him how to proceed.
>
> Yours
>
> Mark B
>
>
> karthik-33 wrote:
> >
> > Mark, Thanks for trying out. I tried the same way you did...
> > Can you suggest me a way to resolve this..
> > What is the option one to try patch HWPF.. May be i can try if you have
> > any
> > new way to approach this problem...
> > And the option two, SWT/OLE iam new to it and i dont know how to work on
> > that..
> >
> > Thanks
> > Karthik
> > On Sun, Aug 9, 2009 at 6:07 AM, MSB <ma...@tiscali.co.uk> wrote:
> >
> >>
> >> This morning, I had the chance to try what I spoke of in my previous
> >> email
> >> -
> >> using Java to replace text in a copy of the Paragraph's text and then
> >> replacing all of the text in the Paragraph with the modified copy. It
> >> worked
> >> but only up to a point; if the replacement text was exactly the same
> >> length
> >> as the search term, this technique worked but any differences in length
> >> rendered the resulting file corrupt; and I am guessing that this is the
> >> same
> >> problem as you originally encountered.
> >>
> >> So, I think I can conclude that if you are trying to replace the search
> >> term
> >> with a String of text that is longer than it, you will run into
> problems.
> >> As
> >> long as the replacement is shorter than the search term - or at most the
> >> same length - then the previous piece of code I posted seems to work
> well
> >> enough.
> >>
> >> To my mind then you have two options. Option 1 would be to patch HWPF so
> >> that it will work as you wish - the API is very immature and has not
> been
> >> the focus of the same sort of development effort as has HSSF for
> example.
> >> Option 2 is to use an alternative such as SWT/OLE or OpenOffice. The
> >> limitation with the OpenOffice approach is that whilst it can read
> >> OpenXML
> >> documents - Office 2007 and beyond with the .docx or similar extension -
> >> it
> >> cannot save a document in this format.
> >>
> >> Sorry for the bad news.
> >>
> >> Yours
> >>
> >> Mark B
> >>
> >>
> >> karthik-33 wrote:
> >> >
> >> > Hi Mark, Thanks for sending this program.
> >> > I tried this program with POI 3.2 final version, which iam currently
> >> > using.
> >> > CharaterRun doesnt behave consistently the same way, sometimes it
> >> splits
> >> > the
> >> > paragraph text into more number of Character run, sometimes it doesnt
> >> > split
> >> > and i see the whole paragraph text in one character run. So the search
> >> > text
> >> > is not getting replaced.
> >> > Is there anyway to solve this issue?
> >> > On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk> wrote:
> >> >
> >> >>
> >> >> Here is the HWPF based code that I put together to play around with.
> >> It
> >> >> was
> >> >> written a very long time ago so I am not sure what testing I
> undertook
> >> >> and
> >> >> exactly what the results were but I have run it this morning just to
> >> >> ensure
> >> >> that
> >> >> it does not crash the PC and all seems to be well. This section has
> >> been
> >> >> cut
> >> >> from a much larger class that is full of other test code that I play
> >> >> around
> >> >> with peridocically. Everything you need is there I believe but on the
> >> >> off-chance
> >> >> that it calls another method whose source I have neglected to
> include,
> >> >> just
> >> >> drop
> >> >> an email to the list please.
> >> >>
> >> >> Currently, I am running POI version 3.5 beta 7 on a PC operating
> under
> >> >> Windows XP SP2. Office 2007 is installed now and it seems able to
> open
> >> >> the
> >> >> files this code produces quite happily. In the back of my mind, I
> seem
> >> to
> >> >> remember that the files produced by some search and replace code I
> put
> >> >> together could be opened but not modified; I tested that problem this
> >> >> morning
> >> >> and the files this code produces seem fine, I can open them, make
> >> changes
> >> >> and
> >> >> then save the results again. But do please be prepared for problems
> >> like
> >> >> that.
> >> >>
> >> >> Again, can I emphasise this is test code; it is scruffy and there are
> >> >> going
> >> >> to
> >> >> be variables I put in there so that I could monitor the progress of
> >> the
> >> >> code
> >> >> by dumping messages to the screen. As you go through, if something
> >> seems
> >> >> to
> >> >> be superfluous, then this is likely the reason and you can comment it
> >> out
> >> >> or
> >> >> delete it.
> >> >>
> >> >> Good luck and I do hope it all works. If you have any problems, just
> >> drop
> >> >> a
> >> >> message onto the list.
> >> >>
> >> >> Yours
> >> >>
> >> >> Mark B
> >> >>
> >> >>
> >> >> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
> >> >> import org.apache.poi.hwpf.HWPFDocument;
> >> >> import org.apache.poi.hwpf.usermodel.Range;
> >> >> import org.apache.poi.hwpf.usermodel.Paragraph;
> >> >> import org.apache.poi.hwpf.usermodel.CharacterRun;
> >> >>
> >> >> import java.io.File;
> >> >> import java.io.FileOutputStream;
> >> >> import java.io.FileInputStream;
> >> >> import java.io.BufferedOutputStream;
> >> >> import java.io.BufferedInputStream;
> >> >> import java.util.HashMap;
> >> >> import java.util.Iterator;
> >> >>
> >> >> /**
> >> >>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
> >> >>  * CONDITIONS OF ANY KIND, either express or implied. It is not
> >> intended
> >> >> to
> >> >>  * be used in a 'production' environment without undergoing rigorous
> >> >> testing.
> >> >>  *
> >> >>  * With that out of the way, an instance of this class can be used to
> >> >> search
> >> >> for
> >> >>  * and replace Strings of text within a Word document. To see how the
> >> >> code
> >> >> may
> >> >>  * be used, look into the main() method for examples.
> >> >>  *
> >> >>  * Note the replacements made by the code contained within this class
> >> >> ignore
> >> >>  * any formatting that may have been applied to the text that is
> >> >> replaced.
> >> >> That
> >> >>  * is to say that if the text was originally formatted to use the
> >> Arial
> >> >> font,
> >> >>  * was sized to 24 points, emboldened, underlined and red in colour,
> >> then
> >> >> all
> >> >>  * of this will be lost if it is replaced. Further if any text is
> >> >> replaced
> >> >> in a
> >> >>  * Paragraph, all the formatting applied to that Paragraph's contents
> >> is
> >> >> likely
> >> >>  * to be lost.
> >> >>  *
> >> >>  * @author Mark Beardsley [msb at apache.org]
> >> >>  * @version 1.00 8th August 2009 (cannot remember when originally put
> >> >> together)
> >> >>  */
> >> >> public class SearchReplace {
> >> >>
> >> >>
> >> >>        /**
> >> >>         * Search for and replace a single occurrence of a string of
> >> text
> >> >> within a
> >> >>         * Word document.
> >> >>         *
> >> >>         * Note that no checks are made on the parameter's values;
> that
> >> is
> >> >> to say
> >> >>         * that the file named in the InputFilename parameter will not
> >> be
> >> >> checked
> >> >>         * to ensure the file exists and neither of the searchTerm nor
> >> >>         * replacementTerm parameters will be checked to ensure they
> >> are
> >> >> not
> >> >> null.
> >> >>         * Also, note that I have never tested passing the same String
> >> to
> >> >> the
> >> >>         * inputFilename and outputFilename parameters but cannot see
> >> why
> >> >> that
> >> >>         * should not be possible.
> >> >>         *
> >> >>         * @param inputFilename An instance of the String class that
> >> >> encapsulates
> >> >>         *                      the name of and path to a Word
> document
> >> >> which is
> >> >>         *                      in the binary (OLE2CDF) format. The
> >> >> contents
> >> >> of
> >> >> this
> >> >>         *                      document will be searched for
> >> occurrences
> >> >> of
> >> >> the
> >> >>         *                      search term.
> >> >>         * @param outputFilename An instance of the String class that
> >> >> encapsulates
> >> >>         *                       the name of and path to a Word
> >> document
> >> >> which is
> >> >>         *                       in the binary (OLE2CDF) format. This
> >> >> document will
> >> >>         *                       contain the results of the search and
> >> >> replace
> >> >>         *                       operation.
> >> >>         * @param searchTerm An instance of the String class that
> >> >> encapsulates a
> >> >>         *                   series of characters, a word or words.
> The
> >> >> document
> >> >>         *                   will be searched for occurrences of this
> >> >> String.
> >> >>         * @param replacementTerm An instance of the String class that
> >> >> contains a
> >> >>         *                        series of characters, a word or
> >> words.
> >> >> The
> >> >> String
> >> >>         *                        encapsulated by the searchTerm
> >> parameter
> >> >> will be
> >> >>         *                        replaced by the 'contents' of this
> >> >> parameter.
> >> >>         *
> >> >>         */
> >> >>        public void searchAndReplace(String inputFilename,
> >> >>                                 String outputFilename,
> >> >>                                 String searchTerm,
> >> >>                                 String replacementText) {
> >> >>
> >> >>        File inputFile = null;
> >> >>        File outputFile = null;
> >> >>        FileInputStream fileIStream = null;
> >> >>        FileOutputStream fileOStream = null;
> >> >>        BufferedInputStream bufIStream = null;
> >> >>        BufferedOutputStream bufOStream = null;
> >> >>        POIFSFileSystem fileSystem = null;
> >> >>        HWPFDocument document = null;
> >> >>        Range docRange = null;
> >> >>        Paragraph paragraph = null;
> >> >>        CharacterRun charRun = null;
> >> >>        int numParagraphs = 0;
> >> >>        int numCharRuns = 0;
> >> >>        String text = null;
> >> >>
> >> >>        try {
> >> >>            // Create an instance of the POIFSFileSystem class and
> >> >>            // attach it to the Word document using an InputStream.
> >> >>            inputFile = new File(inputFilename);
> >> >>            fileIStream = new FileInputStream(inputFile);
> >> >>            bufIStream = new BufferedInputStream(fileIStream);
> >> >>            fileSystem = new POIFSFileSystem(bufIStream);
> >> >>            document = new HWPFDocument(fileSystem);
> >> >>
> >> >>            // Get the overall Range object for the document. Note the
> >> >>            // use of the getRange() method and not the
> >> getOverallRange()
> >> >>            // method, this is just historic - when the code was
> >> >> originally
> >> >>            // written, I do not believe the latter method was part of
> >> the
> >> >> API.
> >> >>            docRange = document.getRange();
> >> >>
> >> >>            // Get the number of Paragraph(s) in the overall range and
> >> >> iterate
> >> >>            // through them
> >> >>            numParagraphs = docRange.numParagraphs();
> >> >>            for(int i = 0; i < numParagraphs; i++) {
> >> >>
> >> >>                // Get a Paragraph and recover the text from it. This
> >> step
> >> >> is
> >> >> far from
> >> >>                // necessary and I think I only got the text so that I
> >> >> could
> >> >> print
> >> >>                // it to screen as a diagnostic check to ensure that
> >> the
> >> >> Paragraph
> >> >>                // contained the text I was searching for. Experiment
> >> with
> >> >> this.
> >> >>                paragraph = docRange.getParagraph(i);
> >> >>                text = paragraph.text();
> >> >>
> >> >>                // Get the number of CharacterRuns in the Paragraph
> >> >>                numCharRuns = paragraph.numCharacterRuns();
> >> >>                for(int j = 0; j < numCharRuns; j++) {
> >> >>
> >> >>                        // Get a character run and recover it's text -
> >> >> note
> >> >> that
> >> >>                        // the same text variable is used as for the
> >> >> Paragraph
> >> >> above.
> >> >>                        // So, it MUST be safe to remove the text =
> >> >> paragraph.text()
> >> >>                        // line above.
> >> >>                    charRun = paragraph.getCharacterRun(j);
> >> >>                    text = charRun.text();
> >> >>
> >> >>                    // Check to see if the text of the CharacterRun
> >> >> contains
> >> >> the
> >> >>                    // search term. If it does, find out where that
> >> term
> >> >> starts
> >> >>                    // and call the replaceText() method passing the
> >> >> index.
> >> >>                    // Maybe this is the key difference between what
> we
> >> >> are
> >> >>                    // doing.
> >> >>                    if(text.contains(searchTerm)) {
> >> >>                        int start = text.indexOf(searchTerm);
> >> >>                        charRun.replaceText(searchTerm,
> >> replacementText,
> >> >> start);
> >> >>                    }
> >> >>                }
> >> >>            }
> >> >>
> >> >>            // Close the InputStream
> >> >>            bufIStream.close();
> >> >>            bufIStream = null;
> >> >>
> >> >>            // Open an OutputStream and write the document away.
> >> >>            outputFile = new File(outputFilename);
> >> >>            fileOStream = new FileOutputStream(outputFile);
> >> >>            bufOStream = new BufferedOutputStream(fileOStream);
> >> >>
> >> >>            document.write(bufOStream);
> >> >>
> >> >>        }
> >> >>        catch(Exception ex) {
> >> >>            System.out.println("Caught an: " +
> >> ex.getClass().getName());
> >> >>            System.out.println("Message: " + ex.getMessage());
> >> >>            System.out.println("Stacktrace follows.............");
> >> >>            ex.printStackTrace(System.out);
> >> >>        }
> >> >>        finally {
> >> >>            if(bufOStream != null) {
> >> >>                try {
> >> >>                    //bufOStream.flush();
> >> >>                    bufOStream.close();
> >> >>                    bufOStream = null;
> >> >>                }
> >> >>                catch(Exception ex) {
> >> >>
> >> >>                }
> >> >>            }
> >> >>            if(bufIStream != null) {
> >> >>                try {
> >> >>                    bufIStream.close();
> >> >>                    bufIStream = null;
> >> >>                }
> >> >>                catch(Exception ex) {
> >> >>                    // I G N O R E //
> >> >>                }
> >> >>            }
> >> >>        }
> >> >>
> >> >>    }
> >> >>
> >> >>    /**
> >> >>         * Search for and replace a single occurrence of a string of
> >> text
> >> >> within a
> >> >>         * Word document.
> >> >>         *
> >> >>         * Note that no checks are made on the parameter's values;
> that
> >> is
> >> >> to say
> >> >>         * that the file named in the InputFilename parameter will not
> >> be
> >> >> checked
> >> >>         * to ensure the file exists and neither of the searchTerm nor
> >> >>         * replacementTerm pare,eters will be checked to ensure they
> >> are
> >> >> not
> >> >> null.
> >> >>         * Also, note that I have never tested passing the same String
> >> to
> >> >> the
> >> >>         * inputFilename and outputFilename parameters but cannot see
> >> why
> >> >> that
> >> >>         * should not be possible.
> >> >>         *
> >> >>         * @param inputFilename An instance of the String class that
> >> >> encapsulates
> >> >>         *                      the name of and path to a Word
> document
> >> >> which is
> >> >>         *                      in the binary (OLE2CDF) format. The
> >> >> contents
> >> >> of
> >> >> this
> >> >>         *                      document will be searched for
> >> occurrences
> >> >> of
> >> >> the
> >> >>         *                      search term.
> >> >>         * @param outputFilename An instance of the String class that
> >> >> encapsulates
> >> >>         *                       the name of and path to a Word
> >> document
> >> >> which is
> >> >>         *                       in the binary (OLE2CDF) format. This
> >> >> document will
> >> >>         *                       contain the results of the search and
> >> >> replace
> >> >>         *                       operation.
> >> >>         * @param replacements An instance of the java.util.HashMap
> >> class
> >> >> that
> >> >>         *                     contains a series of key, value pairs.
> >> Each
> >> >> key
> >> >>         *                     is an instance of the String class that
> >> >> encapsulates
> >> >>         *                     a series of characters, a word or words
> >> >> that
> >> >> the
> >> >>         *                     code will search for and the
> >> accompanying
> >> >> value is
> >> >>         *                     also an instance of the String class
> >> that
> >> >> likewise
> >> >>         *                     encapsulates a series of characters, a
> >> word
> >> >> or
> >> >> words.
> >> >>         *                     The 'contents' of the value's String
> >> will
> >> >> be
> >> >> used to
> >> >>         *                     replace the contents of the key's
> String
> >> if
> >> >> an
> >> >>         *                     occurrence of the latter is found.
> >> >>         */
> >> >>    public void searchAndReplace(String inputFilename,
> >> >>                                 String outputFilename,
> >> >>                                 HashMap<String, String> replacements)
> >> {
> >> >>
> >> >>        File inputFile = null;
> >> >>        File outputFile = null;
> >> >>        FileInputStream fileIStream = null;
> >> >>        FileOutputStream fileOStream = null;
> >> >>        BufferedInputStream bufIStream = null;
> >> >>        BufferedOutputStream bufOStream = null;
> >> >>        POIFSFileSystem fileSystem = null;
> >> >>        HWPFDocument document = null;
> >> >>        Range docRange = null;
> >> >>        Paragraph paragraph = null;
> >> >>        CharacterRun charRun = null;
> >> >>        Set<String> keySet = null;
> >> >>        Iterator<String> keySetIterator = null;
> >> >>        int numParagraphs = 0;
> >> >>        int numCharRuns = 0;
> >> >>        String text = null;
> >> >>        String key = null;
> >> >>        String value = null;
> >> >>
> >> >>        try {
> >> >>            // Create an instance of the POIFSFileSystem class and
> >> >>            // attach it to the Word document using an InputStream.
> >> >>            inputFile = new File(inputFilename);
> >> >>            fileIStream = new FileInputStream(inputFile);
> >> >>            bufIStream = new BufferedInputStream(fileIStream);
> >> >>            fileSystem = new POIFSFileSystem(bufIStream);
> >> >>            document = new HWPFDocument(fileSystem);
> >> >>
> >> >>                        // Get a reference to the overall Range for
> the
> >> >> document
> >> >>                        // and discover how many Paragraphs objects
> >> there
> >> >> are
> >> >>                        // in the document.
> >> >>            docRange = document.getRange();
> >> >>            numParagraphs = docRange.numParagraphs();
> >> >>
> >> >>            // Recover a Set of the keys in the HashMap
> >> >>            keySet = replacements.keySet();
> >> >>
> >> >>            // Step through each Paragraph
> >> >>            for(int i = 0; i < numParagraphs; i++) {
> >> >>                paragraph = docRange.getParagraph(i);
> >> >>                // This line can almost certainly be removed - see
> >> >>                // the comments in the method above.
> >> >>                text = paragraph.text();
> >> >>
> >> >>                // Get the number of CharacterRuns in the Paragraph
> >> >>                // and step through each one.
> >> >>                numCharRuns = paragraph.numCharacterRuns();
> >> >>                for(int j = 0; j < numCharRuns; j++) {
> >> >>                    charRun = paragraph.getCharacterRun(j);
> >> >>
> >> >>                    // Get the text from the CharacterRun and recover
> >> an
> >> >>                    // Iterator to step through the Set of keys.
> >> >>                    text = charRun.text();
> >> >>                    keySetIterator = keySet.iterator();
> >> >>                    while(keySetIterator.hasNext()) {
> >> >>
> >> >>                        // Get the key - which is also the search term
> >> -
> >> >> and
> >> >>                        // check to see if it can be found within the
> >> >>                        // CharacterRuns text.
> >> >>                        key = keySetIterator.next();
> >> >>                        if(text.contains(key)) {
> >> >>
> >> >>                                // If the search term was found in the
> >> >> text,
> >> >> get
> >> >> the
> >> >>                                // matching value from the HashMap,
> >> find
> >> >> out
> >> >> whereabouts
> >> >>                                // in the CharacterRuns text the
> search
> >> >> term
> >> >> is
> >> >>                                // and call the replaceText() method
> to
> >> >> substitute
> >> >>                                // the replacement term for the search
> >> >> term.
> >> >>                            value = replacements.get(key);
> >> >>                            int start = text.indexOf(key);
> >> >>                            charRun.replaceText(key, value, start);
> >> >>
> >> >>                            // Note that this code was added to test
> >> >> whether
> >> >>                            // it was possible to replace multiple
> >> >> occurrences
> >> >>                            // of the search term. I cannot remember
> if
> >> I
> >> >> tested
> >> >>                            // it but believe that it did work; either
> >> >> way,
> >> >>                            // it could be tested now and if succeeds,
> >> >> then
> >> >> the
> >> >>                            // searchAndReplace() method above could
> be
> >> >> modified
> >> >>                            // to include this.
> >> >>                            docRange = document.getRange();
> >> >>                            paragraph = docRange.getParagraph(i);
> >> >>                            charRun = paragraph.getCharacterRun(j);
> >> >>                            text = charRun.text();
> >> >>                        }
> >> >>                    }
> >> >>                }
> >> >>            }
> >> >>
> >> >>            // Close the InputStream
> >> >>            bufIStream.close();
> >> >>            bufIStream = null;
> >> >>
> >> >>            // Open an OutputStream and save the modified document
> >> away.
> >> >>            outputFile = new File(outputFilename);
> >> >>            fileOStream = new FileOutputStream(outputFile);
> >> >>            bufOStream = new BufferedOutputStream(fileOStream);
> >> >>            document.write(bufOStream);
> >> >>        }
> >> >>        catch(Exception ex) {
> >> >>            System.out.println("Caught an: " +
> >> ex.getClass().getName());
> >> >>            System.out.println("Message: " + ex.getMessage());
> >> >>            System.out.println("Stacktrace follows.............");
> >> >>            ex.printStackTrace(System.out);
> >> >>        }
> >> >>        finally {
> >> >>            if(bufIStream != null) {
> >> >>                try {
> >> >>                    bufIStream.close();
> >> >>                    bufIStream = null;
> >> >>                }
> >> >>                catch(Exception ex) {
> >> >>                    // I G N O R E //
> >> >>                }
> >> >>            }
> >> >>            if(bufOStream != null) {
> >> >>                try {
> >> >>                    bufOStream.flush();
> >> >>                    bufOStream.close();
> >> >>                    bufOStream = null;
> >> >>                }
> >> >>                catch(Exception ex) {
> >> >>
> >> >>                }
> >> >>            }
> >> >>        }
> >> >>
> >> >>    }
> >> >>
> >> >>        /**
> >> >>         * The main entry point to the program demonstrating how the
> >> code
> >> >> may
> >> >>         * be utilised.
> >> >>         *
> >> >>         * @param args An array of type String containing argumnets
> >> passed
> >> >> to the
> >> >>         *             program on execution.
> >> >>         */
> >> >>        public static void main(String[] args) {
> >> >>                SearchReplace replacer = new SearchReplace();
> >> >>
> >> >>                // To serach for and replace single items. Note, the
> >> code
> >> >> has not, at
> >> >>                // least as far as I can remember, been tested by
> >> passing
> >> >> the same
> >> >>                // file to both the searchTerm and replacementTerm
> >> >> parameters. It ought
> >> >>                // to work but has NOT been tested I believe.
> >> >>                replacer.searchAndReplace("Document.doc",
>  //
> >> >> Source Document
> >> >>                        "Replaced Document.doc",
> >> >> //
> >> >> Result Document
> >> >>                        "search term",
> >> >> //
> >> >> Search term
> >> >>                        "replacement term");
> >> >> //
> >> >> Replacement term
> >> >>
> >> >>                // To search for and replace a series of items
> >> >>                HashMap<String, String> searchTerms = new
> >> HashMap<String,
> >> >> String>();
> >> >>                searchTerms.put("search term 1", "replacement term
> 1");
> >> >>                searchTerms.put("search term 2", "replacement term
> 2");
> >> >>                searchTerms.put("search term 3", "replacement term
> 3");
> >> >>                searchTerms.put("search term 4", "replacement term
> 4");
> >> >>
> >> >>                replacer.searchAndReplace("Document.doc",    // Source
> >> >> Document
> >> >>                        "Replaced Document.doc",                 //
> >> Result
> >> >> Document
> >> >>                        searchTerms)                             //
> >> >> Search/replacement items
> >> >>         }
> >> >> }
> >> >>
> >> >>
> >> >>
> >> >> karthik-33 wrote:
> >> >> >
> >> >> > 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 <ma...@tiscali.co.uk>
> >> 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: user-unsubscribe@poi.apache.org
> >> >> >> For additional commands, e-mail: user-help@poi.apache.org
> >> >> >>
> >> >> >>
> >> >> >
> >> >> >
> >> >> > --
> >> >> > karthik
> >> >> >
> >> >> >
> >> >>
> >> >> --
> >> >> View this message in context:
> >> >>
> >>
> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
> >> >>  Sent from the POI - User mailing list archive at Nabble.com.
> >> >>
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> >> >> For additional commands, e-mail: user-help@poi.apache.org
> >> >>
> >> >>
> >> >
> >> >
> >> > --
> >> > karthik
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24885699.html
> >> Sent from the POI - User mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> >> For additional commands, e-mail: user-help@poi.apache.org
> >>
> >>
> >
> >
> > --
> > karthik
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24894775.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>


-- 
karthik

Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
HWPF is still very immature and I do not think that with the project in it's
current state there is a way to do any better than we already have, sorry.
With regard to the SWT/OLE technique, can I ask did you download from
Rapidshare the example I posted? If so, then you need to unzip the archive
into a folder and open the file called readme.rtf. It contains instructions
telling you how to proceed. Before going any further, I do need to stress
again that OLE will only work if you are running on a Windpws PC and a copy
of Word is installed onto the machine where you will be running the code.

Off of the top of my head, the first thing to do is unzip the SWT archive -
is has a name something like swt-3.2.3-win32.zip and contains two files you
are interested in. The first is the java achive which is called swt.jar.
Extract that into a folder and place it on your classpath so the code I sent
will compile and run. Secondly, you need to extract a dll and place it into
the system32 subfolder of your windows folder on the computers hard drive.
Again, I cannot remember the name of the dll exactly but think it will be
something like this - swt-win32-3236.dll.

Now, you are ready to compile and run the file of code that is also
contained in the archive. Extract it to a folder somewhere on you computer,
open it using whichever editor you like to use, make sure that the
-classpath environment variable is set so that it can access the swt.jar
archive and then you should be able to compile and run the code. Usually, I
place some code into the main() method that explains what to do and how to
run the class and if I remember correctly, that class is very simple to use
and you may be able to deduce from the code that is there how to search for
and replace strings of text on Word documents. At this stage, do not worry
at all about how the class does what it does. There is a vile mixture of VBA
and java in there which will be hard to decipher until you are a little more
familiar with the OLE technique. The class should run and ought to
successfully search for and replace items in a Word document. It will seem
to run slowly. This is because in the background, it is loading Word,
locating the file, perfroming the search and replace operation, saving the
file away again and then closing Word down.

As to patching HWPF, the first thing you need to do is figure out where the
problem is and the best way to accomplish this is to try to replace a string
of text with one that is far longer. HWPF should respond by throwing an
exception and the stacktrace will help you to identify which class and which
method is the source of that exception. Next, have a look at this page -
http://poi.apache.org/getinvolved/index.html - it explains a little of how
to get involved with developing the API. It asumes that you will commit your
changes to the project - which you are not of course forced to do - but is a
good place to start. Then, as suggested on that page, use SVN (subversion)
to checkout a copy of the project's code and see if you can figure out where
the changes need to be made and, if so, modify your checked out copy of the
project's code. Once you get to that poiont, you can think about whether or
not you wish to submit the changes as a patch and, if so, the easiest way to
do this would be to post to the list again saying that you have a patch you
would like to commit. Someone will respond and help you to commit the patch
to the project.

Sorry to say that I am not the best person to talk you through the process
of developing and commiting patches as I am still just working through that
myself. If you do want to work on HWPF after looking at the code then I
would suggest contacting Nick Burch - nick@apache.org - the project chair
and asking him how to proceed.

Yours

Mark B


karthik-33 wrote:
> 
> Mark, Thanks for trying out. I tried the same way you did...
> Can you suggest me a way to resolve this..
> What is the option one to try patch HWPF.. May be i can try if you have
> any
> new way to approach this problem...
> And the option two, SWT/OLE iam new to it and i dont know how to work on
> that..
> 
> Thanks
> Karthik
> On Sun, Aug 9, 2009 at 6:07 AM, MSB <ma...@tiscali.co.uk> wrote:
> 
>>
>> This morning, I had the chance to try what I spoke of in my previous
>> email
>> -
>> using Java to replace text in a copy of the Paragraph's text and then
>> replacing all of the text in the Paragraph with the modified copy. It
>> worked
>> but only up to a point; if the replacement text was exactly the same
>> length
>> as the search term, this technique worked but any differences in length
>> rendered the resulting file corrupt; and I am guessing that this is the
>> same
>> problem as you originally encountered.
>>
>> So, I think I can conclude that if you are trying to replace the search
>> term
>> with a String of text that is longer than it, you will run into problems.
>> As
>> long as the replacement is shorter than the search term - or at most the
>> same length - then the previous piece of code I posted seems to work well
>> enough.
>>
>> To my mind then you have two options. Option 1 would be to patch HWPF so
>> that it will work as you wish - the API is very immature and has not been
>> the focus of the same sort of development effort as has HSSF for example.
>> Option 2 is to use an alternative such as SWT/OLE or OpenOffice. The
>> limitation with the OpenOffice approach is that whilst it can read
>> OpenXML
>> documents - Office 2007 and beyond with the .docx or similar extension -
>> it
>> cannot save a document in this format.
>>
>> Sorry for the bad news.
>>
>> Yours
>>
>> Mark B
>>
>>
>> karthik-33 wrote:
>> >
>> > Hi Mark, Thanks for sending this program.
>> > I tried this program with POI 3.2 final version, which iam currently
>> > using.
>> > CharaterRun doesnt behave consistently the same way, sometimes it
>> splits
>> > the
>> > paragraph text into more number of Character run, sometimes it doesnt
>> > split
>> > and i see the whole paragraph text in one character run. So the search
>> > text
>> > is not getting replaced.
>> > Is there anyway to solve this issue?
>> > On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk> wrote:
>> >
>> >>
>> >> Here is the HWPF based code that I put together to play around with.
>> It
>> >> was
>> >> written a very long time ago so I am not sure what testing I undertook
>> >> and
>> >> exactly what the results were but I have run it this morning just to
>> >> ensure
>> >> that
>> >> it does not crash the PC and all seems to be well. This section has
>> been
>> >> cut
>> >> from a much larger class that is full of other test code that I play
>> >> around
>> >> with peridocically. Everything you need is there I believe but on the
>> >> off-chance
>> >> that it calls another method whose source I have neglected to include,
>> >> just
>> >> drop
>> >> an email to the list please.
>> >>
>> >> Currently, I am running POI version 3.5 beta 7 on a PC operating under
>> >> Windows XP SP2. Office 2007 is installed now and it seems able to open
>> >> the
>> >> files this code produces quite happily. In the back of my mind, I seem
>> to
>> >> remember that the files produced by some search and replace code I put
>> >> together could be opened but not modified; I tested that problem this
>> >> morning
>> >> and the files this code produces seem fine, I can open them, make
>> changes
>> >> and
>> >> then save the results again. But do please be prepared for problems
>> like
>> >> that.
>> >>
>> >> Again, can I emphasise this is test code; it is scruffy and there are
>> >> going
>> >> to
>> >> be variables I put in there so that I could monitor the progress of
>> the
>> >> code
>> >> by dumping messages to the screen. As you go through, if something
>> seems
>> >> to
>> >> be superfluous, then this is likely the reason and you can comment it
>> out
>> >> or
>> >> delete it.
>> >>
>> >> Good luck and I do hope it all works. If you have any problems, just
>> drop
>> >> a
>> >> message onto the list.
>> >>
>> >> Yours
>> >>
>> >> Mark B
>> >>
>> >>
>> >> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
>> >> import org.apache.poi.hwpf.HWPFDocument;
>> >> import org.apache.poi.hwpf.usermodel.Range;
>> >> import org.apache.poi.hwpf.usermodel.Paragraph;
>> >> import org.apache.poi.hwpf.usermodel.CharacterRun;
>> >>
>> >> import java.io.File;
>> >> import java.io.FileOutputStream;
>> >> import java.io.FileInputStream;
>> >> import java.io.BufferedOutputStream;
>> >> import java.io.BufferedInputStream;
>> >> import java.util.HashMap;
>> >> import java.util.Iterator;
>> >>
>> >> /**
>> >>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
>> >>  * CONDITIONS OF ANY KIND, either express or implied. It is not
>> intended
>> >> to
>> >>  * be used in a 'production' environment without undergoing rigorous
>> >> testing.
>> >>  *
>> >>  * With that out of the way, an instance of this class can be used to
>> >> search
>> >> for
>> >>  * and replace Strings of text within a Word document. To see how the
>> >> code
>> >> may
>> >>  * be used, look into the main() method for examples.
>> >>  *
>> >>  * Note the replacements made by the code contained within this class
>> >> ignore
>> >>  * any formatting that may have been applied to the text that is
>> >> replaced.
>> >> That
>> >>  * is to say that if the text was originally formatted to use the
>> Arial
>> >> font,
>> >>  * was sized to 24 points, emboldened, underlined and red in colour,
>> then
>> >> all
>> >>  * of this will be lost if it is replaced. Further if any text is
>> >> replaced
>> >> in a
>> >>  * Paragraph, all the formatting applied to that Paragraph's contents
>> is
>> >> likely
>> >>  * to be lost.
>> >>  *
>> >>  * @author Mark Beardsley [msb at apache.org]
>> >>  * @version 1.00 8th August 2009 (cannot remember when originally put
>> >> together)
>> >>  */
>> >> public class SearchReplace {
>> >>
>> >>
>> >>        /**
>> >>         * Search for and replace a single occurrence of a string of
>> text
>> >> within a
>> >>         * Word document.
>> >>         *
>> >>         * Note that no checks are made on the parameter's values; that
>> is
>> >> to say
>> >>         * that the file named in the InputFilename parameter will not
>> be
>> >> checked
>> >>         * to ensure the file exists and neither of the searchTerm nor
>> >>         * replacementTerm parameters will be checked to ensure they
>> are
>> >> not
>> >> null.
>> >>         * Also, note that I have never tested passing the same String
>> to
>> >> the
>> >>         * inputFilename and outputFilename parameters but cannot see
>> why
>> >> that
>> >>         * should not be possible.
>> >>         *
>> >>         * @param inputFilename An instance of the String class that
>> >> encapsulates
>> >>         *                      the name of and path to a Word document
>> >> which is
>> >>         *                      in the binary (OLE2CDF) format. The
>> >> contents
>> >> of
>> >> this
>> >>         *                      document will be searched for
>> occurrences
>> >> of
>> >> the
>> >>         *                      search term.
>> >>         * @param outputFilename An instance of the String class that
>> >> encapsulates
>> >>         *                       the name of and path to a Word
>> document
>> >> which is
>> >>         *                       in the binary (OLE2CDF) format. This
>> >> document will
>> >>         *                       contain the results of the search and
>> >> replace
>> >>         *                       operation.
>> >>         * @param searchTerm An instance of the String class that
>> >> encapsulates a
>> >>         *                   series of characters, a word or words. The
>> >> document
>> >>         *                   will be searched for occurrences of this
>> >> String.
>> >>         * @param replacementTerm An instance of the String class that
>> >> contains a
>> >>         *                        series of characters, a word or
>> words.
>> >> The
>> >> String
>> >>         *                        encapsulated by the searchTerm
>> parameter
>> >> will be
>> >>         *                        replaced by the 'contents' of this
>> >> parameter.
>> >>         *
>> >>         */
>> >>        public void searchAndReplace(String inputFilename,
>> >>                                 String outputFilename,
>> >>                                 String searchTerm,
>> >>                                 String replacementText) {
>> >>
>> >>        File inputFile = null;
>> >>        File outputFile = null;
>> >>        FileInputStream fileIStream = null;
>> >>        FileOutputStream fileOStream = null;
>> >>        BufferedInputStream bufIStream = null;
>> >>        BufferedOutputStream bufOStream = null;
>> >>        POIFSFileSystem fileSystem = null;
>> >>        HWPFDocument document = null;
>> >>        Range docRange = null;
>> >>        Paragraph paragraph = null;
>> >>        CharacterRun charRun = null;
>> >>        int numParagraphs = 0;
>> >>        int numCharRuns = 0;
>> >>        String text = null;
>> >>
>> >>        try {
>> >>            // Create an instance of the POIFSFileSystem class and
>> >>            // attach it to the Word document using an InputStream.
>> >>            inputFile = new File(inputFilename);
>> >>            fileIStream = new FileInputStream(inputFile);
>> >>            bufIStream = new BufferedInputStream(fileIStream);
>> >>            fileSystem = new POIFSFileSystem(bufIStream);
>> >>            document = new HWPFDocument(fileSystem);
>> >>
>> >>            // Get the overall Range object for the document. Note the
>> >>            // use of the getRange() method and not the
>> getOverallRange()
>> >>            // method, this is just historic - when the code was
>> >> originally
>> >>            // written, I do not believe the latter method was part of
>> the
>> >> API.
>> >>            docRange = document.getRange();
>> >>
>> >>            // Get the number of Paragraph(s) in the overall range and
>> >> iterate
>> >>            // through them
>> >>            numParagraphs = docRange.numParagraphs();
>> >>            for(int i = 0; i < numParagraphs; i++) {
>> >>
>> >>                // Get a Paragraph and recover the text from it. This
>> step
>> >> is
>> >> far from
>> >>                // necessary and I think I only got the text so that I
>> >> could
>> >> print
>> >>                // it to screen as a diagnostic check to ensure that
>> the
>> >> Paragraph
>> >>                // contained the text I was searching for. Experiment
>> with
>> >> this.
>> >>                paragraph = docRange.getParagraph(i);
>> >>                text = paragraph.text();
>> >>
>> >>                // Get the number of CharacterRuns in the Paragraph
>> >>                numCharRuns = paragraph.numCharacterRuns();
>> >>                for(int j = 0; j < numCharRuns; j++) {
>> >>
>> >>                        // Get a character run and recover it's text -
>> >> note
>> >> that
>> >>                        // the same text variable is used as for the
>> >> Paragraph
>> >> above.
>> >>                        // So, it MUST be safe to remove the text =
>> >> paragraph.text()
>> >>                        // line above.
>> >>                    charRun = paragraph.getCharacterRun(j);
>> >>                    text = charRun.text();
>> >>
>> >>                    // Check to see if the text of the CharacterRun
>> >> contains
>> >> the
>> >>                    // search term. If it does, find out where that
>> term
>> >> starts
>> >>                    // and call the replaceText() method passing the
>> >> index.
>> >>                    // Maybe this is the key difference between what we
>> >> are
>> >>                    // doing.
>> >>                    if(text.contains(searchTerm)) {
>> >>                        int start = text.indexOf(searchTerm);
>> >>                        charRun.replaceText(searchTerm,
>> replacementText,
>> >> start);
>> >>                    }
>> >>                }
>> >>            }
>> >>
>> >>            // Close the InputStream
>> >>            bufIStream.close();
>> >>            bufIStream = null;
>> >>
>> >>            // Open an OutputStream and write the document away.
>> >>            outputFile = new File(outputFilename);
>> >>            fileOStream = new FileOutputStream(outputFile);
>> >>            bufOStream = new BufferedOutputStream(fileOStream);
>> >>
>> >>            document.write(bufOStream);
>> >>
>> >>        }
>> >>        catch(Exception ex) {
>> >>            System.out.println("Caught an: " +
>> ex.getClass().getName());
>> >>            System.out.println("Message: " + ex.getMessage());
>> >>            System.out.println("Stacktrace follows.............");
>> >>            ex.printStackTrace(System.out);
>> >>        }
>> >>        finally {
>> >>            if(bufOStream != null) {
>> >>                try {
>> >>                    //bufOStream.flush();
>> >>                    bufOStream.close();
>> >>                    bufOStream = null;
>> >>                }
>> >>                catch(Exception ex) {
>> >>
>> >>                }
>> >>            }
>> >>            if(bufIStream != null) {
>> >>                try {
>> >>                    bufIStream.close();
>> >>                    bufIStream = null;
>> >>                }
>> >>                catch(Exception ex) {
>> >>                    // I G N O R E //
>> >>                }
>> >>            }
>> >>        }
>> >>
>> >>    }
>> >>
>> >>    /**
>> >>         * Search for and replace a single occurrence of a string of
>> text
>> >> within a
>> >>         * Word document.
>> >>         *
>> >>         * Note that no checks are made on the parameter's values; that
>> is
>> >> to say
>> >>         * that the file named in the InputFilename parameter will not
>> be
>> >> checked
>> >>         * to ensure the file exists and neither of the searchTerm nor
>> >>         * replacementTerm pare,eters will be checked to ensure they
>> are
>> >> not
>> >> null.
>> >>         * Also, note that I have never tested passing the same String
>> to
>> >> the
>> >>         * inputFilename and outputFilename parameters but cannot see
>> why
>> >> that
>> >>         * should not be possible.
>> >>         *
>> >>         * @param inputFilename An instance of the String class that
>> >> encapsulates
>> >>         *                      the name of and path to a Word document
>> >> which is
>> >>         *                      in the binary (OLE2CDF) format. The
>> >> contents
>> >> of
>> >> this
>> >>         *                      document will be searched for
>> occurrences
>> >> of
>> >> the
>> >>         *                      search term.
>> >>         * @param outputFilename An instance of the String class that
>> >> encapsulates
>> >>         *                       the name of and path to a Word
>> document
>> >> which is
>> >>         *                       in the binary (OLE2CDF) format. This
>> >> document will
>> >>         *                       contain the results of the search and
>> >> replace
>> >>         *                       operation.
>> >>         * @param replacements An instance of the java.util.HashMap
>> class
>> >> that
>> >>         *                     contains a series of key, value pairs.
>> Each
>> >> key
>> >>         *                     is an instance of the String class that
>> >> encapsulates
>> >>         *                     a series of characters, a word or words
>> >> that
>> >> the
>> >>         *                     code will search for and the
>> accompanying
>> >> value is
>> >>         *                     also an instance of the String class
>> that
>> >> likewise
>> >>         *                     encapsulates a series of characters, a
>> word
>> >> or
>> >> words.
>> >>         *                     The 'contents' of the value's String
>> will
>> >> be
>> >> used to
>> >>         *                     replace the contents of the key's String
>> if
>> >> an
>> >>         *                     occurrence of the latter is found.
>> >>         */
>> >>    public void searchAndReplace(String inputFilename,
>> >>                                 String outputFilename,
>> >>                                 HashMap<String, String> replacements)
>> {
>> >>
>> >>        File inputFile = null;
>> >>        File outputFile = null;
>> >>        FileInputStream fileIStream = null;
>> >>        FileOutputStream fileOStream = null;
>> >>        BufferedInputStream bufIStream = null;
>> >>        BufferedOutputStream bufOStream = null;
>> >>        POIFSFileSystem fileSystem = null;
>> >>        HWPFDocument document = null;
>> >>        Range docRange = null;
>> >>        Paragraph paragraph = null;
>> >>        CharacterRun charRun = null;
>> >>        Set<String> keySet = null;
>> >>        Iterator<String> keySetIterator = null;
>> >>        int numParagraphs = 0;
>> >>        int numCharRuns = 0;
>> >>        String text = null;
>> >>        String key = null;
>> >>        String value = null;
>> >>
>> >>        try {
>> >>            // Create an instance of the POIFSFileSystem class and
>> >>            // attach it to the Word document using an InputStream.
>> >>            inputFile = new File(inputFilename);
>> >>            fileIStream = new FileInputStream(inputFile);
>> >>            bufIStream = new BufferedInputStream(fileIStream);
>> >>            fileSystem = new POIFSFileSystem(bufIStream);
>> >>            document = new HWPFDocument(fileSystem);
>> >>
>> >>                        // Get a reference to the overall Range for the
>> >> document
>> >>                        // and discover how many Paragraphs objects
>> there
>> >> are
>> >>                        // in the document.
>> >>            docRange = document.getRange();
>> >>            numParagraphs = docRange.numParagraphs();
>> >>
>> >>            // Recover a Set of the keys in the HashMap
>> >>            keySet = replacements.keySet();
>> >>
>> >>            // Step through each Paragraph
>> >>            for(int i = 0; i < numParagraphs; i++) {
>> >>                paragraph = docRange.getParagraph(i);
>> >>                // This line can almost certainly be removed - see
>> >>                // the comments in the method above.
>> >>                text = paragraph.text();
>> >>
>> >>                // Get the number of CharacterRuns in the Paragraph
>> >>                // and step through each one.
>> >>                numCharRuns = paragraph.numCharacterRuns();
>> >>                for(int j = 0; j < numCharRuns; j++) {
>> >>                    charRun = paragraph.getCharacterRun(j);
>> >>
>> >>                    // Get the text from the CharacterRun and recover
>> an
>> >>                    // Iterator to step through the Set of keys.
>> >>                    text = charRun.text();
>> >>                    keySetIterator = keySet.iterator();
>> >>                    while(keySetIterator.hasNext()) {
>> >>
>> >>                        // Get the key - which is also the search term
>> -
>> >> and
>> >>                        // check to see if it can be found within the
>> >>                        // CharacterRuns text.
>> >>                        key = keySetIterator.next();
>> >>                        if(text.contains(key)) {
>> >>
>> >>                                // If the search term was found in the
>> >> text,
>> >> get
>> >> the
>> >>                                // matching value from the HashMap,
>> find
>> >> out
>> >> whereabouts
>> >>                                // in the CharacterRuns text the search
>> >> term
>> >> is
>> >>                                // and call the replaceText() method to
>> >> substitute
>> >>                                // the replacement term for the search
>> >> term.
>> >>                            value = replacements.get(key);
>> >>                            int start = text.indexOf(key);
>> >>                            charRun.replaceText(key, value, start);
>> >>
>> >>                            // Note that this code was added to test
>> >> whether
>> >>                            // it was possible to replace multiple
>> >> occurrences
>> >>                            // of the search term. I cannot remember if
>> I
>> >> tested
>> >>                            // it but believe that it did work; either
>> >> way,
>> >>                            // it could be tested now and if succeeds,
>> >> then
>> >> the
>> >>                            // searchAndReplace() method above could be
>> >> modified
>> >>                            // to include this.
>> >>                            docRange = document.getRange();
>> >>                            paragraph = docRange.getParagraph(i);
>> >>                            charRun = paragraph.getCharacterRun(j);
>> >>                            text = charRun.text();
>> >>                        }
>> >>                    }
>> >>                }
>> >>            }
>> >>
>> >>            // Close the InputStream
>> >>            bufIStream.close();
>> >>            bufIStream = null;
>> >>
>> >>            // Open an OutputStream and save the modified document
>> away.
>> >>            outputFile = new File(outputFilename);
>> >>            fileOStream = new FileOutputStream(outputFile);
>> >>            bufOStream = new BufferedOutputStream(fileOStream);
>> >>            document.write(bufOStream);
>> >>        }
>> >>        catch(Exception ex) {
>> >>            System.out.println("Caught an: " +
>> ex.getClass().getName());
>> >>            System.out.println("Message: " + ex.getMessage());
>> >>            System.out.println("Stacktrace follows.............");
>> >>            ex.printStackTrace(System.out);
>> >>        }
>> >>        finally {
>> >>            if(bufIStream != null) {
>> >>                try {
>> >>                    bufIStream.close();
>> >>                    bufIStream = null;
>> >>                }
>> >>                catch(Exception ex) {
>> >>                    // I G N O R E //
>> >>                }
>> >>            }
>> >>            if(bufOStream != null) {
>> >>                try {
>> >>                    bufOStream.flush();
>> >>                    bufOStream.close();
>> >>                    bufOStream = null;
>> >>                }
>> >>                catch(Exception ex) {
>> >>
>> >>                }
>> >>            }
>> >>        }
>> >>
>> >>    }
>> >>
>> >>        /**
>> >>         * The main entry point to the program demonstrating how the
>> code
>> >> may
>> >>         * be utilised.
>> >>         *
>> >>         * @param args An array of type String containing argumnets
>> passed
>> >> to the
>> >>         *             program on execution.
>> >>         */
>> >>        public static void main(String[] args) {
>> >>                SearchReplace replacer = new SearchReplace();
>> >>
>> >>                // To serach for and replace single items. Note, the
>> code
>> >> has not, at
>> >>                // least as far as I can remember, been tested by
>> passing
>> >> the same
>> >>                // file to both the searchTerm and replacementTerm
>> >> parameters. It ought
>> >>                // to work but has NOT been tested I believe.
>> >>                replacer.searchAndReplace("Document.doc",            //
>> >> Source Document
>> >>                        "Replaced Document.doc",
>> >> //
>> >> Result Document
>> >>                        "search term",
>> >> //
>> >> Search term
>> >>                        "replacement term");
>> >> //
>> >> Replacement term
>> >>
>> >>                // To search for and replace a series of items
>> >>                HashMap<String, String> searchTerms = new
>> HashMap<String,
>> >> String>();
>> >>                searchTerms.put("search term 1", "replacement term 1");
>> >>                searchTerms.put("search term 2", "replacement term 2");
>> >>                searchTerms.put("search term 3", "replacement term 3");
>> >>                searchTerms.put("search term 4", "replacement term 4");
>> >>
>> >>                replacer.searchAndReplace("Document.doc",    // Source
>> >> Document
>> >>                        "Replaced Document.doc",                 //
>> Result
>> >> Document
>> >>                        searchTerms)                             //
>> >> Search/replacement items
>> >>         }
>> >> }
>> >>
>> >>
>> >>
>> >> karthik-33 wrote:
>> >> >
>> >> > 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 <ma...@tiscali.co.uk>
>> 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: user-unsubscribe@poi.apache.org
>> >> >> For additional commands, e-mail: user-help@poi.apache.org
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >> > --
>> >> > karthik
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
>> >>  Sent from the POI - User mailing list archive at Nabble.com.
>> >>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> >> For additional commands, e-mail: user-help@poi.apache.org
>> >>
>> >>
>> >
>> >
>> > --
>> > karthik
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24885699.html
>> Sent from the POI - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
> 
> 
> -- 
> karthik
> 
> 

-- 
View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24894775.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by karthik <ka...@gmail.com>.
Mark, Thanks for trying out. I tried the same way you did...
Can you suggest me a way to resolve this..
What is the option one to try patch HWPF.. May be i can try if you have any
new way to approach this problem...
And the option two, SWT/OLE iam new to it and i dont know how to work on
that..

Thanks
Karthik
On Sun, Aug 9, 2009 at 6:07 AM, MSB <ma...@tiscali.co.uk> wrote:

>
> This morning, I had the chance to try what I spoke of in my previous email
> -
> using Java to replace text in a copy of the Paragraph's text and then
> replacing all of the text in the Paragraph with the modified copy. It
> worked
> but only up to a point; if the replacement text was exactly the same length
> as the search term, this technique worked but any differences in length
> rendered the resulting file corrupt; and I am guessing that this is the
> same
> problem as you originally encountered.
>
> So, I think I can conclude that if you are trying to replace the search
> term
> with a String of text that is longer than it, you will run into problems.
> As
> long as the replacement is shorter than the search term - or at most the
> same length - then the previous piece of code I posted seems to work well
> enough.
>
> To my mind then you have two options. Option 1 would be to patch HWPF so
> that it will work as you wish - the API is very immature and has not been
> the focus of the same sort of development effort as has HSSF for example.
> Option 2 is to use an alternative such as SWT/OLE or OpenOffice. The
> limitation with the OpenOffice approach is that whilst it can read OpenXML
> documents - Office 2007 and beyond with the .docx or similar extension - it
> cannot save a document in this format.
>
> Sorry for the bad news.
>
> Yours
>
> Mark B
>
>
> karthik-33 wrote:
> >
> > Hi Mark, Thanks for sending this program.
> > I tried this program with POI 3.2 final version, which iam currently
> > using.
> > CharaterRun doesnt behave consistently the same way, sometimes it splits
> > the
> > paragraph text into more number of Character run, sometimes it doesnt
> > split
> > and i see the whole paragraph text in one character run. So the search
> > text
> > is not getting replaced.
> > Is there anyway to solve this issue?
> > On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk> wrote:
> >
> >>
> >> Here is the HWPF based code that I put together to play around with. It
> >> was
> >> written a very long time ago so I am not sure what testing I undertook
> >> and
> >> exactly what the results were but I have run it this morning just to
> >> ensure
> >> that
> >> it does not crash the PC and all seems to be well. This section has been
> >> cut
> >> from a much larger class that is full of other test code that I play
> >> around
> >> with peridocically. Everything you need is there I believe but on the
> >> off-chance
> >> that it calls another method whose source I have neglected to include,
> >> just
> >> drop
> >> an email to the list please.
> >>
> >> Currently, I am running POI version 3.5 beta 7 on a PC operating under
> >> Windows XP SP2. Office 2007 is installed now and it seems able to open
> >> the
> >> files this code produces quite happily. In the back of my mind, I seem
> to
> >> remember that the files produced by some search and replace code I put
> >> together could be opened but not modified; I tested that problem this
> >> morning
> >> and the files this code produces seem fine, I can open them, make
> changes
> >> and
> >> then save the results again. But do please be prepared for problems like
> >> that.
> >>
> >> Again, can I emphasise this is test code; it is scruffy and there are
> >> going
> >> to
> >> be variables I put in there so that I could monitor the progress of the
> >> code
> >> by dumping messages to the screen. As you go through, if something seems
> >> to
> >> be superfluous, then this is likely the reason and you can comment it
> out
> >> or
> >> delete it.
> >>
> >> Good luck and I do hope it all works. If you have any problems, just
> drop
> >> a
> >> message onto the list.
> >>
> >> Yours
> >>
> >> Mark B
> >>
> >>
> >> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
> >> import org.apache.poi.hwpf.HWPFDocument;
> >> import org.apache.poi.hwpf.usermodel.Range;
> >> import org.apache.poi.hwpf.usermodel.Paragraph;
> >> import org.apache.poi.hwpf.usermodel.CharacterRun;
> >>
> >> import java.io.File;
> >> import java.io.FileOutputStream;
> >> import java.io.FileInputStream;
> >> import java.io.BufferedOutputStream;
> >> import java.io.BufferedInputStream;
> >> import java.util.HashMap;
> >> import java.util.Iterator;
> >>
> >> /**
> >>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
> >>  * CONDITIONS OF ANY KIND, either express or implied. It is not intended
> >> to
> >>  * be used in a 'production' environment without undergoing rigorous
> >> testing.
> >>  *
> >>  * With that out of the way, an instance of this class can be used to
> >> search
> >> for
> >>  * and replace Strings of text within a Word document. To see how the
> >> code
> >> may
> >>  * be used, look into the main() method for examples.
> >>  *
> >>  * Note the replacements made by the code contained within this class
> >> ignore
> >>  * any formatting that may have been applied to the text that is
> >> replaced.
> >> That
> >>  * is to say that if the text was originally formatted to use the Arial
> >> font,
> >>  * was sized to 24 points, emboldened, underlined and red in colour,
> then
> >> all
> >>  * of this will be lost if it is replaced. Further if any text is
> >> replaced
> >> in a
> >>  * Paragraph, all the formatting applied to that Paragraph's contents is
> >> likely
> >>  * to be lost.
> >>  *
> >>  * @author Mark Beardsley [msb at apache.org]
> >>  * @version 1.00 8th August 2009 (cannot remember when originally put
> >> together)
> >>  */
> >> public class SearchReplace {
> >>
> >>
> >>        /**
> >>         * Search for and replace a single occurrence of a string of text
> >> within a
> >>         * Word document.
> >>         *
> >>         * Note that no checks are made on the parameter's values; that
> is
> >> to say
> >>         * that the file named in the InputFilename parameter will not be
> >> checked
> >>         * to ensure the file exists and neither of the searchTerm nor
> >>         * replacementTerm parameters will be checked to ensure they are
> >> not
> >> null.
> >>         * Also, note that I have never tested passing the same String to
> >> the
> >>         * inputFilename and outputFilename parameters but cannot see why
> >> that
> >>         * should not be possible.
> >>         *
> >>         * @param inputFilename An instance of the String class that
> >> encapsulates
> >>         *                      the name of and path to a Word document
> >> which is
> >>         *                      in the binary (OLE2CDF) format. The
> >> contents
> >> of
> >> this
> >>         *                      document will be searched for occurrences
> >> of
> >> the
> >>         *                      search term.
> >>         * @param outputFilename An instance of the String class that
> >> encapsulates
> >>         *                       the name of and path to a Word document
> >> which is
> >>         *                       in the binary (OLE2CDF) format. This
> >> document will
> >>         *                       contain the results of the search and
> >> replace
> >>         *                       operation.
> >>         * @param searchTerm An instance of the String class that
> >> encapsulates a
> >>         *                   series of characters, a word or words. The
> >> document
> >>         *                   will be searched for occurrences of this
> >> String.
> >>         * @param replacementTerm An instance of the String class that
> >> contains a
> >>         *                        series of characters, a word or words.
> >> The
> >> String
> >>         *                        encapsulated by the searchTerm
> parameter
> >> will be
> >>         *                        replaced by the 'contents' of this
> >> parameter.
> >>         *
> >>         */
> >>        public void searchAndReplace(String inputFilename,
> >>                                 String outputFilename,
> >>                                 String searchTerm,
> >>                                 String replacementText) {
> >>
> >>        File inputFile = null;
> >>        File outputFile = null;
> >>        FileInputStream fileIStream = null;
> >>        FileOutputStream fileOStream = null;
> >>        BufferedInputStream bufIStream = null;
> >>        BufferedOutputStream bufOStream = null;
> >>        POIFSFileSystem fileSystem = null;
> >>        HWPFDocument document = null;
> >>        Range docRange = null;
> >>        Paragraph paragraph = null;
> >>        CharacterRun charRun = null;
> >>        int numParagraphs = 0;
> >>        int numCharRuns = 0;
> >>        String text = null;
> >>
> >>        try {
> >>            // Create an instance of the POIFSFileSystem class and
> >>            // attach it to the Word document using an InputStream.
> >>            inputFile = new File(inputFilename);
> >>            fileIStream = new FileInputStream(inputFile);
> >>            bufIStream = new BufferedInputStream(fileIStream);
> >>            fileSystem = new POIFSFileSystem(bufIStream);
> >>            document = new HWPFDocument(fileSystem);
> >>
> >>            // Get the overall Range object for the document. Note the
> >>            // use of the getRange() method and not the getOverallRange()
> >>            // method, this is just historic - when the code was
> >> originally
> >>            // written, I do not believe the latter method was part of
> the
> >> API.
> >>            docRange = document.getRange();
> >>
> >>            // Get the number of Paragraph(s) in the overall range and
> >> iterate
> >>            // through them
> >>            numParagraphs = docRange.numParagraphs();
> >>            for(int i = 0; i < numParagraphs; i++) {
> >>
> >>                // Get a Paragraph and recover the text from it. This
> step
> >> is
> >> far from
> >>                // necessary and I think I only got the text so that I
> >> could
> >> print
> >>                // it to screen as a diagnostic check to ensure that the
> >> Paragraph
> >>                // contained the text I was searching for. Experiment
> with
> >> this.
> >>                paragraph = docRange.getParagraph(i);
> >>                text = paragraph.text();
> >>
> >>                // Get the number of CharacterRuns in the Paragraph
> >>                numCharRuns = paragraph.numCharacterRuns();
> >>                for(int j = 0; j < numCharRuns; j++) {
> >>
> >>                        // Get a character run and recover it's text -
> >> note
> >> that
> >>                        // the same text variable is used as for the
> >> Paragraph
> >> above.
> >>                        // So, it MUST be safe to remove the text =
> >> paragraph.text()
> >>                        // line above.
> >>                    charRun = paragraph.getCharacterRun(j);
> >>                    text = charRun.text();
> >>
> >>                    // Check to see if the text of the CharacterRun
> >> contains
> >> the
> >>                    // search term. If it does, find out where that term
> >> starts
> >>                    // and call the replaceText() method passing the
> >> index.
> >>                    // Maybe this is the key difference between what we
> >> are
> >>                    // doing.
> >>                    if(text.contains(searchTerm)) {
> >>                        int start = text.indexOf(searchTerm);
> >>                        charRun.replaceText(searchTerm, replacementText,
> >> start);
> >>                    }
> >>                }
> >>            }
> >>
> >>            // Close the InputStream
> >>            bufIStream.close();
> >>            bufIStream = null;
> >>
> >>            // Open an OutputStream and write the document away.
> >>            outputFile = new File(outputFilename);
> >>            fileOStream = new FileOutputStream(outputFile);
> >>            bufOStream = new BufferedOutputStream(fileOStream);
> >>
> >>            document.write(bufOStream);
> >>
> >>        }
> >>        catch(Exception ex) {
> >>            System.out.println("Caught an: " + ex.getClass().getName());
> >>            System.out.println("Message: " + ex.getMessage());
> >>            System.out.println("Stacktrace follows.............");
> >>            ex.printStackTrace(System.out);
> >>        }
> >>        finally {
> >>            if(bufOStream != null) {
> >>                try {
> >>                    //bufOStream.flush();
> >>                    bufOStream.close();
> >>                    bufOStream = null;
> >>                }
> >>                catch(Exception ex) {
> >>
> >>                }
> >>            }
> >>            if(bufIStream != null) {
> >>                try {
> >>                    bufIStream.close();
> >>                    bufIStream = null;
> >>                }
> >>                catch(Exception ex) {
> >>                    // I G N O R E //
> >>                }
> >>            }
> >>        }
> >>
> >>    }
> >>
> >>    /**
> >>         * Search for and replace a single occurrence of a string of text
> >> within a
> >>         * Word document.
> >>         *
> >>         * Note that no checks are made on the parameter's values; that
> is
> >> to say
> >>         * that the file named in the InputFilename parameter will not be
> >> checked
> >>         * to ensure the file exists and neither of the searchTerm nor
> >>         * replacementTerm pare,eters will be checked to ensure they are
> >> not
> >> null.
> >>         * Also, note that I have never tested passing the same String to
> >> the
> >>         * inputFilename and outputFilename parameters but cannot see why
> >> that
> >>         * should not be possible.
> >>         *
> >>         * @param inputFilename An instance of the String class that
> >> encapsulates
> >>         *                      the name of and path to a Word document
> >> which is
> >>         *                      in the binary (OLE2CDF) format. The
> >> contents
> >> of
> >> this
> >>         *                      document will be searched for occurrences
> >> of
> >> the
> >>         *                      search term.
> >>         * @param outputFilename An instance of the String class that
> >> encapsulates
> >>         *                       the name of and path to a Word document
> >> which is
> >>         *                       in the binary (OLE2CDF) format. This
> >> document will
> >>         *                       contain the results of the search and
> >> replace
> >>         *                       operation.
> >>         * @param replacements An instance of the java.util.HashMap class
> >> that
> >>         *                     contains a series of key, value pairs.
> Each
> >> key
> >>         *                     is an instance of the String class that
> >> encapsulates
> >>         *                     a series of characters, a word or words
> >> that
> >> the
> >>         *                     code will search for and the accompanying
> >> value is
> >>         *                     also an instance of the String class that
> >> likewise
> >>         *                     encapsulates a series of characters, a
> word
> >> or
> >> words.
> >>         *                     The 'contents' of the value's String will
> >> be
> >> used to
> >>         *                     replace the contents of the key's String
> if
> >> an
> >>         *                     occurrence of the latter is found.
> >>         */
> >>    public void searchAndReplace(String inputFilename,
> >>                                 String outputFilename,
> >>                                 HashMap<String, String> replacements) {
> >>
> >>        File inputFile = null;
> >>        File outputFile = null;
> >>        FileInputStream fileIStream = null;
> >>        FileOutputStream fileOStream = null;
> >>        BufferedInputStream bufIStream = null;
> >>        BufferedOutputStream bufOStream = null;
> >>        POIFSFileSystem fileSystem = null;
> >>        HWPFDocument document = null;
> >>        Range docRange = null;
> >>        Paragraph paragraph = null;
> >>        CharacterRun charRun = null;
> >>        Set<String> keySet = null;
> >>        Iterator<String> keySetIterator = null;
> >>        int numParagraphs = 0;
> >>        int numCharRuns = 0;
> >>        String text = null;
> >>        String key = null;
> >>        String value = null;
> >>
> >>        try {
> >>            // Create an instance of the POIFSFileSystem class and
> >>            // attach it to the Word document using an InputStream.
> >>            inputFile = new File(inputFilename);
> >>            fileIStream = new FileInputStream(inputFile);
> >>            bufIStream = new BufferedInputStream(fileIStream);
> >>            fileSystem = new POIFSFileSystem(bufIStream);
> >>            document = new HWPFDocument(fileSystem);
> >>
> >>                        // Get a reference to the overall Range for the
> >> document
> >>                        // and discover how many Paragraphs objects there
> >> are
> >>                        // in the document.
> >>            docRange = document.getRange();
> >>            numParagraphs = docRange.numParagraphs();
> >>
> >>            // Recover a Set of the keys in the HashMap
> >>            keySet = replacements.keySet();
> >>
> >>            // Step through each Paragraph
> >>            for(int i = 0; i < numParagraphs; i++) {
> >>                paragraph = docRange.getParagraph(i);
> >>                // This line can almost certainly be removed - see
> >>                // the comments in the method above.
> >>                text = paragraph.text();
> >>
> >>                // Get the number of CharacterRuns in the Paragraph
> >>                // and step through each one.
> >>                numCharRuns = paragraph.numCharacterRuns();
> >>                for(int j = 0; j < numCharRuns; j++) {
> >>                    charRun = paragraph.getCharacterRun(j);
> >>
> >>                    // Get the text from the CharacterRun and recover an
> >>                    // Iterator to step through the Set of keys.
> >>                    text = charRun.text();
> >>                    keySetIterator = keySet.iterator();
> >>                    while(keySetIterator.hasNext()) {
> >>
> >>                        // Get the key - which is also the search term -
> >> and
> >>                        // check to see if it can be found within the
> >>                        // CharacterRuns text.
> >>                        key = keySetIterator.next();
> >>                        if(text.contains(key)) {
> >>
> >>                                // If the search term was found in the
> >> text,
> >> get
> >> the
> >>                                // matching value from the HashMap, find
> >> out
> >> whereabouts
> >>                                // in the CharacterRuns text the search
> >> term
> >> is
> >>                                // and call the replaceText() method to
> >> substitute
> >>                                // the replacement term for the search
> >> term.
> >>                            value = replacements.get(key);
> >>                            int start = text.indexOf(key);
> >>                            charRun.replaceText(key, value, start);
> >>
> >>                            // Note that this code was added to test
> >> whether
> >>                            // it was possible to replace multiple
> >> occurrences
> >>                            // of the search term. I cannot remember if I
> >> tested
> >>                            // it but believe that it did work; either
> >> way,
> >>                            // it could be tested now and if succeeds,
> >> then
> >> the
> >>                            // searchAndReplace() method above could be
> >> modified
> >>                            // to include this.
> >>                            docRange = document.getRange();
> >>                            paragraph = docRange.getParagraph(i);
> >>                            charRun = paragraph.getCharacterRun(j);
> >>                            text = charRun.text();
> >>                        }
> >>                    }
> >>                }
> >>            }
> >>
> >>            // Close the InputStream
> >>            bufIStream.close();
> >>            bufIStream = null;
> >>
> >>            // Open an OutputStream and save the modified document away.
> >>            outputFile = new File(outputFilename);
> >>            fileOStream = new FileOutputStream(outputFile);
> >>            bufOStream = new BufferedOutputStream(fileOStream);
> >>            document.write(bufOStream);
> >>        }
> >>        catch(Exception ex) {
> >>            System.out.println("Caught an: " + ex.getClass().getName());
> >>            System.out.println("Message: " + ex.getMessage());
> >>            System.out.println("Stacktrace follows.............");
> >>            ex.printStackTrace(System.out);
> >>        }
> >>        finally {
> >>            if(bufIStream != null) {
> >>                try {
> >>                    bufIStream.close();
> >>                    bufIStream = null;
> >>                }
> >>                catch(Exception ex) {
> >>                    // I G N O R E //
> >>                }
> >>            }
> >>            if(bufOStream != null) {
> >>                try {
> >>                    bufOStream.flush();
> >>                    bufOStream.close();
> >>                    bufOStream = null;
> >>                }
> >>                catch(Exception ex) {
> >>
> >>                }
> >>            }
> >>        }
> >>
> >>    }
> >>
> >>        /**
> >>         * The main entry point to the program demonstrating how the code
> >> may
> >>         * be utilised.
> >>         *
> >>         * @param args An array of type String containing argumnets
> passed
> >> to the
> >>         *             program on execution.
> >>         */
> >>        public static void main(String[] args) {
> >>                SearchReplace replacer = new SearchReplace();
> >>
> >>                // To serach for and replace single items. Note, the code
> >> has not, at
> >>                // least as far as I can remember, been tested by passing
> >> the same
> >>                // file to both the searchTerm and replacementTerm
> >> parameters. It ought
> >>                // to work but has NOT been tested I believe.
> >>                replacer.searchAndReplace("Document.doc",            //
> >> Source Document
> >>                        "Replaced Document.doc",
> >> //
> >> Result Document
> >>                        "search term",
> >> //
> >> Search term
> >>                        "replacement term");
> >> //
> >> Replacement term
> >>
> >>                // To search for and replace a series of items
> >>                HashMap<String, String> searchTerms = new HashMap<String,
> >> String>();
> >>                searchTerms.put("search term 1", "replacement term 1");
> >>                searchTerms.put("search term 2", "replacement term 2");
> >>                searchTerms.put("search term 3", "replacement term 3");
> >>                searchTerms.put("search term 4", "replacement term 4");
> >>
> >>                replacer.searchAndReplace("Document.doc",    // Source
> >> Document
> >>                        "Replaced Document.doc",                 //
> Result
> >> Document
> >>                        searchTerms)                             //
> >> Search/replacement items
> >>         }
> >> }
> >>
> >>
> >>
> >> karthik-33 wrote:
> >> >
> >> > 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 <ma...@tiscali.co.uk>
> 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: user-unsubscribe@poi.apache.org
> >> >> For additional commands, e-mail: user-help@poi.apache.org
> >> >>
> >> >>
> >> >
> >> >
> >> > --
> >> > karthik
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
> >>  Sent from the POI - User mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> >> For additional commands, e-mail: user-help@poi.apache.org
> >>
> >>
> >
> >
> > --
> > karthik
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24885699.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>


-- 
karthik

Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
This morning, I had the chance to try what I spoke of in my previous email -
using Java to replace text in a copy of the Paragraph's text and then
replacing all of the text in the Paragraph with the modified copy. It worked
but only up to a point; if the replacement text was exactly the same length
as the search term, this technique worked but any differences in length
rendered the resulting file corrupt; and I am guessing that this is the same
problem as you originally encountered.

So, I think I can conclude that if you are trying to replace the search term
with a String of text that is longer than it, you will run into problems. As
long as the replacement is shorter than the search term - or at most the
same length - then the previous piece of code I posted seems to work well
enough.

To my mind then you have two options. Option 1 would be to patch HWPF so
that it will work as you wish - the API is very immature and has not been
the focus of the same sort of development effort as has HSSF for example.
Option 2 is to use an alternative such as SWT/OLE or OpenOffice. The
limitation with the OpenOffice approach is that whilst it can read OpenXML
documents - Office 2007 and beyond with the .docx or similar extension - it
cannot save a document in this format.

Sorry for the bad news.

Yours

Mark B


karthik-33 wrote:
> 
> Hi Mark, Thanks for sending this program.
> I tried this program with POI 3.2 final version, which iam currently
> using.
> CharaterRun doesnt behave consistently the same way, sometimes it splits
> the
> paragraph text into more number of Character run, sometimes it doesnt
> split
> and i see the whole paragraph text in one character run. So the search
> text
> is not getting replaced.
> Is there anyway to solve this issue?
> On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk> wrote:
> 
>>
>> Here is the HWPF based code that I put together to play around with. It
>> was
>> written a very long time ago so I am not sure what testing I undertook
>> and
>> exactly what the results were but I have run it this morning just to
>> ensure
>> that
>> it does not crash the PC and all seems to be well. This section has been
>> cut
>> from a much larger class that is full of other test code that I play
>> around
>> with peridocically. Everything you need is there I believe but on the
>> off-chance
>> that it calls another method whose source I have neglected to include,
>> just
>> drop
>> an email to the list please.
>>
>> Currently, I am running POI version 3.5 beta 7 on a PC operating under
>> Windows XP SP2. Office 2007 is installed now and it seems able to open
>> the
>> files this code produces quite happily. In the back of my mind, I seem to
>> remember that the files produced by some search and replace code I put
>> together could be opened but not modified; I tested that problem this
>> morning
>> and the files this code produces seem fine, I can open them, make changes
>> and
>> then save the results again. But do please be prepared for problems like
>> that.
>>
>> Again, can I emphasise this is test code; it is scruffy and there are
>> going
>> to
>> be variables I put in there so that I could monitor the progress of the
>> code
>> by dumping messages to the screen. As you go through, if something seems
>> to
>> be superfluous, then this is likely the reason and you can comment it out
>> or
>> delete it.
>>
>> Good luck and I do hope it all works. If you have any problems, just drop
>> a
>> message onto the list.
>>
>> Yours
>>
>> Mark B
>>
>>
>> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
>> import org.apache.poi.hwpf.HWPFDocument;
>> import org.apache.poi.hwpf.usermodel.Range;
>> import org.apache.poi.hwpf.usermodel.Paragraph;
>> import org.apache.poi.hwpf.usermodel.CharacterRun;
>>
>> import java.io.File;
>> import java.io.FileOutputStream;
>> import java.io.FileInputStream;
>> import java.io.BufferedOutputStream;
>> import java.io.BufferedInputStream;
>> import java.util.HashMap;
>> import java.util.Iterator;
>>
>> /**
>>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
>>  * CONDITIONS OF ANY KIND, either express or implied. It is not intended
>> to
>>  * be used in a 'production' environment without undergoing rigorous
>> testing.
>>  *
>>  * With that out of the way, an instance of this class can be used to
>> search
>> for
>>  * and replace Strings of text within a Word document. To see how the
>> code
>> may
>>  * be used, look into the main() method for examples.
>>  *
>>  * Note the replacements made by the code contained within this class
>> ignore
>>  * any formatting that may have been applied to the text that is
>> replaced.
>> That
>>  * is to say that if the text was originally formatted to use the Arial
>> font,
>>  * was sized to 24 points, emboldened, underlined and red in colour, then
>> all
>>  * of this will be lost if it is replaced. Further if any text is
>> replaced
>> in a
>>  * Paragraph, all the formatting applied to that Paragraph's contents is
>> likely
>>  * to be lost.
>>  *
>>  * @author Mark Beardsley [msb at apache.org]
>>  * @version 1.00 8th August 2009 (cannot remember when originally put
>> together)
>>  */
>> public class SearchReplace {
>>
>>
>>        /**
>>         * Search for and replace a single occurrence of a string of text
>> within a
>>         * Word document.
>>         *
>>         * Note that no checks are made on the parameter's values; that is
>> to say
>>         * that the file named in the InputFilename parameter will not be
>> checked
>>         * to ensure the file exists and neither of the searchTerm nor
>>         * replacementTerm parameters will be checked to ensure they are
>> not
>> null.
>>         * Also, note that I have never tested passing the same String to
>> the
>>         * inputFilename and outputFilename parameters but cannot see why
>> that
>>         * should not be possible.
>>         *
>>         * @param inputFilename An instance of the String class that
>> encapsulates
>>         *                      the name of and path to a Word document
>> which is
>>         *                      in the binary (OLE2CDF) format. The
>> contents
>> of
>> this
>>         *                      document will be searched for occurrences
>> of
>> the
>>         *                      search term.
>>         * @param outputFilename An instance of the String class that
>> encapsulates
>>         *                       the name of and path to a Word document
>> which is
>>         *                       in the binary (OLE2CDF) format. This
>> document will
>>         *                       contain the results of the search and
>> replace
>>         *                       operation.
>>         * @param searchTerm An instance of the String class that
>> encapsulates a
>>         *                   series of characters, a word or words. The
>> document
>>         *                   will be searched for occurrences of this
>> String.
>>         * @param replacementTerm An instance of the String class that
>> contains a
>>         *                        series of characters, a word or words.
>> The
>> String
>>         *                        encapsulated by the searchTerm parameter
>> will be
>>         *                        replaced by the 'contents' of this
>> parameter.
>>         *
>>         */
>>        public void searchAndReplace(String inputFilename,
>>                                 String outputFilename,
>>                                 String searchTerm,
>>                                 String replacementText) {
>>
>>        File inputFile = null;
>>        File outputFile = null;
>>        FileInputStream fileIStream = null;
>>        FileOutputStream fileOStream = null;
>>        BufferedInputStream bufIStream = null;
>>        BufferedOutputStream bufOStream = null;
>>        POIFSFileSystem fileSystem = null;
>>        HWPFDocument document = null;
>>        Range docRange = null;
>>        Paragraph paragraph = null;
>>        CharacterRun charRun = null;
>>        int numParagraphs = 0;
>>        int numCharRuns = 0;
>>        String text = null;
>>
>>        try {
>>            // Create an instance of the POIFSFileSystem class and
>>            // attach it to the Word document using an InputStream.
>>            inputFile = new File(inputFilename);
>>            fileIStream = new FileInputStream(inputFile);
>>            bufIStream = new BufferedInputStream(fileIStream);
>>            fileSystem = new POIFSFileSystem(bufIStream);
>>            document = new HWPFDocument(fileSystem);
>>
>>            // Get the overall Range object for the document. Note the
>>            // use of the getRange() method and not the getOverallRange()
>>            // method, this is just historic - when the code was
>> originally
>>            // written, I do not believe the latter method was part of the
>> API.
>>            docRange = document.getRange();
>>
>>            // Get the number of Paragraph(s) in the overall range and
>> iterate
>>            // through them
>>            numParagraphs = docRange.numParagraphs();
>>            for(int i = 0; i < numParagraphs; i++) {
>>
>>                // Get a Paragraph and recover the text from it. This step
>> is
>> far from
>>                // necessary and I think I only got the text so that I
>> could
>> print
>>                // it to screen as a diagnostic check to ensure that the
>> Paragraph
>>                // contained the text I was searching for. Experiment with
>> this.
>>                paragraph = docRange.getParagraph(i);
>>                text = paragraph.text();
>>
>>                // Get the number of CharacterRuns in the Paragraph
>>                numCharRuns = paragraph.numCharacterRuns();
>>                for(int j = 0; j < numCharRuns; j++) {
>>
>>                        // Get a character run and recover it's text -
>> note
>> that
>>                        // the same text variable is used as for the
>> Paragraph
>> above.
>>                        // So, it MUST be safe to remove the text =
>> paragraph.text()
>>                        // line above.
>>                    charRun = paragraph.getCharacterRun(j);
>>                    text = charRun.text();
>>
>>                    // Check to see if the text of the CharacterRun
>> contains
>> the
>>                    // search term. If it does, find out where that term
>> starts
>>                    // and call the replaceText() method passing the
>> index.
>>                    // Maybe this is the key difference between what we
>> are
>>                    // doing.
>>                    if(text.contains(searchTerm)) {
>>                        int start = text.indexOf(searchTerm);
>>                        charRun.replaceText(searchTerm, replacementText,
>> start);
>>                    }
>>                }
>>            }
>>
>>            // Close the InputStream
>>            bufIStream.close();
>>            bufIStream = null;
>>
>>            // Open an OutputStream and write the document away.
>>            outputFile = new File(outputFilename);
>>            fileOStream = new FileOutputStream(outputFile);
>>            bufOStream = new BufferedOutputStream(fileOStream);
>>
>>            document.write(bufOStream);
>>
>>        }
>>        catch(Exception ex) {
>>            System.out.println("Caught an: " + ex.getClass().getName());
>>            System.out.println("Message: " + ex.getMessage());
>>            System.out.println("Stacktrace follows.............");
>>            ex.printStackTrace(System.out);
>>        }
>>        finally {
>>            if(bufOStream != null) {
>>                try {
>>                    //bufOStream.flush();
>>                    bufOStream.close();
>>                    bufOStream = null;
>>                }
>>                catch(Exception ex) {
>>
>>                }
>>            }
>>            if(bufIStream != null) {
>>                try {
>>                    bufIStream.close();
>>                    bufIStream = null;
>>                }
>>                catch(Exception ex) {
>>                    // I G N O R E //
>>                }
>>            }
>>        }
>>
>>    }
>>
>>    /**
>>         * Search for and replace a single occurrence of a string of text
>> within a
>>         * Word document.
>>         *
>>         * Note that no checks are made on the parameter's values; that is
>> to say
>>         * that the file named in the InputFilename parameter will not be
>> checked
>>         * to ensure the file exists and neither of the searchTerm nor
>>         * replacementTerm pare,eters will be checked to ensure they are
>> not
>> null.
>>         * Also, note that I have never tested passing the same String to
>> the
>>         * inputFilename and outputFilename parameters but cannot see why
>> that
>>         * should not be possible.
>>         *
>>         * @param inputFilename An instance of the String class that
>> encapsulates
>>         *                      the name of and path to a Word document
>> which is
>>         *                      in the binary (OLE2CDF) format. The
>> contents
>> of
>> this
>>         *                      document will be searched for occurrences
>> of
>> the
>>         *                      search term.
>>         * @param outputFilename An instance of the String class that
>> encapsulates
>>         *                       the name of and path to a Word document
>> which is
>>         *                       in the binary (OLE2CDF) format. This
>> document will
>>         *                       contain the results of the search and
>> replace
>>         *                       operation.
>>         * @param replacements An instance of the java.util.HashMap class
>> that
>>         *                     contains a series of key, value pairs. Each
>> key
>>         *                     is an instance of the String class that
>> encapsulates
>>         *                     a series of characters, a word or words
>> that
>> the
>>         *                     code will search for and the accompanying
>> value is
>>         *                     also an instance of the String class that
>> likewise
>>         *                     encapsulates a series of characters, a word
>> or
>> words.
>>         *                     The 'contents' of the value's String will
>> be
>> used to
>>         *                     replace the contents of the key's String if
>> an
>>         *                     occurrence of the latter is found.
>>         */
>>    public void searchAndReplace(String inputFilename,
>>                                 String outputFilename,
>>                                 HashMap<String, String> replacements) {
>>
>>        File inputFile = null;
>>        File outputFile = null;
>>        FileInputStream fileIStream = null;
>>        FileOutputStream fileOStream = null;
>>        BufferedInputStream bufIStream = null;
>>        BufferedOutputStream bufOStream = null;
>>        POIFSFileSystem fileSystem = null;
>>        HWPFDocument document = null;
>>        Range docRange = null;
>>        Paragraph paragraph = null;
>>        CharacterRun charRun = null;
>>        Set<String> keySet = null;
>>        Iterator<String> keySetIterator = null;
>>        int numParagraphs = 0;
>>        int numCharRuns = 0;
>>        String text = null;
>>        String key = null;
>>        String value = null;
>>
>>        try {
>>            // Create an instance of the POIFSFileSystem class and
>>            // attach it to the Word document using an InputStream.
>>            inputFile = new File(inputFilename);
>>            fileIStream = new FileInputStream(inputFile);
>>            bufIStream = new BufferedInputStream(fileIStream);
>>            fileSystem = new POIFSFileSystem(bufIStream);
>>            document = new HWPFDocument(fileSystem);
>>
>>                        // Get a reference to the overall Range for the
>> document
>>                        // and discover how many Paragraphs objects there
>> are
>>                        // in the document.
>>            docRange = document.getRange();
>>            numParagraphs = docRange.numParagraphs();
>>
>>            // Recover a Set of the keys in the HashMap
>>            keySet = replacements.keySet();
>>
>>            // Step through each Paragraph
>>            for(int i = 0; i < numParagraphs; i++) {
>>                paragraph = docRange.getParagraph(i);
>>                // This line can almost certainly be removed - see
>>                // the comments in the method above.
>>                text = paragraph.text();
>>
>>                // Get the number of CharacterRuns in the Paragraph
>>                // and step through each one.
>>                numCharRuns = paragraph.numCharacterRuns();
>>                for(int j = 0; j < numCharRuns; j++) {
>>                    charRun = paragraph.getCharacterRun(j);
>>
>>                    // Get the text from the CharacterRun and recover an
>>                    // Iterator to step through the Set of keys.
>>                    text = charRun.text();
>>                    keySetIterator = keySet.iterator();
>>                    while(keySetIterator.hasNext()) {
>>
>>                        // Get the key - which is also the search term -
>> and
>>                        // check to see if it can be found within the
>>                        // CharacterRuns text.
>>                        key = keySetIterator.next();
>>                        if(text.contains(key)) {
>>
>>                                // If the search term was found in the
>> text,
>> get
>> the
>>                                // matching value from the HashMap, find
>> out
>> whereabouts
>>                                // in the CharacterRuns text the search
>> term
>> is
>>                                // and call the replaceText() method to
>> substitute
>>                                // the replacement term for the search
>> term.
>>                            value = replacements.get(key);
>>                            int start = text.indexOf(key);
>>                            charRun.replaceText(key, value, start);
>>
>>                            // Note that this code was added to test
>> whether
>>                            // it was possible to replace multiple
>> occurrences
>>                            // of the search term. I cannot remember if I
>> tested
>>                            // it but believe that it did work; either
>> way,
>>                            // it could be tested now and if succeeds,
>> then
>> the
>>                            // searchAndReplace() method above could be
>> modified
>>                            // to include this.
>>                            docRange = document.getRange();
>>                            paragraph = docRange.getParagraph(i);
>>                            charRun = paragraph.getCharacterRun(j);
>>                            text = charRun.text();
>>                        }
>>                    }
>>                }
>>            }
>>
>>            // Close the InputStream
>>            bufIStream.close();
>>            bufIStream = null;
>>
>>            // Open an OutputStream and save the modified document away.
>>            outputFile = new File(outputFilename);
>>            fileOStream = new FileOutputStream(outputFile);
>>            bufOStream = new BufferedOutputStream(fileOStream);
>>            document.write(bufOStream);
>>        }
>>        catch(Exception ex) {
>>            System.out.println("Caught an: " + ex.getClass().getName());
>>            System.out.println("Message: " + ex.getMessage());
>>            System.out.println("Stacktrace follows.............");
>>            ex.printStackTrace(System.out);
>>        }
>>        finally {
>>            if(bufIStream != null) {
>>                try {
>>                    bufIStream.close();
>>                    bufIStream = null;
>>                }
>>                catch(Exception ex) {
>>                    // I G N O R E //
>>                }
>>            }
>>            if(bufOStream != null) {
>>                try {
>>                    bufOStream.flush();
>>                    bufOStream.close();
>>                    bufOStream = null;
>>                }
>>                catch(Exception ex) {
>>
>>                }
>>            }
>>        }
>>
>>    }
>>
>>        /**
>>         * The main entry point to the program demonstrating how the code
>> may
>>         * be utilised.
>>         *
>>         * @param args An array of type String containing argumnets passed
>> to the
>>         *             program on execution.
>>         */
>>        public static void main(String[] args) {
>>                SearchReplace replacer = new SearchReplace();
>>
>>                // To serach for and replace single items. Note, the code
>> has not, at
>>                // least as far as I can remember, been tested by passing
>> the same
>>                // file to both the searchTerm and replacementTerm
>> parameters. It ought
>>                // to work but has NOT been tested I believe.
>>                replacer.searchAndReplace("Document.doc",            //
>> Source Document
>>                        "Replaced Document.doc",                        
>> //
>> Result Document
>>                        "search term",                                  
>> //
>> Search term
>>                        "replacement term");                            
>> //
>> Replacement term
>>
>>                // To search for and replace a series of items
>>                HashMap<String, String> searchTerms = new HashMap<String,
>> String>();
>>                searchTerms.put("search term 1", "replacement term 1");
>>                searchTerms.put("search term 2", "replacement term 2");
>>                searchTerms.put("search term 3", "replacement term 3");
>>                searchTerms.put("search term 4", "replacement term 4");
>>
>>                replacer.searchAndReplace("Document.doc",    // Source
>> Document
>>                        "Replaced Document.doc",                 // Result
>> Document
>>                        searchTerms)                             //
>> Search/replacement items
>>         }
>> }
>>
>>
>>
>> karthik-33 wrote:
>> >
>> > 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 <ma...@tiscali.co.uk> 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: user-unsubscribe@poi.apache.org
>> >> For additional commands, e-mail: user-help@poi.apache.org
>> >>
>> >>
>> >
>> >
>> > --
>> > karthik
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
>>  Sent from the POI - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
> 
> 
> -- 
> karthik
> 
> 

-- 
View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24885699.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by karthik <ka...@gmail.com>.
Hi Mark, Thanks for sending this program.
I tried this program with POI 3.2 final version, which iam currently using.
CharaterRun doesnt behave consistently the same way, sometimes it splits the
paragraph text into more number of Character run, sometimes it doesnt split
and i see the whole paragraph text in one character run. So the search text
is not getting replaced.
Is there anyway to solve this issue?
On Sat, Aug 8, 2009 at 6:34 AM, MSB <ma...@tiscali.co.uk> wrote:

>
> Here is the HWPF based code that I put together to play around with. It was
> written a very long time ago so I am not sure what testing I undertook and
> exactly what the results were but I have run it this morning just to ensure
> that
> it does not crash the PC and all seems to be well. This section has been
> cut
> from a much larger class that is full of other test code that I play around
> with peridocically. Everything you need is there I believe but on the
> off-chance
> that it calls another method whose source I have neglected to include, just
> drop
> an email to the list please.
>
> Currently, I am running POI version 3.5 beta 7 on a PC operating under
> Windows XP SP2. Office 2007 is installed now and it seems able to open the
> files this code produces quite happily. In the back of my mind, I seem to
> remember that the files produced by some search and replace code I put
> together could be opened but not modified; I tested that problem this
> morning
> and the files this code produces seem fine, I can open them, make changes
> and
> then save the results again. But do please be prepared for problems like
> that.
>
> Again, can I emphasise this is test code; it is scruffy and there are going
> to
> be variables I put in there so that I could monitor the progress of the
> code
> by dumping messages to the screen. As you go through, if something seems to
> be superfluous, then this is likely the reason and you can comment it out
> or
> delete it.
>
> Good luck and I do hope it all works. If you have any problems, just drop a
> message onto the list.
>
> Yours
>
> Mark B
>
>
> import org.apache.poi.poifs.filesystem.POIFSFileSystem;
> import org.apache.poi.hwpf.HWPFDocument;
> import org.apache.poi.hwpf.usermodel.Range;
> import org.apache.poi.hwpf.usermodel.Paragraph;
> import org.apache.poi.hwpf.usermodel.CharacterRun;
>
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.FileInputStream;
> import java.io.BufferedOutputStream;
> import java.io.BufferedInputStream;
> import java.util.HashMap;
> import java.util.Iterator;
>
> /**
>  * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR
>  * CONDITIONS OF ANY KIND, either express or implied. It is not intended to
>  * be used in a 'production' environment without undergoing rigorous
> testing.
>  *
>  * With that out of the way, an instance of this class can be used to
> search
> for
>  * and replace Strings of text within a Word document. To see how the code
> may
>  * be used, look into the main() method for examples.
>  *
>  * Note the replacements made by the code contained within this class
> ignore
>  * any formatting that may have been applied to the text that is replaced.
> That
>  * is to say that if the text was originally formatted to use the Arial
> font,
>  * was sized to 24 points, emboldened, underlined and red in colour, then
> all
>  * of this will be lost if it is replaced. Further if any text is replaced
> in a
>  * Paragraph, all the formatting applied to that Paragraph's contents is
> likely
>  * to be lost.
>  *
>  * @author Mark Beardsley [msb at apache.org]
>  * @version 1.00 8th August 2009 (cannot remember when originally put
> together)
>  */
> public class SearchReplace {
>
>
>        /**
>         * Search for and replace a single occurrence of a string of text
> within a
>         * Word document.
>         *
>         * Note that no checks are made on the parameter's values; that is
> to say
>         * that the file named in the InputFilename parameter will not be
> checked
>         * to ensure the file exists and neither of the searchTerm nor
>         * replacementTerm parameters will be checked to ensure they are not
> null.
>         * Also, note that I have never tested passing the same String to
> the
>         * inputFilename and outputFilename parameters but cannot see why
> that
>         * should not be possible.
>         *
>         * @param inputFilename An instance of the String class that
> encapsulates
>         *                      the name of and path to a Word document
> which is
>         *                      in the binary (OLE2CDF) format. The contents
> of
> this
>         *                      document will be searched for occurrences of
> the
>         *                      search term.
>         * @param outputFilename An instance of the String class that
> encapsulates
>         *                       the name of and path to a Word document
> which is
>         *                       in the binary (OLE2CDF) format. This
> document will
>         *                       contain the results of the search and
> replace
>         *                       operation.
>         * @param searchTerm An instance of the String class that
> encapsulates a
>         *                   series of characters, a word or words. The
> document
>         *                   will be searched for occurrences of this
> String.
>         * @param replacementTerm An instance of the String class that
> contains a
>         *                        series of characters, a word or words. The
> String
>         *                        encapsulated by the searchTerm parameter
> will be
>         *                        replaced by the 'contents' of this
> parameter.
>         *
>         */
>        public void searchAndReplace(String inputFilename,
>                                 String outputFilename,
>                                 String searchTerm,
>                                 String replacementText) {
>
>        File inputFile = null;
>        File outputFile = null;
>        FileInputStream fileIStream = null;
>        FileOutputStream fileOStream = null;
>        BufferedInputStream bufIStream = null;
>        BufferedOutputStream bufOStream = null;
>        POIFSFileSystem fileSystem = null;
>        HWPFDocument document = null;
>        Range docRange = null;
>        Paragraph paragraph = null;
>        CharacterRun charRun = null;
>        int numParagraphs = 0;
>        int numCharRuns = 0;
>        String text = null;
>
>        try {
>            // Create an instance of the POIFSFileSystem class and
>            // attach it to the Word document using an InputStream.
>            inputFile = new File(inputFilename);
>            fileIStream = new FileInputStream(inputFile);
>            bufIStream = new BufferedInputStream(fileIStream);
>            fileSystem = new POIFSFileSystem(bufIStream);
>            document = new HWPFDocument(fileSystem);
>
>            // Get the overall Range object for the document. Note the
>            // use of the getRange() method and not the getOverallRange()
>            // method, this is just historic - when the code was originally
>            // written, I do not believe the latter method was part of the
> API.
>            docRange = document.getRange();
>
>            // Get the number of Paragraph(s) in the overall range and
> iterate
>            // through them
>            numParagraphs = docRange.numParagraphs();
>            for(int i = 0; i < numParagraphs; i++) {
>
>                // Get a Paragraph and recover the text from it. This step
> is
> far from
>                // necessary and I think I only got the text so that I could
> print
>                // it to screen as a diagnostic check to ensure that the
> Paragraph
>                // contained the text I was searching for. Experiment with
> this.
>                paragraph = docRange.getParagraph(i);
>                text = paragraph.text();
>
>                // Get the number of CharacterRuns in the Paragraph
>                numCharRuns = paragraph.numCharacterRuns();
>                for(int j = 0; j < numCharRuns; j++) {
>
>                        // Get a character run and recover it's text - note
> that
>                        // the same text variable is used as for the
> Paragraph
> above.
>                        // So, it MUST be safe to remove the text =
> paragraph.text()
>                        // line above.
>                    charRun = paragraph.getCharacterRun(j);
>                    text = charRun.text();
>
>                    // Check to see if the text of the CharacterRun contains
> the
>                    // search term. If it does, find out where that term
> starts
>                    // and call the replaceText() method passing the index.
>                    // Maybe this is the key difference between what we are
>                    // doing.
>                    if(text.contains(searchTerm)) {
>                        int start = text.indexOf(searchTerm);
>                        charRun.replaceText(searchTerm, replacementText,
> start);
>                    }
>                }
>            }
>
>            // Close the InputStream
>            bufIStream.close();
>            bufIStream = null;
>
>            // Open an OutputStream and write the document away.
>            outputFile = new File(outputFilename);
>            fileOStream = new FileOutputStream(outputFile);
>            bufOStream = new BufferedOutputStream(fileOStream);
>
>            document.write(bufOStream);
>
>        }
>        catch(Exception ex) {
>            System.out.println("Caught an: " + ex.getClass().getName());
>            System.out.println("Message: " + ex.getMessage());
>            System.out.println("Stacktrace follows.............");
>            ex.printStackTrace(System.out);
>        }
>        finally {
>            if(bufOStream != null) {
>                try {
>                    //bufOStream.flush();
>                    bufOStream.close();
>                    bufOStream = null;
>                }
>                catch(Exception ex) {
>
>                }
>            }
>            if(bufIStream != null) {
>                try {
>                    bufIStream.close();
>                    bufIStream = null;
>                }
>                catch(Exception ex) {
>                    // I G N O R E //
>                }
>            }
>        }
>
>    }
>
>    /**
>         * Search for and replace a single occurrence of a string of text
> within a
>         * Word document.
>         *
>         * Note that no checks are made on the parameter's values; that is
> to say
>         * that the file named in the InputFilename parameter will not be
> checked
>         * to ensure the file exists and neither of the searchTerm nor
>         * replacementTerm pare,eters will be checked to ensure they are not
> null.
>         * Also, note that I have never tested passing the same String to
> the
>         * inputFilename and outputFilename parameters but cannot see why
> that
>         * should not be possible.
>         *
>         * @param inputFilename An instance of the String class that
> encapsulates
>         *                      the name of and path to a Word document
> which is
>         *                      in the binary (OLE2CDF) format. The contents
> of
> this
>         *                      document will be searched for occurrences of
> the
>         *                      search term.
>         * @param outputFilename An instance of the String class that
> encapsulates
>         *                       the name of and path to a Word document
> which is
>         *                       in the binary (OLE2CDF) format. This
> document will
>         *                       contain the results of the search and
> replace
>         *                       operation.
>         * @param replacements An instance of the java.util.HashMap class
> that
>         *                     contains a series of key, value pairs. Each
> key
>         *                     is an instance of the String class that
> encapsulates
>         *                     a series of characters, a word or words that
> the
>         *                     code will search for and the accompanying
> value is
>         *                     also an instance of the String class that
> likewise
>         *                     encapsulates a series of characters, a word
> or
> words.
>         *                     The 'contents' of the value's String will be
> used to
>         *                     replace the contents of the key's String if
> an
>         *                     occurrence of the latter is found.
>         */
>    public void searchAndReplace(String inputFilename,
>                                 String outputFilename,
>                                 HashMap<String, String> replacements) {
>
>        File inputFile = null;
>        File outputFile = null;
>        FileInputStream fileIStream = null;
>        FileOutputStream fileOStream = null;
>        BufferedInputStream bufIStream = null;
>        BufferedOutputStream bufOStream = null;
>        POIFSFileSystem fileSystem = null;
>        HWPFDocument document = null;
>        Range docRange = null;
>        Paragraph paragraph = null;
>        CharacterRun charRun = null;
>        Set<String> keySet = null;
>        Iterator<String> keySetIterator = null;
>        int numParagraphs = 0;
>        int numCharRuns = 0;
>        String text = null;
>        String key = null;
>        String value = null;
>
>        try {
>            // Create an instance of the POIFSFileSystem class and
>            // attach it to the Word document using an InputStream.
>            inputFile = new File(inputFilename);
>            fileIStream = new FileInputStream(inputFile);
>            bufIStream = new BufferedInputStream(fileIStream);
>            fileSystem = new POIFSFileSystem(bufIStream);
>            document = new HWPFDocument(fileSystem);
>
>                        // Get a reference to the overall Range for the
> document
>                        // and discover how many Paragraphs objects there
> are
>                        // in the document.
>            docRange = document.getRange();
>            numParagraphs = docRange.numParagraphs();
>
>            // Recover a Set of the keys in the HashMap
>            keySet = replacements.keySet();
>
>            // Step through each Paragraph
>            for(int i = 0; i < numParagraphs; i++) {
>                paragraph = docRange.getParagraph(i);
>                // This line can almost certainly be removed - see
>                // the comments in the method above.
>                text = paragraph.text();
>
>                // Get the number of CharacterRuns in the Paragraph
>                // and step through each one.
>                numCharRuns = paragraph.numCharacterRuns();
>                for(int j = 0; j < numCharRuns; j++) {
>                    charRun = paragraph.getCharacterRun(j);
>
>                    // Get the text from the CharacterRun and recover an
>                    // Iterator to step through the Set of keys.
>                    text = charRun.text();
>                    keySetIterator = keySet.iterator();
>                    while(keySetIterator.hasNext()) {
>
>                        // Get the key - which is also the search term - and
>                        // check to see if it can be found within the
>                        // CharacterRuns text.
>                        key = keySetIterator.next();
>                        if(text.contains(key)) {
>
>                                // If the search term was found in the text,
> get
> the
>                                // matching value from the HashMap, find out
> whereabouts
>                                // in the CharacterRuns text the search term
> is
>                                // and call the replaceText() method to
> substitute
>                                // the replacement term for the search term.
>                            value = replacements.get(key);
>                            int start = text.indexOf(key);
>                            charRun.replaceText(key, value, start);
>
>                            // Note that this code was added to test whether
>                            // it was possible to replace multiple
> occurrences
>                            // of the search term. I cannot remember if I
> tested
>                            // it but believe that it did work; either way,
>                            // it could be tested now and if succeeds, then
> the
>                            // searchAndReplace() method above could be
> modified
>                            // to include this.
>                            docRange = document.getRange();
>                            paragraph = docRange.getParagraph(i);
>                            charRun = paragraph.getCharacterRun(j);
>                            text = charRun.text();
>                        }
>                    }
>                }
>            }
>
>            // Close the InputStream
>            bufIStream.close();
>            bufIStream = null;
>
>            // Open an OutputStream and save the modified document away.
>            outputFile = new File(outputFilename);
>            fileOStream = new FileOutputStream(outputFile);
>            bufOStream = new BufferedOutputStream(fileOStream);
>            document.write(bufOStream);
>        }
>        catch(Exception ex) {
>            System.out.println("Caught an: " + ex.getClass().getName());
>            System.out.println("Message: " + ex.getMessage());
>            System.out.println("Stacktrace follows.............");
>            ex.printStackTrace(System.out);
>        }
>        finally {
>            if(bufIStream != null) {
>                try {
>                    bufIStream.close();
>                    bufIStream = null;
>                }
>                catch(Exception ex) {
>                    // I G N O R E //
>                }
>            }
>            if(bufOStream != null) {
>                try {
>                    bufOStream.flush();
>                    bufOStream.close();
>                    bufOStream = null;
>                }
>                catch(Exception ex) {
>
>                }
>            }
>        }
>
>    }
>
>        /**
>         * The main entry point to the program demonstrating how the code
> may
>         * be utilised.
>         *
>         * @param args An array of type String containing argumnets passed
> to the
>         *             program on execution.
>         */
>        public static void main(String[] args) {
>                SearchReplace replacer = new SearchReplace();
>
>                // To serach for and replace single items. Note, the code
> has not, at
>                // least as far as I can remember, been tested by passing
> the same
>                // file to both the searchTerm and replacementTerm
> parameters. It ought
>                // to work but has NOT been tested I believe.
>                replacer.searchAndReplace("Document.doc",            //
> Source Document
>                        "Replaced Document.doc",                         //
> Result Document
>                        "search term",                                   //
> Search term
>                        "replacement term");                             //
> Replacement term
>
>                // To search for and replace a series of items
>                HashMap<String, String> searchTerms = new HashMap<String,
> String>();
>                searchTerms.put("search term 1", "replacement term 1");
>                searchTerms.put("search term 2", "replacement term 2");
>                searchTerms.put("search term 3", "replacement term 3");
>                searchTerms.put("search term 4", "replacement term 4");
>
>                replacer.searchAndReplace("Document.doc",    // Source
> Document
>                        "Replaced Document.doc",                 // Result
> Document
>                        searchTerms)                             //
> Search/replacement items
>         }
> }
>
>
>
> karthik-33 wrote:
> >
> > 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 <ma...@tiscali.co.uk> 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: user-unsubscribe@poi.apache.org
> >> For additional commands, e-mail: user-help@poi.apache.org
> >>
> >>
> >
> >
> > --
> > karthik
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
>  Sent from the POI - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>


-- 
karthik

Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
Here is the HWPF based code that I put together to play around with. It was
written a very long time ago so I am not sure what testing I undertook and 
exactly what the results were but I have run it this morning just to ensure
that
it does not crash the PC and all seems to be well. This section has been cut
from a much larger class that is full of other test code that I play around
with peridocically. Everything you need is there I believe but on the
off-chance
that it calls another method whose source I have neglected to include, just
drop
an email to the list please.

Currently, I am running POI version 3.5 beta 7 on a PC operating under
Windows XP SP2. Office 2007 is installed now and it seems able to open the
files this code produces quite happily. In the back of my mind, I seem to
remember that the files produced by some search and replace code I put
together could be opened but not modified; I tested that problem this
morning
and the files this code produces seem fine, I can open them, make changes
and
then save the results again. But do please be prepared for problems like
that.

Again, can I emphasise this is test code; it is scruffy and there are going
to
be variables I put in there so that I could monitor the progress of the code
by dumping messages to the screen. As you go through, if something seems to
be superfluous, then this is likely the reason and you can comment it out or
delete it.

Good luck and I do hope it all works. If you have any problems, just drop a
message onto the list.

Yours

Mark B


import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.CharacterRun;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.Iterator;

/**
 * This code is provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied. It is not intended to
 * be used in a 'production' environment without undergoing rigorous
testing.
 *
 * With that out of the way, an instance of this class can be used to search
for
 * and replace Strings of text within a Word document. To see how the code
may
 * be used, look into the main() method for examples.
 *
 * Note the replacements made by the code contained within this class ignore
 * any formatting that may have been applied to the text that is replaced.
That
 * is to say that if the text was originally formatted to use the Arial
font,
 * was sized to 24 points, emboldened, underlined and red in colour, then
all
 * of this will be lost if it is replaced. Further if any text is replaced
in a
 * Paragraph, all the formatting applied to that Paragraph's contents is
likely
 * to be lost.
 *
 * @author Mark Beardsley [msb at apache.org]
 * @version 1.00 8th August 2009 (cannot remember when originally put
together)
 */
public class SearchReplace {
	

	/**
	 * Search for and replace a single occurrence of a string of text within a
	 * Word document.
	 *
	 * Note that no checks are made on the parameter's values; that is to say
	 * that the file named in the InputFilename parameter will not be checked
	 * to ensure the file exists and neither of the searchTerm nor
	 * replacementTerm parameters will be checked to ensure they are not null.
	 * Also, note that I have never tested passing the same String to the
	 * inputFilename and outputFilename parameters but cannot see why that
	 * should not be possible.
	 *
	 * @param inputFilename An instance of the String class that encapsulates
	 *                      the name of and path to a Word document which is
	 *                      in the binary (OLE2CDF) format. The contents of
this
	 *                      document will be searched for occurrences of the
	 *                      search term.
	 * @param outputFilename An instance of the String class that encapsulates
	 *                       the name of and path to a Word document which is
	 *                       in the binary (OLE2CDF) format. This document will
	 *                       contain the results of the search and replace
	 *                       operation.
	 * @param searchTerm An instance of the String class that encapsulates a
	 *                   series of characters, a word or words. The document
	 *                   will be searched for occurrences of this String.
	 * @param replacementTerm An instance of the String class that contains a
	 *                        series of characters, a word or words. The String
	 *                        encapsulated by the searchTerm parameter will be
	 *                        replaced by the 'contents' of this parameter.
	 *
	 */
	public void searchAndReplace(String inputFilename,
                                 String outputFilename,
                                 String searchTerm,
                                 String replacementText) {
        
        File inputFile = null;
        File outputFile = null;
        FileInputStream fileIStream = null;
        FileOutputStream fileOStream = null;
        BufferedInputStream bufIStream = null;
        BufferedOutputStream bufOStream = null;
        POIFSFileSystem fileSystem = null;
        HWPFDocument document = null;
        Range docRange = null;
        Paragraph paragraph = null;
        CharacterRun charRun = null;
        int numParagraphs = 0;
        int numCharRuns = 0;
        String text = null;
        
        try {
            // Create an instance of the POIFSFileSystem class and
            // attach it to the Word document using an InputStream.
            inputFile = new File(inputFilename);
            fileIStream = new FileInputStream(inputFile);
            bufIStream = new BufferedInputStream(fileIStream);
            fileSystem = new POIFSFileSystem(bufIStream);
            document = new HWPFDocument(fileSystem);
            
            // Get the overall Range object for the document. Note the
            // use of the getRange() method and not the getOverallRange()
            // method, this is just historic - when the code was originally
            // written, I do not believe the latter method was part of the
API.
            docRange = document.getRange();
            
            // Get the number of Paragraph(s) in the overall range and
iterate
            // through them
            numParagraphs = docRange.numParagraphs();
            for(int i = 0; i < numParagraphs; i++) {
            	
            	// Get a Paragraph and recover the text from it. This step is
far from
            	// necessary and I think I only got the text so that I could
print
            	// it to screen as a diagnostic check to ensure that the
Paragraph
            	// contained the text I was searching for. Experiment with
this.
                paragraph = docRange.getParagraph(i);
                text = paragraph.text();
                
                // Get the number of CharacterRuns in the Paragraph
                numCharRuns = paragraph.numCharacterRuns();
                for(int j = 0; j < numCharRuns; j++) {
                	
                	// Get a character run and recover it's text - note that
                	// the same text variable is used as for the Paragraph
above.
                	// So, it MUST be safe to remove the text =
paragraph.text()
                	// line above.
                    charRun = paragraph.getCharacterRun(j);
                    text = charRun.text();
                    
                    // Check to see if the text of the CharacterRun contains
the
                    // search term. If it does, find out where that term
starts
                    // and call the replaceText() method passing the index.
                    // Maybe this is the key difference between what we are
                    // doing.
                    if(text.contains(searchTerm)) {
                        int start = text.indexOf(searchTerm);
                        charRun.replaceText(searchTerm, replacementText,
start);
                    }
                }
            }
            
            // Close the InputStream
            bufIStream.close();
            bufIStream = null;
            
            // Open an OutputStream and write the document away.
            outputFile = new File(outputFilename);
            fileOStream = new FileOutputStream(outputFile);
            bufOStream = new BufferedOutputStream(fileOStream);
            
            document.write(bufOStream);
        
        }
        catch(Exception ex) {
            System.out.println("Caught an: " + ex.getClass().getName());
            System.out.println("Message: " + ex.getMessage());
            System.out.println("Stacktrace follows.............");
            ex.printStackTrace(System.out);
        }
        finally {
            if(bufOStream != null) {
                try {
                    //bufOStream.flush();
                    bufOStream.close();
                    bufOStream = null;
                }
                catch(Exception ex) {
                    
                }
            }
            if(bufIStream != null) {
                try {
                    bufIStream.close();
                    bufIStream = null;
                }
                catch(Exception ex) {
                    // I G N O R E //
                }
            }
        }
        
    }
    
    /**
	 * Search for and replace a single occurrence of a string of text within a
	 * Word document.
	 *
	 * Note that no checks are made on the parameter's values; that is to say
	 * that the file named in the InputFilename parameter will not be checked
	 * to ensure the file exists and neither of the searchTerm nor
	 * replacementTerm pare,eters will be checked to ensure they are not null.
	 * Also, note that I have never tested passing the same String to the
	 * inputFilename and outputFilename parameters but cannot see why that
	 * should not be possible.
	 *
	 * @param inputFilename An instance of the String class that encapsulates
	 *                      the name of and path to a Word document which is
	 *                      in the binary (OLE2CDF) format. The contents of
this
	 *                      document will be searched for occurrences of the
	 *                      search term.
	 * @param outputFilename An instance of the String class that encapsulates
	 *                       the name of and path to a Word document which is
	 *                       in the binary (OLE2CDF) format. This document will
	 *                       contain the results of the search and replace
	 *                       operation.
	 * @param replacements An instance of the java.util.HashMap class that
	 *                     contains a series of key, value pairs. Each key
	 *                     is an instance of the String class that encapsulates
	 *                     a series of characters, a word or words that the
	 *                     code will search for and the accompanying value is
	 *                     also an instance of the String class that likewise
	 *                     encapsulates a series of characters, a word or
words.
	 *                     The 'contents' of the value's String will be used to
	 *                     replace the contents of the key's String if an
	 *                     occurrence of the latter is found.
	 */
    public void searchAndReplace(String inputFilename,
                                 String outputFilename,
                                 HashMap<String, String> replacements) {
        
        File inputFile = null;
        File outputFile = null;
        FileInputStream fileIStream = null;
        FileOutputStream fileOStream = null;
        BufferedInputStream bufIStream = null;
        BufferedOutputStream bufOStream = null;
        POIFSFileSystem fileSystem = null;
        HWPFDocument document = null;
        Range docRange = null;
        Paragraph paragraph = null;
        CharacterRun charRun = null;
        Set<String> keySet = null;
        Iterator<String> keySetIterator = null;
        int numParagraphs = 0;
        int numCharRuns = 0;
        String text = null;
        String key = null;
        String value = null;
        
        try {
            // Create an instance of the POIFSFileSystem class and
            // attach it to the Word document using an InputStream.
            inputFile = new File(inputFilename);
            fileIStream = new FileInputStream(inputFile);
            bufIStream = new BufferedInputStream(fileIStream);
            fileSystem = new POIFSFileSystem(bufIStream);
            document = new HWPFDocument(fileSystem);
			
			// Get a reference to the overall Range for the document
			// and discover how many Paragraphs objects there are
			// in the document.
            docRange = document.getRange();
            numParagraphs = docRange.numParagraphs();
            
            // Recover a Set of the keys in the HashMap
            keySet = replacements.keySet();
            
            // Step through each Paragraph
            for(int i = 0; i < numParagraphs; i++) {
                paragraph = docRange.getParagraph(i);
                // This line can almost certainly be removed - see
                // the comments in the method above.
                text = paragraph.text();
                
                // Get the number of CharacterRuns in the Paragraph
                // and step through each one.
                numCharRuns = paragraph.numCharacterRuns();
                for(int j = 0; j < numCharRuns; j++) {
                    charRun = paragraph.getCharacterRun(j);
                    
                    // Get the text from the CharacterRun and recover an
                    // Iterator to step through the Set of keys.
                    text = charRun.text();
                    keySetIterator = keySet.iterator();
                    while(keySetIterator.hasNext()) {
                    	
                    	// Get the key - which is also the search term - and
                    	// check to see if it can be found within the 
                    	// CharacterRuns text.
                        key = keySetIterator.next();
                        if(text.contains(key)) {
                        	
                        	// If the search term was found in the text, get
the
                        	// matching value from the HashMap, find out
whereabouts
                        	// in the CharacterRuns text the search term is
                        	// and call the replaceText() method to substitute
                        	// the replacement term for the search term.
                            value = replacements.get(key);
                            int start = text.indexOf(key);
                            charRun.replaceText(key, value, start);
                            
                            // Note that this code was added to test whether
                            // it was possible to replace multiple
occurrences
                            // of the search term. I cannot remember if I
tested
                            // it but believe that it did work; either way,
                            // it could be tested now and if succeeds, then
the
                            // searchAndReplace() method above could be
modified
                            // to include this.
                            docRange = document.getRange();
                            paragraph = docRange.getParagraph(i);
                            charRun = paragraph.getCharacterRun(j);
                            text = charRun.text();
                        }
                    }
                }
            }
            
            // Close the InputStream
            bufIStream.close();
            bufIStream = null;
            
            // Open an OutputStream and save the modified document away.
            outputFile = new File(outputFilename);
            fileOStream = new FileOutputStream(outputFile);
            bufOStream = new BufferedOutputStream(fileOStream);
            document.write(bufOStream);
        }
        catch(Exception ex) {
            System.out.println("Caught an: " + ex.getClass().getName());
            System.out.println("Message: " + ex.getMessage());
            System.out.println("Stacktrace follows.............");
            ex.printStackTrace(System.out);
        }
        finally {
            if(bufIStream != null) {
                try {
                    bufIStream.close();
                    bufIStream = null;
                }
                catch(Exception ex) {
                    // I G N O R E //
                }
            }
            if(bufOStream != null) {
                try {
                    bufOStream.flush();
                    bufOStream.close();
                    bufOStream = null;
                }
                catch(Exception ex) {
                    
                }
            }
        }
        
    }

	/**
	 * The main entry point to the program demonstrating how the code may
	 * be utilised.
	 *
	 * @param args An array of type String containing argumnets passed to the
	 *             program on execution.
	 */
	public static void main(String[] args) {
		SearchReplace replacer = new SearchReplace();
		
		// To serach for and replace single items. Note, the code has not, at
		// least as far as I can remember, been tested by passing the same
		// file to both the searchTerm and replacementTerm parameters. It ought
		// to work but has NOT been tested I believe.
		replacer.searchAndReplace("Document.doc",            // Source Document
			"Replaced Document.doc",                         // Result Document
			"search term",                                   // Search term
			"replacement term");                             // Replacement term
			
		// To search for and replace a series of items
		HashMap<String, String> searchTerms = new HashMap<String, String>();
		searchTerms.put("search term 1", "replacement term 1");
		searchTerms.put("search term 2", "replacement term 2");
		searchTerms.put("search term 3", "replacement term 3");
		searchTerms.put("search term 4", "replacement term 4");
		
		replacer.searchAndReplace("Document.doc",    // Source Document
			"Replaced Document.doc",                 // Result Document
			searchTerms)                             // Search/replacement items
	}
}



karthik-33 wrote:
> 
> 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 <ma...@tiscali.co.uk> 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: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
> 
> 
> -- 
> karthik
> 
> 

-- 
View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24876942.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
OpenOffice has a series of filters that allow it to work with documents in a
variety of different formats; the binary version of Word's document format -
.doc - is one of these so you could use the application. My only concern
with OpenOffice is that sometimes the way it renders more complex documents
differs from the way Word would render them and I worry that the filters may
not be fully able to cope with complex documents although I have no evidence
to support that conclusion. The other problem with OpenOffice from your
point of view would be the learning curve associated with UNO - it is quite
steep and the documentation does not help at all IMO.

As you are working on a PC, is it running Windows? If so then you may want
to look at OLE. This technique would allow you to control an instance of
Word itself and to use that to search and replace for you. Recently, I got
together some SWT/OLE code and posted it here;

http://rapidshare.com/files/262148932/Word_-_Search_and_Replace.rar

in response to a request from another list member. It contains the SWT/OLE
code to perform search and replace operations and you might be interested in
compiling and running it. If you want the code, follow that link, choose the
free download option and you will be able to download the rar archive. Unzip
this to a folder on your PC and then look at the readme.rtf file as it
explains how to set up your PC so that you can compile and then run the java
code. If you do run the code, don't be surprised if it seems to run quite
slowly. The code has to load and run Word first then open the file, search
for and replace the terms and then save it all away again - the delay can
seem quite disconcerting the first time. Remember though, it will only run
under Windows and on a PC that has Word installed. Also, this is not
production code, merely test/demonstration code so do not use it on anything
important until you have throughly tested it please.

Finally, I will be looking through some old folders/discs today and will dig
out the HWPF search/replace code I was working on and post it here once I
do.

Yours

Mark B


karthik-33 wrote:
> 
> 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 <ma...@tiscali.co.uk> 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: user-unsubscribe@poi.apache.org
>> For additional commands, e-mail: user-help@poi.apache.org
>>
>>
> 
> 
> -- 
> karthik
> 
> 

-- 
View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24875568.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by karthik <ka...@gmail.com>.
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 <ma...@tiscali.co.uk> 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: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>


-- 
karthik

Re: Replace Text Problem (Document Corrupt) - POI HWPFDocument

Posted by MSB <ma...@tiscali.co.uk>.
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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org