How to add a link to a file in PdfSharp?

I can not get PdfSharp to show a picture for this annotation. It does not have the PdfAnnotation.Icon property, so I cannot set this.

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);
        gfx.
        link.Rectangle = new PdfRectangle(rec);
        page.Annotations.Add(link);

I got it this far, and the annotation exists, with the exception of an empty box! How do I get him to say something or even just show a picture?

+3
source share
3 answers

You need to use page.AddWebLink (AREArect) and then add the text area using gfx.drawstring (AREArect)

+3
source

PdfLinkAnnotation. page.AddDocumentLink, page.AddWebLink page.AddFileLink . , .

+2

Sample code for Uri use:

...
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");
...
+2
source

Source: https://habr.com/ru/post/1704099/


All Articles