开发者

Help needed about printing text file with delphi

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 any开发者_Python百科thing.

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.


Evidently, the default printer on your computer is the XPS-file generator. You would get the same behavior if you chose the "Print" command from the context menu of that file in Windows Explorer. Change your default printer to something else.


Set a default printer on your machine - make sure you have physical access and proper user rights to use it. The xps printer is the MS default print driver when nothing else is set.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜