473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Send raw data to printer using C#

I have implemented the code to send raw data to printer using the below KB article.
http://support.microsoft.com/?kbid=322091

The receipt printer works for small strings but when I send large string data (250 lines), not all the information is printed correctly. Has anyone run into this issue before.

Please help !!
Apr 23 '12 #1
3 26184
RhysW
70
when you say not printed correctly what do you mean? is it not being printed? being printed but in the wrong place?, being printed off the edge of the paper? the more information you give the faster you can get help!
Apr 24 '12 #2
I meant that not all the data is printed. Some information is missing. For eg. - If I am printing an array of 100 items, then the receipt prints first 30-40 lines then it jumps of to printing last few records.

I am using bool SendStringToPrinter( string szPrinterName, string szString ) function.

I had debugged to find if complete string is passed to the above procedure call and it is correct. How can I find if correct byte value is passed when the below function is called.

pBytes = Marshal.StringToCoTaskMemAnsi(szString);

Thanks for your help!!
Apr 24 '12 #3
Plater
7,872 Expert 4TB
I use raw printer all the time without any trouble, but i made a few changes.
Expand|Select|Wrap|Line Numbers
  1. // SendBytesToPrinter()
  2. // When the function is given a printer name and an unmanaged array
  3. // of bytes, the function sends those bytes to the print queue.
  4. // Returns true on success, false on failure.
  5. private static bool SendBytesToPrinter(string DocName, string szPrinterName, IntPtr pBytes, Int32 dwCount)
  6. {
  7.     Int32 dwWritten = 0;
  8.     IntPtr hPrinter = new IntPtr(0);
  9.     DOCINFOA di = new DOCINFOA();
  10.     bool bSuccess = false; // Assume failure unless you specifically succeed.
  11.  
  12.     LastErrorNumber = 0;
  13.  
  14.     di.pDocName = DocName;// "My C#.NET RAW Document";
  15.     di.pDataType = "RAW";
  16.  
  17.     // Open the printer.
  18.     if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  19.     {
  20.     // Start a document.
  21.     if (StartDocPrinter(hPrinter, 1, di))
  22.     {
  23.       // Start a page.
  24.       if (StartPagePrinter(hPrinter))
  25.       {
  26.         // Write your bytes.
  27.         bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  28.         EndPagePrinter(hPrinter);
  29.       }
  30.       EndDocPrinter(hPrinter);
  31.     }
  32.     ClosePrinter(hPrinter);
  33.     }
  34.     // If you did not succeed, GetLastError may give more information
  35.     // about why not.
  36.     if (bSuccess == false)
  37.     {
  38.         LastErrorNumber = Marshal.GetLastWin32Error();
  39.     }
  40.     return bSuccess;
  41. }
  42. public static bool SendBytesToPrinter(string DocName, string szPrinterName, byte[] data)
  43. {
  44.   bool retval = false;
  45.   IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem(data.Length);//Allocate unmanaged memory
  46.   Marshal.Copy(data, 0, pUnmanagedBytes, data.Length);//Copy bytes into unmanaged memory
  47.   retval = SendBytesToPrinter(DocName, szPrinterName, pUnmanagedBytes, data.Length);//Send bytes to printer
  48.   Marshal.FreeCoTaskMem(pUnmanagedBytes);// Free the allocated unmanaged memory
  49.   return retval;
  50. }
  51.  
Using the sendbytes is my prefered way, but if you want a nice wrapper function for your strings:
Expand|Select|Wrap|Line Numbers
  1. public static bool SendASCIStringToPrinter(string DocName, string szPrinterName, string data)
  2. {//Using ASCII encoding might be wrong, you could try UTF8 if you're not happy with ASCII
  3.     return SendBytesToPrinter(DocName, szPrinterName, Encoding.ASCII.GetBytes(data));
  4. }
  5.  
Apr 27 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fatih BOY | last post by:
Hi, I want to send a report from a windows application to a web page like 'report.asp' Currently i can send it via post method with a context like local=En&Username=fatih&UserId=45&Firm=none...
0
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address,...
13
by: Susan Beebe | last post by:
I have downloaded the code described in Microsoft article Q154078. I am trying to send raw ZPL (zebra barcode printer) code from Microsoft access. It works just fine if I hard code the actual...
2
by: Crazyhorse | last post by:
Hi, I am trying to print labels to a zebra printer using the print document object in vb.net. Printing works fine to my local printer but with the Zebra, the data light indicator just flickers a...
5
by: trig | last post by:
Please help! I am an ICT teacher at a secondary school and my year 12 (AS Level) group need to create a website where data can be sent from a form to a Microsoft Access database. I am trying...
0
by: realgoggi | last post by:
Hello, I have a label printer connected to LPT1 on the server where IIS and the ASPX/C# code is running. From the web application I need to send data to the printer. Preferably directly,...
1
by: amcgary | last post by:
Hello, I am trying to print a System.Drawing.Printing.PrintDocument to a dot matrix printer using C# .NET. I have created an instance of the PrintDocument and create a event handler for the...
2
by: ajaxcoder | last post by:
Hi In my project i had a login form and i am trying to send the username and password to the server for authentication using xmlHttpRequest. Hence i am using POST request but i am unable to send...
5
sid0404
by: sid0404 | last post by:
Hi I am new to the visualstudio.net I am trying to develop an application which requires me to send data to a HTML webpage - http://patft.uspto.gov/netahtml/PTO/search-bool.html and the user...
8
by: ncsthbell | last post by:
Is there a way to change my default printer using vb? I have some reports I want to print to a certain printer and some to a different one. I have a routine that loops through all of the reports to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.