We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Word Merge Field in Hyperlink using C# and Convert to PDF

Hi,

I need to generate a PDF from a Microsoft Word document such that the PDF displays a URL Hyperlink whose link value depends on a data context.

I think the best course of action is to insert word merge field in hyperlink in the Word document. I have followed various sources of information to do so:

  1. Inserting a hyperlink to a mail merge
  2. Add Variable Hyperlink in Mail Merge in Word 2013
  3. https://www.youtube.com/watch?v=iELd7AEsp_E

But the generated PDF does not have any Hyperlink. Instead the message Error! Hyperlink reference not valid is displayed.

You will find as attachments:

  1. The Microsoft Word document used as a source for the generation (“message iwe.docx”)
  2. The generated PDF (“message iwe.docx.pdf”)
  3. The payload to Aspose to generate the document (“aspose_payload.json”)

A few questions:

Q1. Is there another way to generate a PDF from a Microsoft Word document such that the PDF displays a URL Hyperlink whose link value depends on the context, rather than to use a mail merge field within a hyperlink field?
Q2. If not, can you explain how to proceed?
Q3. The Word document also has a URL at the bottom,example.zip (100.7 KB)
which is statically valued (to https://iwecloud.com/). But the PDF does not have the Hyperlink. Why?

Thanks in advance for your help.

@qpergeline,

The following C# code of Aspose.Words for .NET API will first insert a Hyperlink containing a nested Merge Field in Word document, then populates the mail merge field with value and finally saves to PDF format:

C# Code to Insert Word Merge Field in Hyperlink

public static void Word_Merge_Field_In_Hyperlink()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert a Hyperlink with nested Merge Field
    FieldHyperlink hyperlink = (FieldHyperlink)builder.InsertField("HYPERLINK \"", null);
    builder.MoveTo(hyperlink.Start.NextSibling.NextSibling);

    // Insert merge field
    FieldMergeField mergeField = (FieldMergeField)builder.InsertField(FieldType.FieldMergeField, false);
    mergeField.FieldName = "NOTIF_CASE_URL";

    builder.Write("\"");

    // Set the display text
    hyperlink.Result = "Aspsoe";

    // Start Mail Merge Execution
    doc.MailMerge.Execute(new string[] { "NOTIF_CASE_URL" }, new object[] { "https://www.aspose.com/" });

    // Convert Word to PDF
    doc.Save("C:\\Temp\\word to .pdf");
}

Alternatively, you can also make use of Bookmarks inside the Hyperlink field.

public static void Insert_Bookmark_In_Hyperlink()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert a Hyperlink with bookmark
    FieldHyperlink fieldHyperlink = (FieldHyperlink)builder.InsertField("HYPERLINK \"", null);
    builder.MoveTo(fieldHyperlink.Start.NextSibling.NextSibling);

    // Insert Bookmark
    BookmarkStart start = builder.StartBookmark("NOTIF_CASE_URL");
    builder.EndBookmark("NOTIF_CASE_URL");

    builder.Write("\"");

    // Set the display text values
    fieldHyperlink.Result = "Aspsoe";

    // Set Bookmark text
    doc.Range.Bookmarks["NOTIF_CASE_URL"].Text = "https://www.aspose.com/";
    doc.Save("C:\\Temp\\word to .pdf");
}

Also, please check this template - link with merge field.docx (press ALT+F9 in MS Word to see field codes) and try running the following C# code:

Document doc = new Document("C:\\Temp\\template - link with merge field.docx");
doc.MailMerge.Execute(new string[] { "NOTIF_CASE_URL" }, new object[] { "https://www.aspose.com/" });
doc.Save("C:\\Temp\\21.9.pdf");

Hope, this helps in achieving what you are looking for.

@qpergeline,

After an initial test with the licensed latest (21.9) version of Aspose.Words for .NET, we were unable to reproduce this problem on our end. I have attached the PDF file here for your reference.

We used the following simple C# code to produce above PDF:

Document doc = new Document("C:\\235154\\message iwe.docx");
doc.UpdateFields();
doc.Save("C:\\235154\\awnet-21.9.pdf");

We suggest you to please upgrade to the latest version of Aspose.Words for .NET and see how it goes on your end? In case the problem still remains, then can you please also provide source code which will help us to reproduce this issue on our end?