在Open xml文档中添加带有图像的超链接

时间:2016-07-12 04:57:24

标签: c# openxml

您好目前我已使用open xml将图像添加到我的word文档中。我需要添加一个超链接到图像。因此,当有人点击文档中的图像时,它会将他带到特定的网址。我怎样才能做到这一点。请有人帮助我。下面添加添加图像代码。

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++;

1 个答案:

答案 0 :(得分:1)

我找到了答案。愿这对某人有所帮助。

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);