PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Annotations with Weblink in with pdfsharp
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4180
Page 1 of 1

Author:  kunjgulati [ Wed Sep 30, 2020 2:53 pm ]
Post subject:  Annotations with Weblink in with pdfsharp

I am trying to get the annotation (weblink) defined in the PDF on a specific text field.

I am able to get the references from reader with page.annotations but when I call AddWebLink on the XRect object with the rect parameters, It shows at a different locations.

In general, I need a solution on how I can grab the fields which are of type LINK and manipulate that link.

Hope I am able to explain the problem. Below is the sample code.

PdfAcroForm acroform = reader1.AcroForm; // reader1 is a pdfdocument which I am opening in Modify mode
var af = acroform.Fields;
for (int i = 0; i < reader1.PageCount; i++)
{
PdfPage page = reader1.Pages[i];
for (int p = 0; p < 25; p++)
{
PdfAnnotation textAnnot = page.Annotations[p];
PdfItem type = (page.Annotations[p].Elements["/Subtype"]);
string content = textAnnot.Contents;
if (type.ToString() == "/Link")
{
XFont fontNormal = new XFont("Calibri", 10, XFontStyle.Regular);
using (XGraphics gfx = XGraphics.FromPdfPage(page))
{
var xrect = new XRect(textAnnot.Rectangle.X1, textAnnot.Rectangle.Y1, textAnnot.Rectangle.X2, textAnnot.Rectangle.Y2);
var rect = gfx.Transformer.WorldToDefaultPage(xrect);
var pdfrect = new PdfRectangle(rect);
page.AddWebLink(pdfrect, "www.google.com");
gfx.DrawString("This Is a link", fontNormal, XBrushes.Black, xrect, XStringFormats.TopLeft);
}
}

}
}
reader1.Save("E:\\pdfsharp\\pdftest.pdf");

Please suggest.

Author:  kunjgulati [ Mon Oct 26, 2020 8:34 am ]
Post subject:  Re: Annotations with Weblink in with pdfsharp

Figured It out.

Using:

((PdfSharp.Pdf.PdfDictionary)(((PdfSharp.Pdf.Advanced.PdfReference)((PdfSharp.Pdf.PdfItem[])(page.Annotations[p].Elements).Values)[0]).Value)).Elements["/URI"] = new PdfString("https://www.google.com");

this changes the existing URI value of the specific link place holder in pdf, to the one you set(google.com in this case).

Links with the placeholders in pdf can be manipulated using above method.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/