Use -HTML Switch in Fields & Build Reports using C# LINQ .NET JSON | Format Hyperlinks | Save Report to PDF

Hyperlink appears as plain text in Aspose LINQ Reporting. Have attached an example where the hyperlink is added as a static text in the document which appears as clickable and in blue and also inserting content dynamically with html switch <<[Content] -html>> which appears as plain text(no formating)

Expectation is to have the formating as the static hyper link

TestAsposeSample.zip (42.8 KB)

Attached is the solution which has the sample word document and sample Json.

@ravikkashyap,

I have modified your ‘Location.json’ file - (see Location-modified.zip (342 Bytes)), and when you run your C# project against this modified JSON, you will get the desired output. I have also attached the output document here for your reference:

Hi,

Thanks you for your response. Is there any other way to achieve this? The JSON file is also generated dynamically ( It is not static).

Is there any config or setting within the word document?

@ravikkashyap,

Alternatively, you can try the following C# code. In this case, you will not have to do any changes in JSON file.

static void Main(string[] args)
{
    var templateId = "Location_Layout1.docx";
    var templateFile = ReadResourceData(templateId);
    string templateData = ReadResourceJson("Location.json");

    // Generate JSON data source from the request
    MemoryStream dataStream = new MemoryStream(Encoding.ASCII.GetBytes(templateData));
    JsonDataSource jsonDataSource = new JsonDataSource(dataStream);

    // Generate Document
    using (var templateDocumentStream = new MemoryStream(templateFile))
    {
        Document asposeDocument = new Document(templateDocumentStream);

        // Mark existing hyperlink fields
        foreach (Field field in asposeDocument.Range.Fields)
        {
            if (field.Type == FieldType.FieldHyperlink)
            {
                FieldHyperlink link = (FieldHyperlink)field;
                link.ScreenTip = link.ScreenTip + "removeMe";
            }
        }

        ReportingEngine engine = new ReportingEngine();
        engine.KnownTypes.Add(typeof(ControlChar));
        engine.BuildReport(asposeDocument, jsonDataSource);

        // Apply formatting to newly added hyperlink fields only
        foreach (Field field in asposeDocument.Range.Fields)
        {
            if (field.Type == FieldType.FieldHyperlink)
            {
                FieldHyperlink link = (FieldHyperlink)field;
                if (link.ScreenTip == null)
                    FormatHyperlink(field);
                else if (link.ScreenTip.Contains("removeMe"))
                    link.ScreenTip.Replace("removeMe", "");
            }
        }

        asposeDocument.Save("E:\\Temp\\TestAsposeSample\\20.6.docx", SaveFormat.Docx);
    }
}

public static void FormatHyperlink(Field field)
{
    Node currentNode = field.Start;
    while (currentNode != null)
    {
        if (currentNode == field.End)
            break;

        if (currentNode.NodeType.Equals(NodeType.Run))
        {
            Run run = (Run)currentNode;
            run.Font.StyleIdentifier = StyleIdentifier.Hyperlink;
            run.Font.Name = "Arial";
            run.Font.Size = 14;
        }

        Node nextNode = currentNode.NextPreOrder(currentNode.Document);
        currentNode = nextNode;
    }
}

Thanks. I will try this.

I am assuming this will work for output as PDF?

@ravikkashyap,

Yes, when you save it to PDF, the same code will produce the expected output. I have attached the output PDF file here for your reference: