Formatting not applied to hyperlink
Hello,
I am using the following code to apply specific formatting to a hyperlink in a paragraph but the formatting is not being applied. Can you confirm or provide an alternate way to set hyperlink formatting?
var format = new CharacterFormat(document);
format.FontSize = 8;
format.FontName = "Arial";
paragraph.AppendHyperlink("www.exampleLink.com", "linkText", HyperlinkType.WebLink).ApplyCharacterFormat(format);
The same thing occurs when I use the appendField command.
For example, I have defined one CharacterFormat called pageNumberFormat and applied it like this:
paragraph.AppendField("page number", FieldType.FieldPage).ApplyCharacterFormat(pageNumberFormat);
paragraph.AppendText(" of ").ApplyCharacterFormat(pageNumberFormat);
paragraph.AppendField("number of pages", FieldType.FieldNumPages).ApplyCharacterFormat(pageNumberFormat);
but the desired formatting is only being applied to "of" and not to the numbers.
Thanks.
Hi Natalie,
From the given details, we have found that your requirement is to apply
character format to a hyperlink and field. According to the Syncfusion DocIO
library, the hyperlink is essentially a field thus, both the hyperlink and
field have the same structure in the DOM. The structure of the field is as
follows:
1. Field
2. Field Code (a text range)
3. Separator
4. Field Result (a text range)
5. Field End
When applying the character format to the AppendHyperlink or AppendField
method, it only applies the character format to the first value in the
structure. The text ranges are only used to display in the UI. Therefore, we
suggest applying the character format to the text ranges.
You can refer to the below documentation for formatting fields:
https://help.syncfusion.com/file-formats/docio/working-with-fields#formatting-fields
You can also find the modified code snippet below:
1. For the hyperlink, we have a basic character format, which includes blue
text color and underline, when appending a hyperlink. If you use the
ApplyCharacterFormat method to change the entire character format, the default
formatting will be lost. Therefore, we suggest changing the values of the
existing character format of those text ranges.
|
WordDocument document = new WordDocument(); document.EnsureMinimal(); WParagraph paragraph = document.LastParagraph; var format = new WCharacterFormat(document); IWField field = paragraph.AppendHyperlink("www.exampleLink.com", "linkText", HyperlinkType.WebLink); IEntity entity = field; //Iterates to sibling items until Field End while (entity.NextSibling != null) { if (entity is WTextRange) { //Apply character format for text ranges (entity as WTextRange).CharacterFormat.FontSize = 8; (entity as WTextRange).CharacterFormat.FontName = "Arial"; } else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd) break; //Gets next sibling item. entity = entity.NextSibling; } |
2. For the field, we suggest updating the fields to show the field result in
the UI.
|
WordDocument document = new WordDocument(); document.EnsureMinimal(); WParagraph paragraph = document.LastParagraph; var format = new WCharacterFormat(document); format.FontSize = 8; format.FontName = "Arial"; IWField field = paragraph.AppendField("page number", FieldType.FieldNumPages); IEntity entity = field; //Iterates to sibling items until Field End while (entity.NextSibling != null) { if (entity is WTextRange) { //Apply character format for text ranges (entity as WTextRange).ApplyCharacterFormat(format); } else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd) break; //Gets next sibling item. entity = entity.NextSibling; } document.UpdateDocumentFields(); |
Regards,
Sneha.
- 1 Reply
- 2 Participants
- Marked answer
-
NW Natalie Westphal
- Jan 11, 2024 06:45 PM UTC
- Jan 12, 2024 08:06 AM UTC