PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

XRect and Annotations using different coordinate systems?
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3954
Page 1 of 1

Author:  Matt Myers [ Fri May 10, 2019 6:12 pm ]
Post subject:  XRect and Annotations using different coordinate systems?

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:

Code:
        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:

Code:
        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?

Thanks!

Author:  TH-Soft [ Sun May 12, 2019 12:58 pm ]
Post subject:  Re: XRect and Annotations using different coordinate systems

The function call you need is
Code:
XRect rect = this.gfx.Transformer.WorldToDefaultPage(this.hyperlinkRect);
[/quote]


See also:
viewtopic.php?p=3805#p3805

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