开发者

How can I print text fields at the right coordinates?

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);

   开发者_StackOverflow社区       //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.


@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

  • QuickReports Tutorial
  • QuickReports Documentation, Knowledge Base and FAQ

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

  • Printing Directly from Delphi
  • Delphi - Printing via the TPrinter Canvas


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜