473,809 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Send raw data to printer using C#

2 New Member
I have implemented the code to send raw data to printer using the below KB article.
http://support.microso ft.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 26438
RhysW
70 New Member
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
harini19
2 New Member
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 SendStringToPri nter( 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.StringT oCoTaskMemAnsi( szString);

Thanks for your help!!
Apr 24 '12 #3
Plater
7,872 Recognized Expert Expert
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
20753
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 But the problem occures when i want to send a data with & sign (i.e: Firm=F&B). I try to solve this problem with using boundary, but i failed. Any idea!?
0
3037
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, to HIS port 8801. This means that I need to bind my sender socket to (myaddress,8801), use it for the send, then shut it down and close it. Then I want to wait, say 30 seconds, and do it all over again. Naturally, the socket's ReuseAddress...
13
21716
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 data in the string being sent to the printer. However, if I prompt the user for data or if I insert data from a particular record/field in a form Access crashes/closes on its own. I have included the code below, if anyone has any suggestions,...
2
24408
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 couple of times and nothing else happens? Any help on this would be greatly appreciated Thanks
5
6087
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 to do this in my own time and then hopefully teach them how to do it. I have installed IIS on my machine and have followed a few web
0
1492
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, mimicking the command line "type label.prt > lpt1:" as this is what I have working now, after tuning and altering the raw print data (it is really basic, less than 500 bytes data, close to clear text, per label). Is there any way to do this? Please,...
1
5430
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 PrintPage property of the PrintDocument. PrintDocument document = new PrintDocument(); document.PrintPage += new PrintPageEventHandler(pd_PrintInvoicePage); document.Print(); However, when I try to print to the dot matrix printer the printer...
2
4222
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 data. I tried sending using GET request and GET is working fine. Here is code snippet function postRequest() { \n\ if(window.ActiveXObject) \n\ xmlHTTPObj = new ActiveXObject('Microsoft.XMLHTTP'); \n\ else if (window.XMLHttpRequest) \n\
5
12874
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 sendsd the data to the webpage and receives the response to that particular values from the webpage. I am trying to use httpwebrequest for this purpose, but I am receiving errors, kindly let me know where I am going wrong. When I llok at the...
8
19134
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 print them so I would like to be able to change the default printer, print the odd reports and then change it back to the default printer after. I am running Access 2000.
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.