1 Attachment(s)
Sending a text file to the printer using win32 API
Attachment 32213Hi all,
I got a project in that,Text file creating by application should get in to print.So i want to send the text file to the printer using win 32 API.I don't have any idea about Printer device driver.Please Give some idea.i created Print Dialog Box using PRINTDLG Structure,After that while click on PrintButton the text file shuold get print.
Thanks& Regards,
Manju
Re: Sending a text file to the printer using win32 API
Re: Sending a text file to the printer using win32 API
Thanks for your great comment.I tryed that sample code with PrimoPDF and i can generate a pdf file also.Next my question is how to pass a file(text )content into the TextOut function.Any help would be greatly appreciated.
Thanks &Regards
Manju
Re: Sending a text file to the printer using win32 API
You don't pass a full text file content into TextOut(). You need to read the text file line and line and pass each line to TextOut() specifying the appropriate x, y coordinates for the position of the individual text line in the device context. x would usually be a fixed constant for each line, and the y value would be incremented for each line by the height of the selected font in the device context.
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
2kaud
You don't pass a full text file content into TextOut(). You need to read the text file line and line and pass each line to TextOut() specifying the appropriate x, y coordinates for the position of the individual text line in the device context. x would usually be a fixed constant for each line, and the y value would be incremented for each line by the height of the selected font in the device context.
I tryed with wcstok to split the text file content line by line .
Code:
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = (LPCTSTR)TEXT("Printing Test");
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR)TEXT( "RAW");
HDC hPrinter=pd.hDC;
//Reading data from text file and storing into a location pStore
char cRead=0;
DWORD uiRead=1 ;
HANDLE FHandle=CreateFile(cFileName1,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
LARGE_INTEGER filesize;
if(FHandle!=INVALID_HANDLE_VALUE)
BOOL b=GetFileSizeEx(FHandle,&filesize);
////Allocate required space to keep the file as such.
TCHAR *pStore = new TCHAR[filesize.u.LowPart];
TCHAR *pTemp1 = pStore;
while ( uiRead > 0 )
{
if (!( ReadFile ( FHandle, &cRead, 1,&uiRead,NULL) ) )
return false;
*pTemp1 = cRead;
pTemp1++;
}
CloseHandle(FHandle);
//Splitting line by line and texting out
if(StartDoc(hPrinter,&di))
{
if(StartPage(hPrinter))
{
TCHAR seps[]=_T("\t\n");
TCHAR *token1 = NULL;
TCHAR *next_token1 = NULL;
int iI=0;
token1 = wcstok_s( pStore, seps, &next_token1);
while (token1 != NULL)
{
// While there are tokens in "string"
if(!(TextOut(hPrinter,100,100*iI,token1,wcslen(token1))))
{
int res=GetLastError();
return FALSE;
}
token1 = wcstok_s( NULL, seps, &next_token1);
iI++;
}
}
EndPage(hPrinter);
}
EndDoc(hPrinter);
This code is working only for 1 page and Allignment is proper only for PrimoPDF.I have some doubt
1.How to change the Printer DC's Scaling factor,font etc.If i changing tht Scaling factor. will it work with all printer?
2.What modification i have to do on
Code:
if(!(TextOut(hPrinter,100,100*iI,token1,wcslen(token1))))
3.How to print more than 1 page?
Please Help me
Thanks & Regards
manju
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
manjut19
...
This code is working only for 1 page ...
It is because you call StartPage/EndPage only once.
Re: Sending a text file to the printer using win32 API
The easiest way to read a text file line by line is something like this
Code:
#include <fstream>
#include <string>
using namespace std;
ifstream inf("file name");
string line;
while (inf.good()) {
getline(inf, line);
//process line. Use line.c_str() to get a c-style null terminated string
}
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
VictorN
It is because you call StartPage/EndPage only once.
If iam calling StartPage/EndPage multiple time multiple page can print.But i don't know howmany lines will be there in 1 page.How to get that number?Is it printer depended or programatically setting one?
Re: Sending a text file to the printer using win32 API
Use GetDeviceCaps() with an index of VERTRES. This will return the height in pixels of the printable area of the page. When the ypos to which you are printing exceeds this area (or exceeds a fixed amount below this value), then start a new page.
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
2kaud
The easiest way to read a text file line by line is something like this
Code:
#include <fstream>
#include <string>
using namespace std;
ifstream inf("file name");
string line;
while (inf.good()) {
getline(inf, line);
//process line. Use line.c_str() to get a c-style null terminated string
}
How to convert std::string to LPCWSTR.
Code:
if(!(TextOut(hPrinter,100,100*iI,line,wcslen(line))))
Re: Sending a text file to the printer using win32 API
Are you compiling as UNICODE or ASCII?
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
2kaud
are you compiling as unicode or ascii?
unicode
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
manjut19
How to convert std::string to LPCWSTR.
Code:
if(!(TextOut(hPrinter,100,100*iI,line,wcslen(line))))
Use TextOutA() which takes LPCSTR rather than LPCWSTR if compiled as UNICODE.
Re: Sending a text file to the printer using win32 API
Quote:
Originally Posted by
2kaud
Use TextOutA() which takes LPCSTR rather than LPCWSTR if compiled as UNICODE.
Thank you for your kind replay.Its working now
Thanks & Regards
Manju