Graphics.DrawText and cm/mm instead of pixels

Hi,
i need to print some text on a postal that has specific places/forms where to insert text.
i’m using Graphics.DrawText:

Var ps As New PrinterSetup If ps.ShowPageSetupDialog Then Var page As Graphics page = OpenPrinterDialog(ps) If page <> Nil Then page.FontName = "Times New Roman" page.FontUnit = FontUnits.Point page.bold = True page.FontSize = 10 page.DrawText(label1.value, 6, 10) page.bold = False page.FontSize = 12 page.DrawText(label2.value, 22, 26) page.FontSize = 8 page.DrawText(label3.value, 30, 35) page.FontSize = 9 page.DrawText(label4, 29, 49) page.FontSize = 9 page.DrawText(label5, 40, 61) End If End If

Is it possible to use centimeters / millimeters instead of pixels ?
Or another method to do that ?
It’s quite annoying and time consumption trying to insert the exact value in pixel to make the strings printing correctly on the postal …

printersetup have a .VerticalResolution for dot per inch and graphics have .scalex scaley
1 inch = 2,54 cm

Are they always the same ?

If so (for example in an enveloppe; slow mail), you can compute (with a pen and paper) where to print and hard code the values.

I’ve done that using a word processor to print in a medical form (first name / family name / # / etc., long time ago. I was tired of doing that by hand. But it was for me.

Edit:
If thee choosed resolution is 72 dots per inch, and you have to start printing at X = 2.54 cm, you have to use x = 72 (in the DrawString X value).
(if you are in Windows and use 96 pixels per inch… x = 96).

If you have to print the start of the text at 5.34 cm, you have to compute the value in dot per inch by yourself (it‘s too late for me to compute that). You understand how to do it.
[probably (5.34 / 2.54) * ]

Is it clear ?

@Markus Rauch @Emile Schwarz thanks both !
So if i want to print a string at 2 cm left (x) and 3 cm top (y) i should insert these values:
2 cm / 2.54 * 72 dpi = 56.69 (i should rounded the value ?)
3 cm / 2.54 * 72 dpi = 85.03

page.DrawText(p1lblStudio.value, 56, 85)

The 72 dots per inch needed to be setup or is a default value for Mac ?
For Windows is 96 the DPI default value?
How to change the DPI (if needed) ?

printers have different dpi, so you need to read the dpi from printer. 150,300,600,1200 dpi
i would use the graphics scale propertys so you can input centimeters at graphics methods with x,y,width,height

So, assuming a 72 DPI printer (haven’t found a method for detect in Mac OS the dpi in Xojo) which is the correct graphics method to call for printing a string at 1 cm from left (x) and 2 cm from top (y)?
I’m trying with no result …

Dont assume the DPI
Once you select a printer the PrinterSetup has the vertical and horizontal as well as the size of the graphics surface (basically the page size) and you know the density from those values
Check out the Printing Examples that are part of Xojo (in a dir next to the EXE
Example Projects > Printing And Reporting > Printing
and you’ll see examples of how to use the horizontal and vertical resolution

First of all, read the Language Reference for the code to get the printer default resolution.

Then, compute the x and y values to position your text.

It was already said that the resolution (in dpi: dot per inch) have to be * by 2.54 to get the value in cm (1 inch = 2.54 cm).

In simple words:

Printer resolution: 300 dpi
Left value in cm: 10
Top value in cm: 5.5

You create a Picture with the correct width (22 cm) and height (11 cm) and use 300 as the dpi.

Then, for the first line, you print your text at,
X = 10 cm
Y = 5 cm

10 cm is nearly 4 inch (3.937…), so you use (Y = 10 / 2.54) * 300
Where 300 stands for the dpi, 2.54 is the number oc cm in an inch.

Same for Y.

For the next line, add a value a bit higher than the TextSize.

When the Text drawing in the Picture is done, you print the Picture to your enveloppe.

And If I am right, you get what you want. Else adjust the values.

The hard part is to be able to print on an enveloppe (Printer with Ink). Adjusting the values (the computations) is very easy once you get the first print.

If you do not achieve what you want, share your lines of code, someone will be able to help.

check this post for how to get the maximum resolution from a printer
https://forum.xojo.com/conversation/post/274368

Below is code that print your name in a PDF document.

It comes from a PushButton (modified from the LR).

Click, then select Enveloppe DL and open it in PDF (if you run macOS), / else generate a pdf.

It needs to be modified to fit your needs, but that is how I would solve the described problem.

[code]Sub Action() Handles Action
Var g As Graphics
Var p As PrinterSetup

p = New PrinterSetup
If p.ShowPageSetupDialog Then
g = p.ShowPrinterDialog
If g <> Nil Then
// Use the max resolution (Problems with PDF)
// p.MaxHorizontalResolution = -1
// p.MaxHorizontalResolution = -1

  Var X, Y As Integer
  Var Inch As Double = 2.54
  Var DPI_X As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
  Var DPI_Y As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
  
  // Set the X start value
  X = (DPI_X * 10) / 2 // For PDF printing only (Remove / 2 for paper printing ?)
  y = (DPI_Y *  5) / 2 // For PDF PRinting only (Remove / 2 for paper printing ?)
  
  // For debug purposes only
  // MessageBox "X,Y:" + EndOfLine + X.ToString + "," + Y.ToString + EndOfLine + "Horizontal DPI: " + DPI_X.ToString
  
  // Draw the first line
  g.DrawString("Fabrizio Cesare", X, Y + 16) // + 16: value of the TextSize
End If

End If
End Sub[/code]

Hi @Fabrizio Cesare

Don’t know if that would help to solve the problem you’re describing, but did you try to change FontUnits to points or millimeters instead of pixels?

Look there:
http://documentation.xojo.com/api/graphics/graphics.html#graphics-fontUnit

added in 2019r2.

[quote=497327:@Emile Schwarz]Below is code that print your name in a PDF document.

It comes from a PushButton (modified from the LR).
// Set the X start value
X = (DPI_X * 10) / 2 // For PDF printing only (Remove / 2 for paper printing ?)
y = (DPI_Y * 5) / 2 // For PDF PRinting only (Remove / 2 for paper printing ?)
[/quote]

I’ve tried with /2 and on paper the string is printed at 15,4 cm (left) and 7,4 (top)
removing the /2 nothing is printed

@Javier Menéndez i’ve also tried with points and millimeters with no success …

It is not DPI_X * 10 / 2 but
DPI_X * 10 / 2.54
and don’t remove / 2.54

Remember also that printers have margins.
In PrinterSetup, see differences between Height / PageHeight and Width / PageWidth.
If you write X = DPI_X * 10 / 2.54, you draw at 10 cm from the left margin.

Eric

So, if the values of:

p.PageLeft p.PageTop

are -18 and -18 i’ve to subtract these values to X and Y, correct ?