How to print text fields in the correct coordinates?

I have no background in programming, and this is my first shot. I wrote a Delphi program that should print on a results sheet. I work at the institute, and every two months we have to make hundreds of results. This is really hard to do, and other handwriting is also an important issue. I have this code:

if PrintDialog.Execute() then begin with MyPrinter do begin MyPrinter.BeginDoc();//Start Printing //Prints First Name MyPrinter.Canvas.TextOut(FirstNameX,FirstNameY,EditFirstName.Text); //Prints Last Name MyPrinter.Canvas.TextOut(LastNameX,LastNameY,EditLastName.Text); //Prints Level MyPrinter.Canvas.TextOut(LevelX,LevelY,EditLevel.Text); //Prints Date MyPrinter.Canvas.TextOut(DateX,DateY,MEditDate.Text); //Prints Student Number MyPrinter.Canvas.TextOut(StdNumX,StdNumY,EditStdnumber.Text); .... MyPrinter.EndDoc();//End Printing end; end; 

I cannot get the correct coordinates correctly. Am I missing something? How to set the correct coordinates? You know that TPrinter uses pixels to get coordinates, but papers are measured in inches or centimeters. I'm really confused. I appreciate any help.

+4
source share
2 answers

@Milad I recommend using a report component, such as Quick Report (supplied with older versions of delphi and very easy to use) or FreeReport (free) to make the task easier.

check the abstract of links for more information

if you want to print directly from delphi without components here, I leave a couple of links that may help.

+4
source

Using a reporting tool is a good idea, if you really want to do it yourself, you can. I do this for printing on custom paper in our licensed application. The key is to know the size of your paper and work with it using the PageHeight and PageWidth page properties. For A4 paper, I chose 297 mm by 210 mm, and from there I was able to calculate where I want. The calculation is performed as follows:

 nStartPos := 210-141; nUserColX := muldiv(localPrinter.PageWidth, 187, 297); nUserColY := muldiv(localPrinter.PageHeight, nStartPos, 210); 

The variable nStartPos is executed to run in a specific place, and then nUserColY is used to move through the line at a time, as here:

 nUserColY := nUserColY - localPrinter.canvas.font.height - (localPrinter.canvas.font.height div 8); 

Then it allows you to pick up more than one line at a time beautifully. This is not complete, but should be a good start for custom printing. A.

+1
source

Source: https://habr.com/ru/post/1309476/


All Articles