Re: Delphi Printing: more than 1 page

[ DelphiLand Discussion Forum ]

Posted by webmaster Guido on March 16, 2003

In Reply to: Printing more than 1 page posted by Ste D on March 13, 2003

: Using some bodged code I made I got my tables printing just fine at the moment, but noticed that it only prints the first page, and since I can only fit about 10 records per page its not much good since there’s gonna be loads more any ideas any1??

---------

Calculate the maximal vertical position where you can print, say Ymax.

Keep track of the vertical position, say Y, where you have to print the next record.
Before printing a record: if Y > Ymax then start a new page with Printer.NewPage and print the column headers.
After printing a record, update Y so that it contains the position for the next line to print.

var
  LineSpacing, BottomMargin, TopMargin, Y, Ymax: integer;
  X1, X2, X3: integer;

begin
  // Calculate or set fixed values for vertical positions
  TopMargin := 300; 
  BottomMargin := 300;
  Ymax := Printer.PageHeight - BottomMargin;
  // Calculate or set fixed values for horizontal positions
  X1 := 200;  // left margin
  X2 := 1000; // start of column 2 
  X3 := ...;  // start of column 3 

  // Print report heading, only on first page 

  Y := TopMargin;
  Printer.Canvas.Font := ...; // Font for report heading
  LineSpacing := Printer.Canvas.TextHeight('X'); 
  // If you want a bigger linespacing, multiply by some factor, e.g.:
  // LineSpacing := Round(LineSpacing * 1.1); 
  Printer.Canvas.TextOut(X1, Y, 'Report Header');
  Y := Y + LineSpacing;
  // ...and more lines if desired

  // Print column headings (repeated on each page)

  Printer.Canvas.Font := ...; // Font for header
  LineSpacing := ...;
  Printer.Canvas.TextOut(X1, Y, 'Field 1');
  Printer.Canvas.TextOut(X2, Y, 'Field 2');
  // ...and so on for more fields
  Y := Y + LineSpacing;

  // Print table data 

  Table1.First;
  Printer.Canvas.Font := ...; // Font for data lines
  LineSpacing := ...; 
  while not Table1.Eof do begin
    if Y > Ymax then begin
      Printer.NewPage;
      Y := TopMargin;
      // Print column headings... (see above)
      // Set Font and LineSpacing back to values for data... (see above)
    end;
  
    // Print a record
    Printer.Canvas.TextOut(X1, Y, ...); // field 1
    Printer.Canvas.TextOut(X2, Y, ...); // field 2
    // ...and so on for more fields
    Y := Y + LineSpacing;
    Table1.Next;
  end;

  Printer.EndDoc; 
end;


Related Articles and Replies:


[ DelphiLand Discussion Forum ]