Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Fri May 06, 2011 10:08 am

Hi,
I want to replace some text (a keyword) with a new Bookmark (with some other text).
So I tried...

Code: Select all
         //text is the keyword to "replace" and is located everywhere in my document
        TextRange textRange = text.GetAsOneRange();
        Paragraph paragraph = textRange.OwnerParagraph;

       int index = paragraph.ChildObjects.IndexOf(textRange);
       if (index != -1){
                  //
                 //is this the correct way ????? :(
                 //
                  paragraph.AppendBookmarkStart(bookName);
                  paragraph.AppendText(sVal);
                  paragraph.AppendBookmarkEnd(bookName);

                  paragraph.ChildObjects.RemoveAt(index);
                  paragraph.ChildObjects.Insert(index, paragraph);
        }
               

The keyword text is replaced with the sVal text but I lose the format!!.
How can I do this?
THANKS

michelap
 
Posts: 6
Joined: Mon Jan 17, 2011 7:44 am

Tue May 10, 2011 3:32 am

Dear michelap,
Thans for your query!

Maybe you can try this
Code: Select all
paragraph.Replace(string matchString, string newValue, bool caseSensitive, bool wholeWord);

If you still have some question.Ask us again,please!Attch your code and the file will be good!
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Tue May 10, 2011 12:26 pm

Thanks but in this way I replace "only" the text.
My goal is to replace some text with the new textand create a new bookmark with it.
The bookmark is very important so I'll locate it and replace its content again.

es ->before
bla bla bla Keyword bla bla bla
after
bla bla bla <BOOKMARKSTART> text replaced <BOOKMARKEND> bla bla bla

1) I'm not sure if my code to insert the bookmark is correct. (I have some doubt here: paragraph.ChildObjects.Insert(index, paragraph);)
2) My code seems to work well but I lose the original format .

Thanks

michelap
 
Posts: 6
Joined: Mon Jan 17, 2011 7:44 am

Wed May 11, 2011 3:17 am

Dear michelap,
Thanks for your inqury.
you can use this:
Code: Select all
Document document = new Document("test.doc");
            TextSelection[] textSelections = document.FindAllString("e-iceblue", true, true);
            foreach (TextSelection textSelection in textSelections)
            {
                TextRange range = textSelection.GetAsOneRange();
                Paragraph p = range.OwnerParagraph;
                int index = p.Items.IndexOf(range);
                if (-1 != index)
                {
                    BookmarkStart start = p.AppendBookmarkStart("test");
                    TextRange text = p.AppendText("testbookmark");
                    BookmarkEnd end = p.AppendBookmarkEnd("test");                   
                    p.Items.RemoveAt(index);//p.ChildObjects.RemoveAt(index);
                    p.Items.Insert(index, start);
                    p.Items.Insert(index, text);
                    p.Items.Insert(index, end);
                }

            }

            document.SaveToFile("result.doc");
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Wed May 11, 2011 2:20 pm

Thank you! Your code works fine!!
but....
when I create the bookmark I lose the original text format
I made a little example
Code: Select all
...
Hashtable htCustom = new Hashtable();
      htCustom.Add("D_N1", "text for D_N1");
      htCustom.Add("D_N2", "text for D_N2");
      htCustom.Add("D_N3", "text for D_N3");
      htCustom.Add("D_N4", "text for D_N4");
      htCustom.Add("D_N5", "text for D_N5");

      Document _doc = new Document();
      _doc.LoadFromFile(@"C:\TEMP\DEMO.docx", FileFormat.Docx);

      //-- 1 -- Cerco tutti i TOKEN (stringhe che cominciano per D_ )
      Regex reg = new Regex(@"\bD_[A-Za-z0-9]+");

      TextSelection[] selections = _doc.FindAllPattern(reg);

      string sToken, bookName;

      // --4 -- REPLACE DEL TESTO e Inserimento dei Bookmark
      foreach (TextSelection text in selections)
      {
        TextRange textRange = text.GetAsOneRange();
        bookName = textRange.Text;

        if (!htCustom.ContainsKey(bookName))
          continue;
        try
        {
          Paragraph p = textRange.OwnerParagraph;
          int index = p.Items.IndexOf(textRange);
          if (-1 != index)
          {
            BookmarkStart start = p.AppendBookmarkStart(bookName);
            TextRange _tr1 = p.AppendText(htCustom[bookName].ToString());
            BookmarkEnd end = p.AppendBookmarkEnd(bookName);
            //p.Items.RemoveAt(index);//p.ChildObjects.RemoveAt(index);
            p.Items.Insert(index, start);
            p.Items.Insert(index, _tr1);
            p.Items.Insert(index, end);
          }
        }
        catch (Exception ex)
        {
          MessageBox.Show("Compile DOC. error bookmark: " + bookName + " Error: " + ex.Message);
        }
      }

      _doc.SaveToFile(@"C:\TEMP\RESULT.docx", FileFormat.Docx);

- as you can see in Result.docx the style of "text for D_N1" is different

Any suggestions?
Thank's
Michela
You do not have the required permissions to view the files attached to this post.

michelap
 
Posts: 6
Joined: Mon Jan 17, 2011 7:44 am

Thu May 12, 2011 2:00 am

Thanks for your inqury,
I am sorry for that I don't notice that you ask me the question about formatting.
From your code you can do like this:
Code: Select all
.......
TextRange _tr1 = p.AppendText(htCustom[bookName].ToString());
_tr1.ApplyCharacterFormat(textRange.CharacterFormat);

The format didn't lose .And I tested it, it worked!
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Thu May 12, 2011 7:46 am

Thanks for your support.
I'm sorry but after insert a bookmark my object model of p.Items is like this

Code: Select all
p.Items.Count = 4
[0]   {Spire.Doc.Fields.TextRange}   object {Spire.Doc.Fields.TextRange}  //text before bookmark
[1]   {Spire.Doc.BookmarkEnd}   object {Spire.Doc.BookmarkEnd}       //??????
[2]   {Spire.Doc.Fields.TextRange}   object {Spire.Doc.Fields.TextRange}
[3]   {Spire.Doc.BookmarkStart}   object {Spire.Doc.BookmarkStart}  //??????


so I tried to increment index like this and seems to works fine

Code: Select all
  BookmarkStart start = p.AppendBookmarkStart(bookName);
            TextRange _tr1 = p.AppendText(htCustom[bookName].ToString());
            _tr1.ApplyCharacterFormat(textRange.CharacterFormat);

            BookmarkEnd end = p.AppendBookmarkEnd(bookName);
            p.Items.RemoveAt(index);
            p.Items.Insert(index, start);
            p.Items.Insert(++index, _tr1);
            p.Items.Insert(++index, end);

Regards
Michela

michelap
 
Posts: 6
Joined: Mon Jan 17, 2011 7:44 am

Thu May 12, 2011 7:55 am

It's true michelap! The index must be added!
Thanks for your inqury!
If you have some questions again,ask us , please!
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Return to Spire.Doc