Hi,

here is what i did with poi library to implement find and search in a MS WORD DOC : the replace is working but the format of modified document is altered : many newlines and pages are inserted in result document.

the steps i did :
1. get the cvs code of scratchpad related to hwfp package.
2. add following method to class Range :

   code:
   ------------------------------------------------------------------------

     /**
      * Replaces the text of this range with
      * some other text
      * @param text
      */
     public void replaceWith( String text){
     initAll();
TextPiece tp = (TextPiece)_text.get(_textStart);
       StringBuffer sb = (StringBuffer)tp.getStringBuffer();
       int start = _start > tp.getStart() ? _start - tp.getStart() : 0;
       int end = _end <= tp.getEnd() ? _end - tp.getStart() : tp.getEnd() -
   tp.getStart();
       if(tp.usesUnicode()) // convert the byte pointers to char pointers
       {
         start/=2;
         end/=2;
       }
       String oldText= sb.substring(start, end);
int length = 0;
       if ( text.length() > oldText.length() ){
       length = text.length() - oldText.length();
       sb.replace(start, end, text);
               int adjustedLength =
   _doc.getTextTable().adjustForInsert(_textStart, length);
               _doc.getCharacterTable().adjustForInsert(_charEnd - 1,
   adjustedLength);
               _doc.getParagraphTable().adjustForInsert(_parEnd - 1,
   adjustedLength);
               _doc.getSectionTable().adjustForInsert(_sectionEnd - 1,
   adjustedLength);
adjustForInsert(length);
       }else {
       StringBuffer newText = new StringBuffer(text);
       length = oldText.length()- text.length();
       for ( int i = 0; i < length; ++i ){
       newText.append(" ");
       }
       sb.replace(start, end, newText.toString());
       }
     }
   ------------------------------------------------------------------------



3. here is the method i use to do find and replace :

   code:
   ------------------------------------------------------------------------

     HWPFDocument doc;
       public String replaceWord(String file, String searchThis, String 
replaceWith) {
        StringBuffer sb=new StringBuffer();
         try
            {
                 FileInputStream        istream= new FileInputStream(file);
               doc = new HWPFDocument(istream);
              Range r = doc.getRange();
// WordExtractor extractor = new WordExtractor(doc);
            String newText=regexReplace(r.text(),searchThis,  replaceWith);
            r.replaceWith(newText);
          // doc.verifyAndBuildPOIFS(istream);
         //  File f= new File("c:\\temp.doc");
        // f.delete();
         FileOutputStream docOut =
                  new FileOutputStream
                                ("c:\\temp.doc");
        
        
                doc.write(docOut);
                docOut.close();
    }
            catch (Throwable t)
            {
              t.printStackTrace();
            }
        return sb.toString();
       }
   private String  regexReplace( String text, String searchThis,String 
replaceWith){
                
                
                Pattern p = Pattern.compile("(?i)"+searchThis);
           Matcher m = p.matcher(text);
           StringBuffer sb = new StringBuffer();
           while (m.find()) {
               m.appendReplacement(sb,  replaceWith);
           }
         //  m.appendTail(sb);
           return (sb.toString());
   }
   ------------------------------------------------------------------------



as i said the result document seems to write each paragraph (in original doc) into a whole page ...
can some one help me fix the issue ?
many thanks.

Reply via email to