Hi,
This is my first post. Yesterday, I started to play with TPrinter for TStringGrid printing fucntion. I have to use 'NewPage' procedure when the pahe height is not enough. It works well when I use a printing machine : I print 2 or more pages with inserted data. But when the output is a ps file (Printer.Filename := 'output.ps'), the second page is created but there is data only on the first page.
procedure TMainForm.acPrintExecute(Sender: TObject);
const
LeftMargin = 100;
TopMargin = 200;
BottomMargin = 200;
HEADLINE = 'APTEC sarl / Le ';
var
I, Y, LineHeight, NbPages: Integer;
HeadMargin, FootMargin, TitleMargin : Integer;
PrintString: String;
begin
// Fermer si la liste est vide
if MainGrid.RowCount =1 then Exit;
if PrintDialog.Execute then
begin
// Si Impression fichier alors init nom du fichier
with Printer do
try
if PrintDialog.PrintToFile = true then
FileName := ExtractFileNameWithoutExt(OpenedFileName)+'.ps';
Orientation := poPortrait;
LineHeight := Round(1.2 * Abs(Canvas.TextHeight('I'))); // Calcule hauteur ligne
// Démarre le document
BeginDoc;
Canvas.Font.Name := 'Courier New'; // Init le nom de la police
Canvas.Font.Color := clBlack; // Init couleur de la police
Canvas.Font.Size := 10; // Init taille de la police
Canvas.Font.Bold := true;
// Calcule le nombre de pages
NbPages := ((LineHeight*MainGrid.RowCount)+(TopMargin*2))div PageHeight;
// Imprimer entete document
Y := TopMargin; // Position début écriture
PrintString := HEADLINE + DateTimeToStr(Now)
+' - Page' +inttostr(PageNumber)
+'/'+inttostr(NbPages)
+FileName;
Canvas.TextOut(LeftMargin, Y, PrintString);
Canvas.Line(LeftMargin, Y+LineHeight, PageWidth-LEFTMARGIN, Y+LineHeight);
// Imprimer entete du tableau
Canvas.Font.Size := 12; // Init taille de la police
Y := Y+(LineHeight); // Position début écriture
PrintString := Format('%-5s', ['Ordre'])+' '+
Format('%-10s', [MainGrid.Columns[6].Title.Caption])+' '+
Format('%-100s', [MainGrid.Columns[7].Title.Caption]);
Canvas.TextOut(LeftMargin, Y, PrintString);
//Imprimer listing
Canvas.Font.Bold := False;
for i:=1 to MainGrid.RowCount-1 do
begin
if (Y >= (PageHeight-BottomMargin)) then
begin
Y := TopMargin;
NewPage;
end;
Y := Y+(LineHeight);
PrintString := Format('%-5s', [MainGrid.Cells[0, i]])+' '+
Format('%-10s', [MainGrid.Cells[7, i]])+' '+
Format('%-100s', [MainGrid.Cells[8, i]]);
Canvas.TextOut(LeftMargin, Y, PrintString);
//ShowMessage(inttostr(Y));
end;
finally
EndDoc;
end;
end;
end;
Please inform if there is any problem with multi page output PS file. The other solution is to use a local PDF printer and that time, it works well but I cannot manage save directory and file name.
Regards.
Salim.