Create an Image Hyperlink in Word in C#, VB.NET

With linked images, you can direct users to a URL when they click the image. This article will show you how to create image hyperlinks in a Word document by using Spire.Doc.

Step 1: Create a Word document, add a section and a paragraph.

Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();

Step 2: Load an image to a DocPicture object.

DocPicture picture = new DocPicture(doc);
picture.LoadImage(Image.FromFile("logo.png"));

Step 3: Add an image hyperlink to the paragraph.

paragraph.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink);

Step 4: Save the file.

doc.SaveToFile("output.docx", FileFormat.Docx);

Output:

Create an Image Hyperlink in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace CreateLink
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document();
            Section section = doc.AddSection();

            Paragraph paragraph = section.AddParagraph();
            Image image = Image.FromFile("logo.png");
            DocPicture picture = new DocPicture(doc);
            picture.LoadImage(image);
            paragraph.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink);

            doc.SaveToFile("output.docx", FileFormat.Docx);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Namespace CreateLink
	Class Program

		Private Shared Sub Main(args As String())

			Dim doc As New Document()
			Dim section As Section = doc.AddSection()

			Dim paragraph As Paragraph = section.AddParagraph()
			Dim image__1 As Image = Image.FromFile("logo.png")
			Dim picture As New DocPicture(doc)
			picture.LoadImage(image__1)
			paragraph.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink)

			doc.SaveToFile("output.docx", FileFormat.Docx)
		End Sub
	End Class
End Namespace