Wednesday, October 14, 2026
HomeiOS Developmentc# - How to attract upright annotation on a rotated PDFKit.PDFPage in...

c# – How to attract upright annotation on a rotated PDFKit.PDFPage in iOS

[ad_1]

1. Summarize the issue:
I am making an attempt to attract annotations on PdfPage in iOS from a pattern JSON knowledge like this:

"annotations": [
    {
        "id": "1024",
        "start_x": 1.60162,
        "start_y": 565.139,
        "end_x": 131.333,
        "end_y": 610.398,
        "page": 1
    },
]

The API facet understands {that a} pdf web page coordinate system all the time has its origin on the bottom-left nook of the seen web page no matter its rotation angles. Due to this fact it is anticipated that with the above JSON the annotation will likely be drawn close to the top-left nook of the web page. I’ve highlighted each the API coordinate system and the anticipated annotation place in yellow colour.

The PDFKit.PdfPage has a rotation property that trigger each the web page and its coordinate system rotated (highlighted in inexperienced colour). The rotation property of PdfPage on iOS improve clockwise. As proven within the picture, the pdfPage.rotation = 90 and the annotion is drawn rotated. The pattern pdf present within the picture has 2 pages the place web page no. 2 (proven close to the primary part of the picture above the purple line) has its rotation = 0 so annotions are drawn precisely as anticipated from JSON knowledge response.

With that being stated, I have to adress two important issues to attract an upright annotation on a rotated pdf web page:

  • Downside #1: The inexperienced rectangle annotation have to be rotated and/or translated to its anticipated place whatever the web page’s rotation. On this case the web page’s rotation is 90 diploma and its coordinate origin is on the top-left nook, however it may be 180 diploma rotation with origin at top-right nook and 270 diploma rotation with origin at backside proper nook. The API facet all the time nail the coordinate origin on the bottom-left of the seen web page whatever the web page’s rotation and may response annotation positions wherever on the seen web page.
  • Downside #2: The textual content “Pattern Anno” additionally must be drawn upright based on API’s coordinate system and once more, whatever the web page’s rotation angles. Presently I am utilizing a PDFKit.pdfAnnotation to attract a certain of sort CGRect, set it background colour to inexperienced and its contents is “Pattern Anno”. Inside this annotation I draw a UILabel which match inside the required certain and it is drawing upright based on the web page’s coordinate system.

2. What I’ve tried:
Very first thing that come to my thoughts is ready pdfPage.rotation to “0” and run but it surely additionally rotate the entire seen web page, and this doesn’t certified to my anticipated consequence as a result of the web page must preserve its present rotation angle to match with response from API.
Then I’ve tried to rotate the annotation certain utilizing this method, with the rotation of 53 diploma (purely random) across the anchor level is A (middle of the web page’s media field) as present within the backside part of the picture, the certain is rotated horizontally however nonetheless removed from the anticipated place which is highlighted in yellow colour. And this method additionally make the textual content smaller as a result of it nonetheless being drawn upright based on web page’s rotation and desires to suit inside the brand new certain (drawback #2). Now I am utilizing CGAffineTransform.MakeRotation() and context.RotateCTM() features from this reply to drawback #2 however to this point the textual content disappear.
I even have considered proposing the API facet coordinate system to alter based on pdfkit rotation however that may be double the work since this drawback is exclusive to iOS and each the online and Xamarin.Android are drawing annotation the identical manner.

3. My questions are:
Q1: Are there any common guidelines or formulation that may convert some extent from any pdfPage.rotation coordinate system (be it 90, 180 or 270) to the bottom-left coordinate system?
Q2: If the reply to Q1 is none. How can I draw each annotation certain and textual content upright in every of the three pdfPage.rotation angle? As a result of every rotation angle has differnt impact on how the annotation is drawn. For instance with 180 diploma rotation angle, annotation that anticipated to be on the highest left of the web page will likely be flipped and rotated to the bottom-right space of this web page. 270 diploma rotation is just like 90 diploma rotation however the distinction is that annotation is rotated counter-clockwise.

I am growing this in Xamarin.iOS so solutions in each C# and Swift are nice. Thanks guys a lot!
enter image description here

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments