Rotated Text in Xojo PDF

I’m trying to using Xojo’s implementation of PDF to produce a 1 page document with rotated text along the Y-axis of a graph that I am drawing (not using Chart class because the graph is too complicated). It seems there are two ways to draw rotated text that work fine in a Canvas but not in PDFs:

  1. Use a TextShape and use object2D rotate:

    Var t as New TextShape
    t.value = “Axis Label”
    t.rotate = -pi/2
    g.DrawObject t

  2. Rotate the entire drawing context using Graphics.Rotate"

    Var t as String = “Axis Label”
    g.Rotate -pi/2, x, y
    g.DrawText t
    g.Rotate pi/2, x, y

Neither of these work with PDFs. Option 1 does not crash but the text is simply not drawn to the PDF. Option 2 produces an UnsupportedOperationException “Graphics object cannot be used outside its Paint event”. In option 2, if I use, instead, g.Rotate -pi/2, no exception occurs and the text is drawn but it is not rotated. The documentation for Graphics.Rotate implies that g.Rotate works in PDFs but there may be problems when combined with g.Scale or g.Translate…

Is there another way to accomplish this in Xojo PDFs or is it time to report an “issue”? [I am aware that this can be accomplished using the MBS DynaPDF plugin but wanted to see if I could use Xojo’s implementation for this project.]

The samples on this blog entry shows text rotation.

Thanks for the reminder about that. After playing around some more, I figured out what is happening and can reproduce the behavior using the code on the blog:

My code was using 1.570796326794412 for pi/2. This works fine, though perhaps a bit overly precise, for rotating the drawing context in a canvas. However, that many significant figures in the pdf results in unrotated text. You have to reduce the significant figures to the right of the decimal point to 4 or less for g.rotate to work in the the pdf:

g.rotate(-1.5707) // works as expected
g.rotate(-1.57079) // text remains unrotated!

what a weird behavior!

And I could replicate it. Please report the issue, It doesn’t make sense.

This code:

Const pi = 3.1415926

Var d As New PDFDocument
Var g As Graphics = d.Graphics

var offx As Double = 40
Var offy As Double = 40

g.FontSize = 30

g.DrawText("Sample Text", offx, offy)


g.DrawText("Sample Text", offx, (offy+g.TextWidth("Sample Text")))


offx = 80+g.TextWidth("Sample Text")

g.DrawText("Sample Text", offx, offy)


g.DrawText("Sample Text", offx, (offy+g.TextWidth("Sample Text")))

g.Rotate(-pi / 2)

g.DrawText("-pi/2 ok __________", offx, (offy+g.TextWidth("-pi/2 ok __________")))

Var f As FolderItem = SpecialFolder.Desktop.Child("Rotate.pdf")



Should produce this output:

But instead it produces:

The g.Rotate(-pi / 2) triggers some weird bug and the string “-pi/2 ok __________” vanishes

Issue reported (72921).

To be clear, the bug applies, not just to text, but to all graphics objects. The code I added to the issue tracker includes the drawing of a rectangle to demonstrate this.

Thanks to @Rick A for confirming the bug and for reminding me of the blog post that caused me to dig deeper into why the blog post code works and my code didn’t.

1 Like

Ok @Richard_Allmendinger and @Rick_Araujo

First of all… thank you for finding it and reporting!! I found the culprit and the fix is on its way :+1:


Thanks, Javier!

P.s., Any chance of getting graphics.rotate(pi/2, x, y) working as well? Right now that causes an Unsupported Operation error when attempting to use it in drawing to a PDF.