Change style (i.e. fontname and fontsize) of a hyperlink

Hallo,

Is there a way to change the style (i.e. fontname and fontsize) of a hyperlink that is located in a table?

I tried to change it using a textrange but it does not change anaything:

        textRange = table(0, 1).AddParagraph().AppendHyperlink("https://xxxx.com/", "xxxxx", HyperlinkType.WebLink)
        textRange.ApplyCharacterFormat(textFormatNormal) 


3 Replies

MR Manikandan Ravichandran Syncfusion Team May 29, 2020 02:25 PM UTC

Hi Vince,

Thank you for contacting Syncfusion support.

From the given details, we have found that your requirement is to change the background color of the for the Hyperlink. For this requirement, we suggest you to use the following code example

 
WordDocument document = new WordDocument();
document.EnsureMinimal();
IWTable table = document.LastSection.AddTable();
table.ResetCells(2,2);
IWField hyperlinkField = table[0,1].AddParagraph().AppendHyperlink(
"https://xxxx.com/", "xxxxx", HyperlinkType.WebLink);
ChangeHyperlinkSytle(hyperlinkField);
document.Save(
"output.docx");
document.Close();
Process.Start(
"output.docx"); 
/// <summary>
///
Change the background for hyperlink field.
/// </summary>
///
<param name="hyperlinkField"></param>
private void ChangeHyperlinkSytle(IWField hyperlinkField)
{
    WParagraph paragraph = hyperlinkField.OwnerParagraph;
   
foreach (ParagraphItem item in paragraph.ChildEntities)
    {
       
if (!(item is IWField) && (item is WTextRange))
        {
             (item
as WTextRange).CharacterFormat.FontName = "Arial";
             (item as WTextRange).CharacterFormat.FontSize = 20;
         }
     }
}
 

If you think, we misunderstood your requirement, kindly share the complete requirement or runnable sample and also expected output document/ screenshot, so that we can proceed further to analyse and update you with the appropriate details

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran 



MR Manikandan Ravichandran Syncfusion Team May 29, 2020 02:36 PM UTC

Hi Vince,

Please ignore the previous update

From the given details, we have found that your requirement is to change the font size and font name of the for the Hyperlink. For this requirement, we suggest you to use the following code example

 
WordDocument document = new WordDocument();
document.EnsureMinimal();
IWTable table = document.LastSection.AddTable();
table.ResetCells(2,2);
IWField hyperlinkField = table[0,1].AddParagraph().AppendHyperlink(
"https://xxxx.com/", "xxxxx", HyperlinkType.WebLink);
ChangeHyperlinkSytle(hyperlinkField);
document.Save(
"output.docx");
document.Close();
Process.Start(
"output.docx"); 
/// <summary>
///
Change the background for hyperlink field.
/// </summary>
///
<param name="hyperlinkField"></param>
private void ChangeHyperlinkSytle(IWField hyperlinkField)
{
    WParagraph paragraph = hyperlinkField.OwnerParagraph;
   
foreach (ParagraphItem item in paragraph.ChildEntities)
    {
       
if (!(item is IWField) && (item is WTextRange))
        {
             (item
as WTextRange).CharacterFormat.FontName = "Arial";
             (item as WTextRange).CharacterFormat.FontSize = 20;
         }
     }
}
 
 

If you think, we misunderstood your requirement, kindly share the complete requirement or runnable sample and also expected output document/ screenshot, so that we can proceed further to analyse and update you with the appropriate details

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran 



VI Vince May 30, 2020 10:17 AM UTC

Hallo Manikandan,

Thanks for your reply.

Your code sample does the job. Thanks!

Regards,

Vince

Loader.
Up arrow icon