Articles in this section
Category / Section

How to replace URL of image hyperlink in Word document

5 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can replace the URL of an image hyperlink in the Word document.

Steps to replace the URL of an image hyperlink in the Word document programmatically:

  1. Create a new C# console application project.

CreateWordDocument

  1. Install the Syncfusion.DocIO.Winforms NuGet package as a reference to your .NET Framework applications from NuGet.org.
    NugetPackage
  2. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

 

VB.NET

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS

 

  1. Use the following code example to replace the URL of an image hyperlink in the Word document.
    //Open the template document.
    using (WordDocument wordDocument = new WordDocument("Data\\Template_with_link.docx", FormatType.Docx))
    {
        //Process the body contents for each section in the Word document.
        foreach (WSection section in wordDocument.Sections)
        {
           //Access the Body of the section where all the contents of the document are apart.
           WTextBody sectionBody = section.Body;
           IterateTextBody(sectionBody);
        }
        //Save the word document.
        wordDocument.Save("Template.docx");
    }
    

 

 

VB.NET

'Open the template document.
Using wordDocument As WordDocument = New WordDocument("Data\Template_with_link.docx", FormatType.Docx)
    'Process the body contents for each section in the Word document.
    For Each section As WSection In wordDocument.Sections
        'Access the Body of the section where all the contents of the document are apart.
        Dim sectionBody As WTextBody = section.Body
        IterateTextBody(sectionBody)
    Next
    'Save the word document.
    wordDocument.Save("Template.docx")
End Using

 

  1. Helper method.

C#

private static void IterateTextBody(WTextBody textBody)
{
    //Iterate through each of the child items of WTextBody.
    for (int i = 0; i < textBody.ChildEntities.Count; i++)
    {
        //IEntity is the basic unit in the DocIO DOM. 
        //Access the body items (should be either paragraph, table or block content control) as IEntity.
        IEntity bodyItemEntity = textBody.ChildEntities[i];
        //A Text body has 3 types of elements - Paragraph, Table and Block Content Control.
        //Decide the element type by using the EntityType.
        switch (bodyItemEntity.EntityType)
        {
            case EntityType.Paragraph:
                WParagraph paragraph = bodyItemEntity as WParagraph;
                //Process the paragraph contents.
                //Iterate through the paragraph's DOM.
                IterateParagraph(paragraph.Items);
                break;
            case EntityType.Table:
                //Table is a collection of rows and cells.
                //Iterate through table's DOM.
                IterateTable(bodyItemEntity as WTable);
                break;
            case EntityType.BlockContentControl:
                BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
                //Iterate to the body items of Block Content Control.
                IterateTextBody(blockContentControl.TextBody);
                break;
        }
    }
}
private static void IterateTable(WTable table)
{
    //Iterate the row collection in a table.
    foreach (WTableRow row in table.Rows)
    {
        //Iterate the cell collection in a table row.
        foreach (WTableCell cell in row.Cells)
        {
           //Table cell is derived from (also a) TextBody.
           //Reuse the code meant for iterating the TextBody.
           IterateTextBody(cell);
        }
    }
}
private static void IterateParagraph(ParagraphItemCollection paraItems)
{
    for (int i = 0; i < paraItems.Count; i++)
    {
        Entity entity = paraItems[i];
        //A paragraph can have child elements such as text, images, hyperlinks, symbols, etc.,
        //Decide the element type by using the EntityType.
        if (entity.EntityType == EntityType.Field)
        {
            WField field = entity as WField;
            if (field.FieldType == FieldType.FieldHyperlink)
            {
                //Get the hyperlink field.
                Hyperlink link = new Hyperlink(field);
                //Check whether it is an image with a hyperlink.
                if (link.PictureToDisplay != null && link.PictureToDisplay is WPicture)
                {
                    WPicture picture = link.PictureToDisplay as WPicture;
                    //Checks whether in which picture the hyperlink needs to be changed.
                    if (link.Type == HyperlinkType.WebLink && picture.Name == "Obraz 1")
                    {
                        //Modify the url of the hyperlink.
                        link.Uri = "http://www.google.com";
                    }
                }
            }
        }
    }
}

 

VB.NET

Private Shared Sub IterateTextBody(ByVal textBody As WTextBody)
    'Iterate through each of the child items of WTextBody
    Dim i As Integer = 0
    Do While (i < textBody.ChildEntities.Count)
        'IEntity is the basic unit in the DocIO DOM. 
        'Access the body items (should be either paragraph, table or block content control) as IEntity
        Dim bodyItemEntity As IEntity = textBody.ChildEntities(i)
        'A Text body has 3 types of elements - Paragraph, Table and Block Content Control
        'Decide the element type by using the EntityType
        Select Case (bodyItemEntity.EntityType)
            Case EntityType.Paragraph
                Dim paragraph As WParagraph = CType(bodyItemEntity, WParagraph)
                'Process the paragraph contents
                'Iterate through the paragraph's DOM
                IterateParagraph(paragraph.Items)
            Case EntityType.Table
                'Table is a collection of rows and cells
                'Iterate through the table's DOM
                IterateTable(CType(bodyItemEntity, WTable))
            Case EntityType.BlockContentControl
                Dim blockContentControl As BlockContentControl = CType(bodyItemEntity, BlockContentControl)
                'Iterate to the body items of the Block Content Control.
                IterateTextBody(blockContentControl.TextBody)
        End Select
 
        i = (i + 1)
    Loop
 
End Sub
 
Private Shared Sub IterateTable(ByVal table As WTable)
    'Iterate the row collection in a table
    For Each row As WTableRow In table.Rows
        'Iterate the cell collection in a table row
        For Each cell As WTableCell In row.Cells
            'Table cell is derived from (also a) TextBody
            'Reuse the code meant for iterating the TextBody
            IterateTextBody(cell)
        Next
    Next
End Sub
 
Private Shared Sub IterateParagraph(ByVal paraItems As ParagraphItemCollection)
    Dim i As Integer = 0
    Do While (i < paraItems.Count)
        Dim entity As Entity = paraItems(i)
        'A paragraph can have child elements such as text, images, hyperlinks, symbols, etc.,
        'Decide the element type by using the EntityType
        If (entity.EntityType = EntityType.Field) Then
            Dim field As WField = CType(entity, WField)
            If (field.FieldType = FieldType.FieldHyperlink) Then
                'Get the hyperlink field.
                Dim link As Hyperlink = New Hyperlink(field)
                'Check whether it is an image with a hyperlink.
                If ((link.PictureToDisplay IsNot Nothing) AndAlso TypeOf link.PictureToDisplay Is WPicture) Then
                    Dim picture As WPicture = CType(link.PictureToDisplay, WPicture)
                    'Check whether in which picture the hyperlink needs to be changed.
                    If ((link.Type = HyperlinkType.WebLink) AndAlso (picture.Name = "Obraz 1")) Then
                        'Modify the url of the hyperlink.
                        link.Uri = "http://www.google.com"
                    End If
 
                End If
 
            End If
 
        End If
 
        i = (i + 1)
    Loop
 
End Sub

 

A complete working sample to replace the URL of an image hyperlink in the Word document using C# can be downloaded from Sample.zip.

Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.

 

 

 

Conclusion

I hope you enjoyed learning about how to replace URL of image hyperlink in WinForms Word document.

You can refer to our WinForms word feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our 
WinForms word example
 to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied