需要帮助用delphi打印文本文件 [英] Help needed about printing text file with delphi
问题描述
procedure TForm1.print_btnClick(Sender:TObject);
var
filename:string;
begin
filename:='printfile.txt';
ShellExecute(handle,'print',pchar(Filename),nil,nil,SW_NORMAL);
结束
另一个位于 http://www.delphibasics.co.uk/Article.asp?Name=Printing
这是一个循环的ok对话框一次又一次,它可以打印任何东西。
greetings
选项1
您可以编写自己的打印代码。一个简单的例子(使用打印机
):
procedure PrintTextFile(const FileName :string; const编号:boolean = true);
const
FONT_NAME ='Times New Roman';
FONT_SIZE = 10;
var
MARGIN:integer;
sl:TStringList;
i,h:整数;
r,rFooter:TRect;
s:string;
DocEnd:integer;
开始
与TPrintDialog.Create(nil)做
尝试
如果不执行然后
退出;
终于
免费;
结束
sl:= TStringList.Create;
try
sl.LoadFromFile(FileName);
Printer.BeginDoc;
Printer.Title:= FileName; //或应用程序名称或sth else
Printer.Canvas.Font.Name:= FONT_NAME;
Printer.Canvas.Font.Size:= FONT_SIZE;
MARGIN:= 5 * Printer.Canvas.TextWidth('M');
DocEnd:= Printer.PageHeight - MARGIN;
如果编号然后
开始
dec(DocEnd,2 * Printer.Canvas.TextHeight('8'));
rFooter:= Rect(0,DocEnd,Printer.PageWidth,Printer.PageHeight - MARGIN);
DrawText(Printer.Canvas.Handle,
PChar(IntToStr(Printer.PageNumber)),
length(IntToStr(Printer.PageNumber)),
rFooter,
DT_SINGLELINE或DT_CENTER或DT_BOTTOM);
结束
r.Left:= MARGIN;
r.Top:= MARGIN;
for i:= 0 to sl.Count - 1 do
begin
r.Right:= Printer.PageWidth - MARGIN;
r.Bottom:= DocEnd;
s:= sl.Strings [i];
如果s =''then s:='';
h:= DrawText(Printer.Canvas.Handle,//纸张上的段落高度
PChar,
长度,
r,
DT_LEFT或DT_TOP或DT_WORDBREAK或DT_CALCRECT);
如果r.Top + h> = DocEnd然后
begin
Printer.NewPage;
如果编号然后
DrawText(Printer.Canvas.Handle,
PChar(IntToStr(Printer.PageNumber)),
length(IntToStr(Printer.PageNumber)),
rFooter,
DT_SINGLELINE或DT_CENTER或DT_BOTTOM);
r.Top:= MARGIN;
r.Bottom:= DocEnd;
结束
如果h> Printer.PageHeight - 2 * MARGIN then
raise Exception.Create('Line too long to fit on single page。');
DrawText(Printer.Canvas.Handle,
PChar(s),
length(s),
r,
DT_LEFT或DT_TOP或DT_WORDBREAK);
inc(r.Top,h);
结束
Printer.EndDoc;
finally
sl.Free;
结束
结束
警告:上面的代码不如果文本文件中的任何一行都非常宽,以至于它不能放在一张纸上(在包装之后),则可以正常工作。我现在太累了,无法解决这个问题。
选项2
讨厌的伎俩是使用无形的 TRichEdit
打印。
procedure PrintTextFile (AOwner:TWinControl; const FileName:string);
begin
with TRichEdit.Create(nil)do
try
可见:= false;
父级:= AOwner;
Lines.LoadFromFile(FileName);
with TPrintDialog.Create(nil)do
try
if Execute then
Print(FileName);
终于
免费;
结束
终于
免费;
结束
结束
我建议反对,因为它有点太讨厌了。
i m trying to print a text file with Delphi 2010. i found some code but when i run, it asks to save an xps file, it doesn t show print dialog. the code is located at http://www.delphipages.com/forum/showthread.php?t=72986
procedure TForm1.print_btnClick(Sender: TObject);
var
filename: string;
begin
filename := 'printfile.txt';
ShellExecute(handle, 'print', pchar(Filename), nil, nil, SW_NORMAL);
end;
another one is located at http://www.delphibasics.co.uk/Article.asp?Name=Printing
this one is looping "ok" dialogs again and again, it can t print anything.
greetings
Option 1
You could write your own printing code. A simple example (uses Printers
):
procedure PrintTextFile(const FileName: string; const Numbering: boolean = true);
const
FONT_NAME = 'Times New Roman';
FONT_SIZE = 10;
var
MARGIN: integer;
sl: TStringList;
i, h: Integer;
r, rFooter: TRect;
s: string;
DocEnd: integer;
begin
with TPrintDialog.Create(nil) do
try
if not Execute then
Exit;
finally
Free;
end;
sl := TStringList.Create;
try
sl.LoadFromFile(FileName);
Printer.BeginDoc;
Printer.Title := FileName; // or application name or sth else
Printer.Canvas.Font.Name := FONT_NAME;
Printer.Canvas.Font.Size := FONT_SIZE;
MARGIN := 5*Printer.Canvas.TextWidth('M');
DocEnd := Printer.PageHeight - MARGIN;
if Numbering then
begin
dec(DocEnd, 2*Printer.Canvas.TextHeight('8'));
rFooter := Rect(0, DocEnd, Printer.PageWidth, Printer.PageHeight - MARGIN);
DrawText(Printer.Canvas.Handle,
PChar(IntToStr(Printer.PageNumber)),
length(IntToStr(Printer.PageNumber)),
rFooter,
DT_SINGLELINE or DT_CENTER or DT_BOTTOM);
end;
r.Left := MARGIN;
r.Top := MARGIN;
for i := 0 to sl.Count - 1 do
begin
r.Right := Printer.PageWidth - MARGIN;
r.Bottom := DocEnd;
s := sl.Strings[i];
if s = '' then s := ' ';
h := DrawText(Printer.Canvas.Handle, // Height of paragraph on paper
PChar(s),
length(s),
r,
DT_LEFT or DT_TOP or DT_WORDBREAK or DT_CALCRECT);
if r.Top + h >= DocEnd then
begin
Printer.NewPage;
if Numbering then
DrawText(Printer.Canvas.Handle,
PChar(IntToStr(Printer.PageNumber)),
length(IntToStr(Printer.PageNumber)),
rFooter,
DT_SINGLELINE or DT_CENTER or DT_BOTTOM);
r.Top := MARGIN;
r.Bottom := DocEnd;
end;
if h > Printer.PageHeight - 2*MARGIN then
raise Exception.Create('Line too long to fit on single page.');
DrawText(Printer.Canvas.Handle,
PChar(s),
length(s),
r,
DT_LEFT or DT_TOP or DT_WORDBREAK);
inc(r.Top, h);
end;
Printer.EndDoc;
finally
sl.Free;
end;
end;
Warning: The code above does not work if any single line in the text file is so wide that it cannot fit on a single paper (after it has been wrapped). I am too tired to fix that right now.
Option 2
A nasty trick is to use an invisible TRichEdit
to print.
procedure PrintTextFile(AOwner: TWinControl; const FileName: string);
begin
with TRichEdit.Create(nil) do
try
Visible := false;
Parent := AOwner;
Lines.LoadFromFile(FileName);
with TPrintDialog.Create(nil) do
try
if Execute then
Print(FileName);
finally
Free;
end;
finally
Free;
end;
end;
I advice against it, since it is a bit too nasty.
这篇关于需要帮助用delphi打印文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!