Kanishka
Kanishka

Reputation: 361

Add hyperlink with an image in Open xml document

Hi Currently I have added an image to my word document using open xml. I need to add an hyperlink to the image. So when someone click on the image in the document it will take him to the specific web url. How can i achieve this. Please someone help me. Adding the image code is added bellow.

var run1 = new Run();
                        var picture1 = new Picture();
                        var shape1 = new Shape() { Id = "_x0000_i1025" + x};

                    string rId = "rId" + x ;
                    var imageData1 = new ImageData() { RelationshipId = rId };
                    shape1.Append(imageData1);
                    picture1.Append(shape1);
                    run1.Append(picture1);

                    mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
                   new System.Uri(matchString, System.UriKind.RelativeOrAbsolute), rId);

                    DocumentFormat.OpenXml.Wordprocessing.TableRow tr1 = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
                    DocumentFormat.OpenXml.Wordprocessing.TableCell Name1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new Run(new Text(item.FirstName))));
                    DocumentFormat.OpenXml.Wordprocessing.TableCell Message1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(run1));
                    DocumentFormat.OpenXml.Wordprocessing.TableCell Time1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new Run(new Text(item.CreatedDate))));
                    tr1.Append(Name1, Message1, Time1);
                    table.AppendChild(tr1);
                    x++;

Upvotes: 1

Views: 1570

Answers (1)

Kanishka
Kanishka

Reputation: 361

I found the answer. May this help someone.

var run1 = new Run();
                            var picture1 = new Picture();
                            var shape1 = new Shape() { Id = "_x0000_i1025" + x};

                            string hyperid="hprid_"+x;
                            var hlink = new Hyperlink() { Id=hyperid, DocLocation=matchString}; ;
                            hlink.Append(picture1);

                            string rId = "rId" + x ;
                            var imageData1 = new ImageData() { RelationshipId = rId };
                            shape1.Append(imageData1);
                            picture1.Append(shape1);
                            run1.Append(hlink);
                            mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
                           new System.Uri(matchString, System.UriKind.RelativeOrAbsolute), rId);

                            mainPart.AddHyperlinkRelationship(new Uri(matchString), true, hyperid);


                            DocumentFormat.OpenXml.Wordprocessing.TableRow tr1 = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
                            DocumentFormat.OpenXml.Wordprocessing.TableCell Name1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new Run(new Text(item.FirstName))));
                            DocumentFormat.OpenXml.Wordprocessing.TableCell Message1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(run1));
                            DocumentFormat.OpenXml.Wordprocessing.TableCell Time1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new Run(new Text(item.CreatedDate))));
                            tr1.Append(Name1, Message1, Time1);
                            table.AppendChild(tr1);

Upvotes: 1

Related Questions