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

本文关键字:图像 超链接 添加 Open xml 文档 | 更新日期: 2023-09-27 17:56:22

嗨,目前我已经使用打开的xml向我的word文档添加了图像。我需要为图像添加超链接。因此,当有人单击文档中的图像时,它会将他带到特定的 Web URL。我怎样才能做到这一点。请有人帮助我。添加图像代码是在下面添加的。

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

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

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

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