1 2 Previous Next 17 Replies Latest reply: May 18, 2009 11:43 PM by 807588 RSS

    find and replace rtf file

    807588
      hello
      Im trying to make a programe that find and replace text in an rtf file

      i can read the rtf file
      i can put the rtf file in editorpane
      it just im not haveing the same text format or style after replacing

      here is my code ;

      RTFEditorKit rtf = new RTFEditorKit();
      
      editor.setContentType(rtf.getContentType());
      editor.setEditorKit( rtf );
      
      File fi = new File( "test.rtf" );
      File fo = new File( "test10.rtf" );
      
      FileReader reader = null;
      FileWriter writer= null;
      
      try
      {
             reader = new FileReader(fi);
      }
      catch (FileNotFoundException ex)
      {
            ex.printStackTrace();
      }
      
      
      try
      {
            writer =new FileWriter(fo);
      } 
      catch (IOException ex)
      {
            ex.printStackTrace();
      }
      
      Document doc = rtf.createDefaultDocument();
       //Document doc = editor.getDocument();  I tried this but it do nothing
      
      
      try
      {
             rtf.read( reader,doc, 0 );
      }
       catch (BadLocationException ex) 
      {
            ex.printStackTrace();
      }
       catch (IOException ex)
      {
           ex.printStackTrace();
      }
      
      editor.setDocument(doc);
       //  editor.setText(doc.getText(0,doc.getLength())); it is not working 
         
      try
      {
           String text=doc.getText(0,doc.getLength());
           text= text.replaceAll("replace_me","I will replace you");
           writer.write(text);
      }
       catch (BadLocationException ex) 
      {
           ex.printStackTrace();
      }
      catch (IOException ex) 
      {
         ex.printStackTrace()
      }
      try
       {
           writer.close();
           reader.close();
        }
      catch (IOException ex)
      {
           ex.printStackTrace();
      }
      It has been a 2 days trying to solve this problem
      thank you in advance
        • 1. Re: find and replace rtf file
          807588
          I think that instead of :
          String text=doc.getText(0,doc.getLength());
               text= text.replaceAll("replace_me","I will replace you");
               writer.write(text);
          You should use:
          String text=doc.getText(0,doc.getLength());
               text= text.replaceAll("replace_me","I will replace you");
               doc.remove(0,doc.getLength());
               doc.insertString(0,text,new SimpleAttributeSet());
               rtf.write(writer,doc,0);
               
          The way you posted you will always write simple text. because you use the write method of a simple Writer, not the one of RTFEditorKit!
          • 2. Re: find and replace rtf file
            807588
            helloo thank you for your answer
            I tried to use your code ,and here is what i get

            java.io.IOException: RTF is an 8-bit format
                    at javax.swing.text.rtf.RTFEditorKit.write(RTFEditorKit.java:135)
                    at test.NewJFrame.jButton1ActionPerformed(NewJFrame.java:159)
            any idea ??
            • 3. Re: find and replace rtf file
              807588
              I don't know, I' ve never used RTFEditorKit before!
              It probably has to do with the fact that the String may contain 16Bit charachter, but I don't know much about this topic either.
              • 4. Re: find and replace rtf file
                807588
                FileWriter writer= null;
                Try to use a FileOutputStream instead of a FileWriter. Don't know if it will help, though...

                Message was edited by:
                Tonytammaro
                • 5. Re: find and replace rtf file
                  807588
                  ok thank you any way


                  by the way here is the code i added
                  String text=doc.getText(0,doc.getLength());
                  text= text.replaceAll("replace_me","I will replace you");
                  doc.remove(0,doc.getLength());
                  doc.insertString(0,text,new SimpleAttributeSet());
                  rtf.write(writer,doc,0,doc.getLength());
                  and the error point on this line
                  rtf.write(writer,doc,0,doc.getLength());
                  thank you again for your help
                  • 6. Re: find and replace rtf file
                    807588
                    But, does it work if you use the FileOutputStream instead of the FileWriter?
                    • 7. Re: find and replace rtf file
                      807588
                      ok .... no errors now but ...
                      with the FileOutputStream im abel to copy the rtf file keeping the style and everthing

                      but when i try to replace the text ,I lose the style ,like font color and size ,etc..
                      FileOutputStream out =null;
                              try {
                                  out = new FileOutputStream (fo);
                              } catch (FileNotFoundException ex) {
                                  ex.printStackTrace();
                              }
                      
                           //if i commet the next 4 lines I will be abel to copy my file keeping the style
                        
                          String text=doc.getText(0,doc.getLength());
                           text= text.replaceAll("replace_me","I will replace you");
                           doc.remove(0,doc.getLength());
                           doc.insertString(0,text,new SimpleAttributeSet());
                           
                           rtf.write(out,doc,0,doc.getLength());
                      • 8. Re: find and replace rtf file
                        807588
                        Sorry, I give up at this point... You should probably retrieve the AtributeSet char by char and write it back to the output with the correct AttributeSet, but I bet it would become much more complicated...
                        Good luck :-)
                        • 9. Re: find and replace rtf file
                          807588
                          ok thank you any way

                          just one more question

                          my programme is supposed to open a model or template that was created by word ,and then to replace the book mark in the template

                          in way im trying to generate document

                          do you see any other or better solution than the one im doing ??
                          • 10. Re: find and replace rtf file
                            807588
                            any idea ???
                            or any link to help to resolve my pb ??
                            • 11. Re: find and replace rtf file
                              807588
                              hello ,it is me back .
                              I still can't find out how to do it ??
                              any one has an idea ???
                              • 12. Re: find and replace rtf file
                                807588
                                last try ???
                                • 13. Re: find and replace rtf file
                                  807588
                                  I simply don't know, sorry.
                                  I gave my best to be helpful and I think at least some of my advices were good, but I'm not supposed to do the work for you and besides I don't know anything of the topic and never done something similar.
                                  That was the meaning of my last post and I also think this thread is dead, because no one other than me replied.
                                  I think it would be fair to spare me some dukes but this is entirely up to you, so I won't blame you anyway.
                                  Regards,
                                  Riccardo
                                  • 14. Re: find and replace rtf file
                                    807588
                                    I simply don't know, sorry.
                                    lol
                                    you are realy nice ,but I did not mean you with my last post .
                                    I was just trying to keep this thread up

                                    I think it would be fair to spare me some dukes but
                                    this is entirely up to you, so I won't blame you
                                    anyway.
                                    at the beginning i wanted to give 1 duck for the subject ,when i did not find any solution I incresed to 10

                                    ur last post made me laught ,that's why i wil lgive u the 1 duck ,I wanted to give at the beginning

                                    thank you any way .
                                    bye
                                    1 2 Previous Next