Board index » cppbuilder » sending an ascii file to the printer

sending an ascii file to the printer


2005-01-28 02:01:46 AM
cppbuilder50
Is there an easy way to dump an ascii file to a printer (in a windows
environment) without first putting it into a memo or richedit
component? I don't want to use the dos system comand. I'm not
concerned about formatting or determining paper size.
I've tried the ShellExecute(Handle, "print", "c:\\..., but this adds
page headers and uses different fonts.
thanks
AW
 
 

Re:sending an ascii file to the printer

This is sample (source: Bytamin-C) I saved in my archives
some years ago:
<quote>
Sending Data Directly to the Printer
Below is a quick sample of how to send data directly to the printer.
Remember to include winspool.h to your file.
NB: If you printer is directly connected to your computer (LPT1-LPT3) or is
a mapped networkprinter in which case you can use UNC paths to the printer
(eg. "\\MainServer\MatrixPrinter" which in C would be
"\\\\MainServer\\MatrixPrinter") you can just use simple file functions eg.
fopen("lpt1", "wt") or fopen("\\\\MainServer\\MatrixPrinter", "wt"). I hope
this helps those of you in despair ;)
Regards, Jeppe.
// szBuffer is the buffer which holds the data we wanna send to the printer
bool Print(char* szPrinterName, char* szBuffer) {
HANDLE hPrinter; // Printer handle
int iLen = strlen(szBuffer);// Length of the data to be sent
DWORD dwWritten; // How much was written to the printer
// Open printer and get a handle to it (which is saved in hPrinter)
if(OpenPrinter(szPrinterName, &hPrinter, NULL))
{
// Doc info
DOC_INFO_1 Info;
memset(&Info, 0, sizeof(Info));
Info.pDocName = "My PrintJob"; // Name of the Doc (which is shown in the
PrintManager)
Info.pOutputFile = NULL; // We're not going to write to a file so we set it
to NULL
Info.pDatatype = "RAW"; // Data are sent in RAW format (RAW and EMF are
supported by both
// Win9x and Win NT). Win NT also supports TEXT.
// Start doc
if(!StartDocPrinter(hPrinter, 1, (LPBYTE)&Info))
return false;
// Tell the printer we're starting on a new page
StartPagePrinter(hPrinter); // Write data to the printer
if(!WritePrinter(hPrinter, szBuffer, iLen, &dwWritten)) {
// An error happened while sending data to the printer.
// Use LastError to figure out what went wrong.
}
// Tell the printer we're done writing this page
EndPagePrinter(hPrinter);
// End the document
if(!EndDocPrinter(hPrinter))
return false;
// Close the printer
ClosePrinter(hPrinter);
return true;
}
else
{
return false;
}
}
</quote>
Recently, I made one quick (and ugly) version for myself, for
printing the contents of TListView. I give you it as is, withount
any changes.
You can use it with some modifications:
void TLanMsgMainForm::PrintItems( bool Selected )
{
ListView->Enabled = false;
int margin_pixels = 30;
Printer()->BeginDoc();
if ( Selected )
Printer()->Title = X(IDS_SER_MAIN_PRINTING_SELECTED);
else
Printer()->Title = X(IDS_SER_MAIN_PRINTING_ALL);
Printer()->Canvas->Font->Name = "Terminal";
Printer()->Canvas->Font->Size = 9;
Printer()->Canvas->Font->Charset = EASTEUROPE_CHARSET;
Printer()->Canvas->Font->Pitch = fpFixed;
int line = 0;
int real_line = 0;
for ( int i=0; i<ListView->Items->Count; i++ )
{
if ( Selected && !ListView->Items->Item[i]->Selected )
continue;
++line;
++real_line;
AnsiString LineToPrint =
Format( "%4s.%4s%10s%12s%11s%10s%25s%30s",
ARRAYOFCONST(( (IntToStr(real_line)),
(ListView->Items->Item[i]->SubItems->Strings[0]),
(ListView->Items->Item[i]->SubItems->Strings[1]),
(ListView->Items->Item[i]->SubItems->Strings[2]),
(ListView->Items->Item[i]->SubItems->Strings[3]),
(ListView->Items->Item[i]->SubItems->Strings[4]),
(ListView->Items->Item[i]->SubItems->Strings[5]),
(ListView->Items->Item[i]->SubItems->Strings[6]) ))
);
int TH = Printer()->Canvas->TextHeight( LineToPrint );
int top = margin_pixels + ( line * TH );
if ( ( top + 2*margin_pixels + TH )>= Printer()->PageHeight )
{
Printer()->NewPage();
line = 1;
top = margin_pixels + ( line * TH );
}
Printer()->Canvas->TextOut( margin_pixels, top, LineToPrint );
}
Printer()->NewPage();
Printer()->EndDoc();
ListView->Enabled = true;
}
Best regards,
Vladimir Stefanovic
"Arlie Winters" < XXXX@XXXXX.COM >wrote in message
Quote
Is there an easy way to dump an ascii file to a printer (in a windows
environment) without first putting it into a memo or richedit
component? I don't want to use the dos system comand. I'm not
concerned about formatting or determining paper size.
I've tried the ShellExecute(Handle, "print", "c:\\..., but this adds
page headers and uses different fonts.

thanks
AW

 

Re:sending an ascii file to the printer

Thanks Vladimir
Vladimir Stefanovic wrote:
Quote
This is sample (source: Bytamin-C) I saved in my archives
some years ago:

<quote>

Sending Data Directly to the Printer

Below is a quick sample of how to send data directly to the printer.
Remember to include winspool.h to your file.

NB: If you printer is directly connected to your computer (LPT1-LPT3) or is
a mapped networkprinter in which case you can use UNC paths to the printer
(eg. "\\MainServer\MatrixPrinter" which in C would be
"\\\\MainServer\\MatrixPrinter") you can just use simple file functions eg.
fopen("lpt1", "wt") or fopen("\\\\MainServer\\MatrixPrinter", "wt"). I hope
this helps those of you in despair ;)

Regards, Jeppe.

// szBuffer is the buffer which holds the data we wanna send to the printer
bool Print(char* szPrinterName, char* szBuffer) {
HANDLE hPrinter; // Printer handle
int iLen = strlen(szBuffer);// Length of the data to be sent
DWORD dwWritten; // How much was written to the printer

// Open printer and get a handle to it (which is saved in hPrinter)
if(OpenPrinter(szPrinterName, &hPrinter, NULL))
{
// Doc info
DOC_INFO_1 Info;
memset(&Info, 0, sizeof(Info));
Info.pDocName = "My PrintJob"; // Name of the Doc (which is shown in the
PrintManager)
Info.pOutputFile = NULL; // We're not going to write to a file so we set it
to NULL
Info.pDatatype = "RAW"; // Data are sent in RAW format (RAW and EMF are
supported by both
// Win9x and Win NT). Win NT also supports TEXT.
// Start doc
if(!StartDocPrinter(hPrinter, 1, (LPBYTE)&Info))
return false;
// Tell the printer we're starting on a new page
StartPagePrinter(hPrinter); // Write data to the printer
if(!WritePrinter(hPrinter, szBuffer, iLen, &dwWritten)) {
// An error happened while sending data to the printer.
// Use LastError to figure out what went wrong.
}
// Tell the printer we're done writing this page
EndPagePrinter(hPrinter);
// End the document
if(!EndDocPrinter(hPrinter))
return false;
// Close the printer
ClosePrinter(hPrinter);
return true;
}
else
{
return false;
}
}

</quote>

Recently, I made one quick (and ugly) version for myself, for
printing the contents of TListView. I give you it as is, withount
any changes.

You can use it with some modifications:

void TLanMsgMainForm::PrintItems( bool Selected )
{

ListView->Enabled = false;

int margin_pixels = 30;

Printer()->BeginDoc();

if ( Selected )
Printer()->Title = X(IDS_SER_MAIN_PRINTING_SELECTED);
else
Printer()->Title = X(IDS_SER_MAIN_PRINTING_ALL);

Printer()->Canvas->Font->Name = "Terminal";
Printer()->Canvas->Font->Size = 9;
Printer()->Canvas->Font->Charset = EASTEUROPE_CHARSET;
Printer()->Canvas->Font->Pitch = fpFixed;

int line = 0;
int real_line = 0;

for ( int i=0; i<ListView->Items->Count; i++ )
{

if ( Selected && !ListView->Items->Item[i]->Selected )
continue;

++line;
++real_line;

AnsiString LineToPrint =
Format( "%4s.%4s%10s%12s%11s%10s%25s%30s",
ARRAYOFCONST(( (IntToStr(real_line)),
(ListView->Items->Item[i]->SubItems->Strings[0]),
(ListView->Items->Item[i]->SubItems->Strings[1]),
(ListView->Items->Item[i]->SubItems->Strings[2]),
(ListView->Items->Item[i]->SubItems->Strings[3]),
(ListView->Items->Item[i]->SubItems->Strings[4]),
(ListView->Items->Item[i]->SubItems->Strings[5]),
(ListView->Items->Item[i]->SubItems->Strings[6]) ))
);

int TH = Printer()->Canvas->TextHeight( LineToPrint );
int top = margin_pixels + ( line * TH );

if ( ( top + 2*margin_pixels + TH )>= Printer()->PageHeight )
{
Printer()->NewPage();
line = 1;
top = margin_pixels + ( line * TH );
}

Printer()->Canvas->TextOut( margin_pixels, top, LineToPrint );

}

Printer()->NewPage();

Printer()->EndDoc();

ListView->Enabled = true;

}

Best regards,
Vladimir Stefanovic
"Arlie Winters" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>Is there an easy way to dump an ascii file to a printer (in a windows
>environment) without first putting it into a memo or richedit
>component? I don't want to use the dos system comand. I'm not
>concerned about formatting or determining paper size.
>I've tried the ShellExecute(Handle, "print", "c:\\..., but this adds
>page headers and uses different fonts.
>
>thanks
>AW
>
 

{smallsort}