PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 29, 2020 11:19 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Sep 30, 2020 2:53 pm 
Offline

Joined: Wed Sep 30, 2020 2:19 pm
Posts: 2
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 26, 2020 8:34 am 
Offline

Joined: Wed Sep 30, 2020 2:19 pm
Posts: 2
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group