PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Sep 28, 2021 6:27 am

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: Fri May 10, 2019 6:12 pm 

Joined: Fri May 10, 2019 6:00 pm
Posts: 1
Hi all,

I am attempting to create a WebLink that overlays drawn text. I am using the same XRect when drawing the text and adding the web link. An example of my current code is:

        PdfDocument document = new PdfDocument();
        PdfPage page = document.Pages.Add();
        XFont font = new XFont("Arial Unicode MS", 10, XFontStyle.Regular);
        string text = "Hello World";

        XGraphics gfx = XGraphics.FromPdfPage(page);
        XSize textSize = gfx.MeasureString(text, font);
        XPoint textLocation = new XPoint(50, 50);
        XRect textRectangle = new XRect(textLocation, textSize);
        gfx.DrawString(text, font, XBrushes.Black, textRectangle, XStringFormats.TopLeft);

        page.AddWebLink(new PdfRectangle(textRectangle), "http://www.google.com");

However, the above code creates the text in the top left corner and the annotation in the bottom left. In order to get the two elements to line up I need to invert the annotation's Y position like so:

        XPoint invertedYPoint = new XPoint(textLocation.X, page.Height - textLocation.Y - textSize.Height);
        page.AddWebLink(new PdfRectangle(invertedYPoint, textSize), "http://www.google.com");

Is this intended behavior, or perhaps there are orientation settings I need to add/change?


Reply with quote  
PostPosted: Sun May 12, 2019 12:58 pm 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 660
Location: CCAA
The function call you need is
XRect rect = this.gfx.Transformer.WorldToDefaultPage(this.hyperlinkRect);

See also:

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

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: Google [Bot] and 61 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