如何使用MigraDoc和C#将超链接添加到标题图像?

问题描述

我有一个项目,该项目需要在作为标头一部分的图像中嵌入超链接。标头会自动复制到其他PDF页面

请清楚一点,我要生成PDF,然后单击图像,然后将打开一个浏览器选项卡并显示一个网站。 我不想从网站上提取图像并将其嵌入到我的PDF中。

图像是文档中的第一个对象。

否则,我的PDF效果很好。

这是我的一些获取图像并显示它的代码

bmp.Save(combinesfinalpath);   // Save the Image


// Retrieve the Image and add to the Header    
MigraDoc.DocumentObjectModel.Shapes.Image image = sec.Headers.Primary.AddImage(@"C:\Users\" + userName + @"\" + "AppData" + @"\" + "Roaming" + @"\" + "PN" + @"\" + "PH2.png");


//Adjust the Image to fit page
image.Height = "3.0cm";
image.Width = "16.0cm";
image.LockAspectRatio = true;
        
 


// Start Adding Header Text       
sec.Headers.Primary.AddParagraph();  // insert blank paragraph

Paragraph paragraph = sec.Headers.Primary.AddParagraph();
        
paragraph.Format.Font.Color = Colors.Red;
paragraph.Format.Font.Size = "9";
paragraph.Format.Alignment = ParagraphAlignment.Right;
paragraph.AddFormattedText("Website  |  Facebook  |  Instagram        ",textformat.Bold);


....code continues

仅用于纯文本的超文本示例相当容易,但是使用标头图像则很难找到。

我确实找到了这个,但是示例中的图片没有作为标题的一部分:

Hyperlink hyperlink = paragraph.AddHyperlink(str,hyperlinkType.Web)
hyperlink.AddImage(...);

一个问题是,由于图像是第一个对象,因此它不是段落的一部分,因此该段落引用了错误信息(未定义)。

该示例也来自2008年的论坛帖子,因此不确定是否是最佳解决方案。

任何帮助将不胜感激!

解决方法

现在已解决!添加了第二段。