Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Mar 09, 2016 7:38 pm

Hello, how can I insert (replace placeholder with) a link in an existing Word document. I want to replace IN the text, not add a new paragraph nor section. I found how to add an hyperlink in a new document or how to append it at the end in a new section or paragraph, but I really need to replace a placeholder (like the document.replace fonctionnality, except that I need to create a link).

Thanks
Robin Leblond
.Net Consultant, Montréal, Qc, Canada

robinleblond
 
Posts: 22
Joined: Fri Jan 29, 2016 5:46 pm

Thu Mar 10, 2016 10:11 am

Hello,

Thanks for your inquiry. You can try to the following method to insert link at specific place.
Code: Select all
Document document = new Document();
            document.LoadFromFile(@"E:\FindAndReplace.doc");

            TextSelection ts = document.FindString("word", false, false);
            TextRange tr = ts.GetAsOneRange();
            int index = tr.OwnerParagraph.ChildObjects.IndexOf(tr);

            Field field = new Field(document);
            field.Code = "HYPERLINK \"http://www.e-iceblue.com\"";
            field.Type = FieldType.FieldHyperlink;
           
            tr.OwnerParagraph.ChildObjects.Insert(index, field);

            FieldMark fm = new FieldMark(document, FieldMarkType.FieldSeparator);
            tr.OwnerParagraph.ChildObjects.Insert(index + 1, fm);
           
            TextRange textR = new TextRange(document);
            textR.Text = "test";
            textR.CharacterFormat.TextColor = Color.Blue;
            textR.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;
            tr.OwnerParagraph.ChildObjects.Insert(index + 2, textR);

            FieldMark fmend = new FieldMark(document, FieldMarkType.FieldEnd);
            tr.OwnerParagraph.ChildObjects.Insert(index + 3, fmend);

            field.End = fmend;
            tr.OwnerParagraph.ChildObjects.RemoveAt(index + 4);
       
            document.SaveToFile("Sample.doc", FileFormat.Doc);

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Tue Mar 15, 2016 7:01 am

Hello,

Did the method resolve your issue? If there is any question, welcome to get it back to us.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Fri Mar 18, 2016 1:22 pm

YES!!! It did! Thanks a lots !

(Sorry for the delay to answer/tell you, I was working on something else and I didn't had the time to test it).
Robin Leblond
.Net Consultant, Montréal, Qc, Canada

robinleblond
 
Posts: 22
Joined: Fri Jan 29, 2016 5:46 pm

Mon Mar 21, 2016 1:50 am

Hello,

Thank you for taking time from busy to respond. I am glad to help you.

Have a nice day.

Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Tue Apr 04, 2017 8:45 pm

When I try to use this code, I'm getting "'FieldMark' does not contain a constructor that takes 2 arguments".

Am I doing something stupid (I'm new to Visual Studio, C# and .NET) or has the FieldMark API changed? It looks like the only constructor is a protected class. Is there a subclass I should be using?

owurman
 
Posts: 20
Joined: Tue Apr 04, 2017 8:43 pm

Wed Apr 05, 2017 2:41 am

Dear owurman,

Thanks for your inquiry.
I checked the code on my side using the latest Spire.Doc Pack(hot fix) Version:5.8.76, but didn't find the issue you mentioned.
Could you please tell us which version you were using(e.g. Spire.Doc.dll V5.8.76.4040) ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Apr 05, 2017 4:30 am

I had started with a demo app (I can't even remember where I got it) that was using a much older version of Spire bundled with it. Installing Spire properly fixed the problem, thanks.

owurman
 
Posts: 20
Joined: Tue Apr 04, 2017 8:43 pm

Wed Apr 05, 2017 5:43 am

Dear owurman,

Thanks for your feedback.
Any question, please do not hesitate to contact us. We will be happy to help you :) .

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Dec 20, 2018 6:13 am

Currently working with the proposed resolution on the latest (as of today) Spire.Doc Free edition and that solution doesn't appear to work properly for me.

Instead of the token being a link it is just blue and underlined text and not a hyperlink for me. Is there a better way of doing this now or is something off in my understanding?

Thanks.

dgellman
 
Posts: 2
Joined: Sun Dec 16, 2018 12:16 am

Thu Dec 20, 2018 7:05 am

Hi dgellman,

Thanks for your inquiry.
Please provide us with your input document and the code you were using for further investigation.

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Dec 25, 2018 6:18 am

Hi dgellman,

Hope you are doing well.
Has your issue been resolved? If not, please provide us with your input document and the code you were using.

Thank,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc