No proper right alignment possible in pdf

I’m having a lot of trouble right-aligning text in a pdf. In print it is done correctly, but when I output the same code as a pdf it is not possible to right align text.
I use the textwidth function for right alignment by calculating the column width minus the text length and outputting the text from that position.
As you can see in the screenshots, it works in print, but unfortunately not in pdf.

Print:

pdf:

Create some sample code showing the issue, a simple one line print showing it will help.
Zip the project and share here.

Print also a rectangle as an expected area for a “table” for spatial reference.

Here is a short sample-code showing the problem

pdfDruckTest.zip (5.8 KB)

Seems something really wrong with the PDF measurements.

Open an Issue case.

Here is a simpler sample: pdfDruckTest1.zip (4.9 KB)

Measures the same, but offsets differently.

Tested using Xojo 2024r4.2 on Windows 11

in your pdf screenshot, why are the text not correctly right aligned, but the numbers at the right are correctly aligned ???

try text shape (instead of DrawText), but i believe it was not supported in web apps.

Var d As New PDFDocument
Var g As PDFGraphics = d.Graphics
Var t As New TextShape
t.Text = "Hello World"
t.x = g.Width
t.y = 100
t.FontSize = 24
g.DrawingColor = Color.Red
t.HorizontalAlignment = TextShape.Alignment.Right
g.DrawObject t
Var f As FolderItem = FolderItem.TemporaryFile
d.Save f
f.Open

When I replace € with ‘Euro’ the text is aligned correctly, so for whatever reason it seems to be the Euro glyph that’s triggering this issue.

I was wondering the same thing. My idea was because numbers always have the same format, i.e. numbers and euro signs. However, spaces and completely different characters are used in the texts.

That sounds interesting, I didn’t know about the existence of TextShape. I will try it out.

Unfortunately, it is not only related to the euro symbol, it also does not work correctly with “normal” characters. It seems to be a coincidence here.

The first 2 lines does not have Euro in the PDF and are unaligned.

Using TextShape helps a lot pdfTestTextshape.zip (5.1 KB)

Example with FontName = “Helvetica”

But in case of font name mismatch it starts silently fail too
there’s something wrong and weird here.

Example with FontName = “Helvetic”

https://tracker.xojo.com/xojoinc/xojo/-/issues/78663

1 Like

Built a script that does a thing, just dropping by to test…
#78663 - PDF font metrics seems bugged under certain circustances causing alignment issues

This doesn’t surprise me. If you specify an invalid font name during document construction, you’re likely getting A) invalid metrics in your code, and B) a substituted font in your PDF reader. Either will produce misaligned text.

1 Like

Well, you may need DynaPDF to do this correctly.

Xojo will probably use graphics.StringWidth() internally to measure the width of text and position it. But then it depends on the text width in graphics class to the the same as in the PDF, which may not be the case.

You see the difference best if you use a font available for PDF, but not installed on the computer. Xojo’s right alignment measures then with the system font, which may be off by a lot.

With DynaPDF we can measure the characters as they are drawn in the PDF.

1 Like

I would expect a font fallback to the default standard PDF Helvetica and proper measurements. It would not look as expected if the user mistyped a valid font, but it would have proper measurements.

Nope, we just need Xojo to fix simple things. DynaPDF is for advanced things.

1 Like

A lot of ideas, thanks to everyone. I will test the TextShape and hope that one day the PDF measurements will work correctly in Xojo.

I’m not sure there is a documented standard behaviour for missing font substitution. Adobe has their own magic system for faking a missing font; laser printers usually show Courier.

Don’t count on it. I’ve tried importing the Test.pdf in GraphicsConverter which offers a choice between the Apple Quartz PDF Importer and the DynaPDF Importer, and both render the text differently.

Your code tries to position the text so it will get rendered correctly if you were drawing in a Graphics context of a Canvas or Picture. The problem is that you are actually creating a PDF that may get rendered slightly differently and your measurements don’t really apply – not exactly anyway. As it is technically a subclass of Graphics a PDFGraphics may behave a lot like a Graphics object but I would bet that internally the two aren’t similar at all. Drawing text in a Graphics context creates pixels in a bitmap while drawn in a PDFGraphics it is still text (that you can select and copy as text when you open the PDF, which you couldn’t if it was a bitmap).

TextShape may give better results because you can explicitly state that you want the text to be right justified rather than calculating the positions of the lines of text yourself.