简体   繁体   English

如何在PdfSharp中添加到文件的链接?

[英]How do I add a link to a file in PdfSharp?

I can't seem to get PdfSharp to show a picture for this annotation. 我似乎无法让PdfSharp显示此注释的图片。 It doesn't have the PdfAnnotation.Icon property, so I can't set that. 它没有PdfAnnotation.Icon属性,所以我不能设置它。

XFont font = new XFont("Verdana", 10);
PdfPage page = wDoc.Parent.Page;
XGraphics gfx = wDoc.Parent.gfx;
XRect rec = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(30, 30)));
PdfRectangle rect = new PdfRectangle(rec);
PdfLinkAnnotation link = PdfLinkAnnotation.CreateFileLink(rect, wDoc.FileLocation);
gfx.DrawString("These files were attached:", font, XBrushes.Black, 30, 50, XStringFormat.Default);

link.Rectangle = new PdfRectangle(rec);
page.Annotations.Add(link);

I've gotten it that far, and the annotation DOES exist, except it's a blank box! 我已经了解到了,并且注解确实存在,只是它是一个空白框! How do I go about making it say something, or even just show a picture? 如何使它说些什么,甚至只是显示图片?

您需要使用page.AddWebLink(AREArect),然后使用gfx.drawstring(AREArect)添加文本区域

I am not familiar with class PdfLinkAnnotation. 我对PdfLinkAnnotation类不熟悉。 You can use page.AddDocumentLink, page.AddWebLink, and page.AddFileLink to create links. 您可以使用page.AddDocumentLink,page.AddWebLink和page.AddFileLink创建链接。 If you use these methods, you can draw the icon as an image. 如果使用这些方法,则可以将图标绘制为图像。

sample code for Uri usage: Uri使用的示例代码:

...
PdfDocument pdfDoc = PdfReader.Open(myUri.LocalPath, PdfDocumentOpenMode.Import);
PdfDocument pdfNewDoc = new PdfDocument();
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
   PdfPage page = pdfNewDoc.AddPage(pdfDoc.Pages[i]);
   XFont fontNormal = new XFont("Calibri", 10, XFontStyle.Regular);    
   XGraphics gfx = XGraphics.FromPdfPage(page);
   var xrect = new XRect(240, 395, 300, 20);
   var rect = gfx.Transformer.WorldToDefaultPage(xrect);
   var pdfrect = new PdfRectangle(rect);

   //file link
   page.AddFileLink(pdfrect, myUri.LocalPath);
   //web link
   //page.AddWebLink(pdfrect, myUri.AbsoluteUri);


   gfx.DrawString("MyFileName", fontNormal, XBrushes.Black, xrect, XStringFormats.TopLeft);
}
pdfNewDoc.Save(myDestinationUri.LocalPath + "MyNewPdfFile.pdf");
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM