Thema: Delphi .prn dateien ausdrucken

Einzelnen Beitrag anzeigen

Benutzerbild von Flocke
Flocke

Registriert seit: 9. Jun 2005
Ort: Unna
1.172 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#2

Re: .prn dateien ausdrucken

  Alt 19. Okt 2006, 14:36
In meiner PrintToFile-Unit hab ich's so gelöst, das sollte funktionieren:
Delphi-Quellcode:
{ This function sends the given file to the currently selected printer
  (Delphi's "Printer" object).
}

procedure SendFileToPrinter(const Filename: string; const Docname: string = '');
var
  hPrinter, hFile: THandle;
  Info: TDocInfo1;
  Buffer: array [0 .. 4095] of char;
  NumBytes: cardinal;

  { This function returns the device name of the currently selected printer
    (Delphi's "Printer" object).
  }

  function GetPrinterDeviceName: string;
  var
    szDevice, szDriver, szPort: array [0 .. 1023] of char;
    hDevMode: THandle;
  begin
    Printer.GetPrinter(szDevice, szDriver, szPort, hDevMode);
    Result := StrPas(szDevice);
  end;

begin
  if not OpenPrinter(PChar(GetPrinterDeviceName), hPrinter, nil) then
    RaiseLastOsError;

  try
    if Docname <> 'then
      Info.pDocName := PChar(Docname)
    else
      Info.pDocName := PChar(Filename);
    Info.pOutputFile := nil;
    Info.pDatatype := nil;
    if StartDocPrinter(hPrinter, 1, @Info) = 0 then
      RaiseLastOsError;

    try
      if not StartPagePrinter(hPrinter) then
        RaiseLastOsError;

      try
        hFile := CreateFile(PChar(Filename), GENERIC_READ, FILE_SHARE_READ, nil,
                   OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
        if hFile = INVALID_HANDLE_VALUE then
          RaiseLastOsError;

        try
          repeat
            if not ReadFile(hFile, Buffer, SizeOf(Buffer), NumBytes, nil) then
              RaiseLastOsError;

            if NumBytes > 0 then
              if not WritePrinter(hPrinter, @Buffer[0], NumBytes, NumBytes) then
                RaiseLastOsError;
          until NumBytes = 0;
        finally
          CloseHandle(hFile);
        end;
      finally
        EndPagePrinter(hPrinter);
      end;
    finally
      EndDocPrinter(hPrinter);
    end;
  finally
    ClosePrinter(hPrinter);
  end;
end;
Benutzt den gerade eingestellten Drucker aus "Printers.pas". Alternativ musst du nur die Zeile mit OpenPrinter ändern.

// Edit: Eben schnell noch aus der Funktion eine Prozedur gemacht.
Volker
Besucht meine Garage
Aktuell: RtfLabel 1.3d, PrintToFile 1.4
  Mit Zitat antworten Zitat