Printing in Delphi.net
have a look at Microsoft .Net System.Drawing.Printing example
This is how it looks like with Delphi:
program printing;
{$APPTYPE CONSOLE}
{ need to make a reference to System.Drawing, because this is a console application, and it is not
done automatically; otherwise you get the message: "File not found System.Drawing.dcuil".
}
{%DelphiDotNetAssemblyCompiler '$(SystemRoot)\microsoft.net\framework\v1.1.4322\System.Drawing.dll'}
uses
System.IO,
System.Drawing,
System.Drawing.Printing;
var
streamToPrint: StreamReader;
printFont: Font;
// The PrintPage event is raised for each page to be printed.
procedure pd_PrintPage(sender: TObject; ev: PrintPageEventArgs);
var
linesPerPage: double;
yPos: double;
leftMargin, topMargin: double;
count: integer;
line: string;
begin
linesPerPage := 0;
yPos := 0;
count := 0;
leftMargin := ev.MarginBounds.Left;
topMargin := ev.MarginBounds.Top;
line := '';
// Calculate the number of lines per page.
linesPerPage := ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
line:=streamToPrint.ReadLine();
while((count < linesPerPage) and
(line <> nil)) do
begin
yPos := topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, StringFormat.GenericDefault);
inc(count);
line:=streamToPrint.ReadLine();
end;
// If more lines exist, print another page.
if(line <> nil) then
ev.HasMorePages := true
else
ev.HasMorePages := false;
end;
var
pd: PrintDocument;
begin
try
streamToPrint := StreamReader.Create
('c:\\myDocuments\\mytext.txt');
try
printFont := Font.Create('Arial', 10);
pd := PrintDocument.Create();
include(pd.PrintPage, pd_PrintPage);
pd.Print();
finally
streamToPrint.Close();
end;
except on E: Exception do
begin
System.Console.get_Error().Write(E.Message);
end;
end;
end.