Using a fieldcode as a hyperlink's displaytext (result)

What I want to achieve is this:

hyperlink.Address = url;
hyperlink.ScreenTip = tooltip;
hyperlink.Result = $"{{ DOCPROPERTY {fieldcode} }}";
hyperlink.Update();

Basically i want the text of the hyperlink to be a docproperty variable.

Is this possible? (It is possible to do this in word, but can it be done with Aspose?)

@jjdoe You can achieve this using code like this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
doc.BuiltInDocumentProperties.Title = "This is a cool document created by Aspose.Words";

// Insert hyperlink with empty displayed text
Field hyperlink = builder.InsertHyperlink("", "https://www.aspose.com", false);
// Move document builder inside the hyperlink displayed value (between field separator and field end)
// Put all nodes from the target paragraph into the hyperlink value.
builder.MoveTo(hyperlink.End);
// Insert a field
builder.InsertField("DOCPROPERTY Title");

//doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

out.docx (7.2 KB)

Thanks for the quick response.

This almost works, but the links appear unformatted. (Black text, no underline).

Any way to fix this?

EDIT:
This seems to work, not sure why it was needed, but did the trick:

builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
builder.InsertField($"DOCPROPERTY {fieldcode}");
builder.Font.ClearFormatting();

Thanks again.

@jjdoe Yes, you are absolutely right, it is required to set hyperlink style. But I would set hyperlink style before inserting the hyperlink field

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
doc.BuiltInDocumentProperties.Title = "This is a cool document created by Aspose.Words";

// Insert hyperlink with empty displayed text
builder.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
Field hyperlink = builder.InsertHyperlink("", "https://www.aspose.com", false);
// Move document builder inside the hyperlink displayed value (between field separator and field end)
// Put all nodes from the target paragraph into the hyperlink value.
builder.MoveTo(hyperlink.End);
// Insert a field
builder.InsertField("DOCPROPERTY Title");
builder.Font.ClearFormatting();

//doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");