Sending a text file to the printer using win32 API
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Sending a text file to the printer using win32 API

  1. #1
    Join Date
    Apr 2013
    Posts
    77

    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 1st, 2014 at 11:56 PM.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Sending a text file to the printer using win32 API

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2013
    Posts
    77

    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 12:30 AM.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2013
    Posts
    77

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by 2kaud View Post
    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 12:28 AM.

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,299

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by manjut19 View Post
    ...
    This code is working only for 1 page ...
    It is because you call StartPage/EndPage only once.
    Victor Nijegorodov

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2013
    Posts
    77

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by VictorN View Post
    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?

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 2013
    Posts
    77

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by 2kaud View Post
    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))))

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Apr 2013
    Posts
    77

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by 2kaud View Post
    are you compiling as unicode or ascii?
    unicode

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by manjut19 View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Apr 2013
    Posts
    77

    Re: Sending a text file to the printer using win32 API

    Quote Originally Posted by 2kaud View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center