views:

165

answers:

2

I don't have any background in programming and this is my first shot. I wrote a Delphi program that is supposed to print on a result sheet. I work in an institute and we have to establish hundreds of result sheets every 2 months. It's really difficult to do that and different 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 can't get the right coordinates to print properly. Am I missing something? How can I set the right coordinates? You know TPrinter uses pixels to get the coordinates but papers are measured in inches or centimeters. I'm really confused. I appreciate any help.

+4  A: 

@Milad I recommend you use a report component like Quick Report (comes with the oldies versions of delphi and is very easy to use) or FreeReport (it's free) to make your task easier.

for more info check theses links

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

RRUZ
In other words, take a look at whatever report generator is bundled with your version of Delphi: It might be ReportBuilder for example. If you'd like to spend some money (and you shouldn't for a one-time-job) you might want to look at FastReport, the bigger brother of FreeReport...
Cosmin Prund
+1  A: 

While using a reporting tool is a good idea, if you really do want to do it yourself, you can. I do this for printing to custom paper in our licensing application. The key is to know the size of your paper, and work it out from that using the printer PageHeight and PageWidth properties. For A4 paper I chose to use 297mm by 210mm, and from that I was able to calculate where I wanted things to be. The calculation is done thus:

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

The nStartPos variable is done to start on a particular place, and then nUserColY is used to move down a line at a time as here:

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

This then allows more than one line at a time to fit nicely. This is not complete, but should be a good start for custom printing.

mj2008