-
January 2nd, 2014, 12:46 AM
#1
Sending a text file to the printer using win32 API
Test1.txtHi 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
Last edited by manjut19; January 2nd, 2014 at 12:56 AM.
-
January 2nd, 2014, 05:10 AM
#2
Re: Sending a text file to the printer using win32 API
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 3rd, 2014, 01:26 AM
#3
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
Last edited by manjut19; January 3rd, 2014 at 01:30 AM.
-
January 3rd, 2014, 06:40 AM
#4
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 4th, 2014, 01:25 AM
#5
Re: Sending a text file to the printer using win32 API
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
Last edited by manjut19; January 4th, 2014 at 01:28 AM.
-
January 4th, 2014, 03:25 AM
#6
Re: Sending a text file to the printer using win32 API
Originally Posted by manjut19
...
This code is working only for 1 page ...
It is because you call StartPage/EndPage only once.
Victor Nijegorodov
-
January 4th, 2014, 05:47 AM
#7
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
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 4th, 2014, 06:42 AM
#8
Re: Sending a text file to the printer using win32 API
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?
-
January 4th, 2014, 07:00 AM
#9
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 4th, 2014, 07:48 AM
#10
Re: Sending a text file to the printer using win32 API
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))))
-
January 4th, 2014, 08:01 AM
#11
Re: Sending a text file to the printer using win32 API
Are you compiling as UNICODE or ASCII?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 6th, 2014, 01:10 AM
#12
Re: Sending a text file to the printer using win32 API
Originally Posted by 2kaud
are you compiling as unicode or ascii?
unicode
-
January 6th, 2014, 06:04 AM
#13
Re: Sending a text file to the printer using win32 API
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 8th, 2014, 01:49 AM
#14
Re: Sending a text file to the printer using win32 API
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|