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.
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
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.
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.
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.
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.
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.