Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I was faced with a problem while developing an application on Microsoft .NET Framework. The problem was .NET Framework�s printout support. NET framework has lack of printing and print preview support for some resource reasons. Therefore, I started to develop a print engine for Compact Framework. Actually, Pocket PC 2003 has APIs for printing support to Pocket PC 2003 through serial port which is useful for developers who want to develop an application on Compact Framework with printing support.

I used �P/Invoking Serial APIs in the Compact Framework� sample code on MSDN while developing serial communication class. As we know, dot-matrix printers uses COM or LPT port in order to communicate with the system. If you want to send data through COM port, you should use byte streaming.

In my case, I used following values;

BaudRate = 9600
Size = 8
Parity = 0
StopBits = 0

My class has print preview support, too. It is based on dynamically created window as below;

Pic. 1

I used multiline, locked textbox in order to show print preview. Text box object is so necessary for this job because of its nature. Actually, by forming a page from byte array; I can set text property to print buffer in order to show preview, easily. As you see, preview has BACK and NEXT buttons and page number label, too. All of the controls has been created dynamically.

Background

Smart Device Print Engine is formed by two main layers; print class and serial communication class. I will describe in detail and step by step, how it works.

Pic.2

Using the code

As I mentioned before, serial port communication is based on byte streaming. Therefore, you need to send data in a byte array. I used CEWriteFile API function in order to send data through COM port. You can put your data in a (n) sized array and send it to the port. For example :

int iNumberOfBytesRead=0;
uint iNumberOfBytesToRead=1024;
byte[] Output = new byte[1024];

CEWriteFile(hPort, Output, iNumberOfBytesToRead, 
  ref iNumberOfBytesRead, IntPtr.Zero); 

The API function mentioned, sends 1024 bytes data at a time which is necessary for minimizing trips between smart device and the serial port. For example you want to print �hello world� string from a line printer. How this is going to happen? Firstly, you need to create a page as below;

// Create SDPE instance

SDPE = new SmartDevicePrintEngine();

// Set COM port for SDPE

SDPE.Port="COM1:";

// Create page with size parameters

SDPE.CreatePage(36,46);

Code block mentioned above, creates a SmartDevicePrintEngine object instance and sets serial port number to COM1 and creates a logical page on memory which is 2 dimensional 36x46 byte array. Then it creates, CommDevice instance in order to communicate with printer. In CommDevice constructor, I set package sizes to 1024 as default value. That means, 1024 bytes will be send at a time for each package. Then, I created Device Control Block instance to set serial port properties. As we know, Compact Framework doesn�t support Abort() method for threads. Therefore, I used event mechanism in order to stop the thread execution. I used an ArrayList to store pages for print preview in SmartDevicePrintEngine constructor. These happen at Form_Load event. Now, we are ready to fill up the logical page with our strings.

How will I write my string and locate on paper? I developed WriteToLogicalPage() method since it puts a string on the print area. Method has two overloads which are aligned string and unaligned string. I will describe, how it works in the following sections.

Writing and locating a string on a page is quite easy. Look at the following code block for an example;

SDPE.WriteToLogicalPage(17, 2, "Hello World");

X and Y coordinates are starting from value 1. We want to put up �Hello World� string to 17, 2 location. First step in WriteToLogicalPage() method is checking page bounds and converting the string (�Hello World�) into PageBuffer which is a byte array. We created a logical page, set and located the string on page. Now, we need to call the most important method which is Print().

I initialized serial port by OpenPort, SetupCommSetDCB, SetConnectionTimeout and CreateCommThread. These methods initializes and opens serial port device communication. After initialization, we need to send byte array to serial port. Therefore, we should call Send() method. In Send() method, we need to send 1024 bytes package for each trip. At the end of this process, all of the data is sent to the serial port as 1024 bytes packages. We need to send form feed character (�0x0c�) for page break. If we have more than one page, we need to call NewPage() method which flushes old page and creates a new one. Now that, print process has completed successfully, we should close port by Close() method. That�s all.

Now, we will look at the preview support in Smart Device Print Engine. All steps are the same as Print() method until AddToPreview(). In AddToPreview() method; we need to create a StringBuilder object instance in order to concatenate rows for building a new preview page. We should insert new page into page array. NewPage() method, flushes old page and creates new one. Preview page is formed dynamically. It has a textbox in order to represent printout of pages.

I want to describe class methods and properties at below;

Class : SmartDevicePrintEngine

Signature : SmartDevicePrintEngine()
Description : Constructor.
Signature : public bool CreatePage(int Width, int Height)
Description : Creates a logical page which is 2 dimensional byte array.
Signature : public void WriteToLogicalPage(int x, int y, string Value)
Description : Writes a string to given location on page. String shouldn�t overflow from page.
Signature : public void WriteToLogicalPage(int x, int y, int Length, int Alignment, string Value)
Description : Writes a string to given location by alignment and fixed length on page. String shouldn�t overflow from page. For example, you may want to write numeric value which should be right aligned.
Signature : public void FormFeed()
Description : It is simple to understand. Sends 0x0c to serial port.
Signature : private string ReadLogicalPageByRow(int RowNumber)
Description : Reads byte array row by row and returns it in a string variable.
Signature : public void Dispose()
Description : Flushes objects from memory.
Signature : public void NewPage()
Description : Creates new page.
Signature : public void Print()
Description : Prints the document page or pages.
Signature : public void AddToPreview()
Description : Adds your preview pages into array in order to represent at print preview screen.
Signature : public void Preview()
Description : Calls preview window and represents preview of a page.
Signature : public int PageWidth
Description : Page width.
Signature : public int PageHeight
Description : Page height.
Signature : public string Port
Description : COM port number.


Class : CommDev

Signature : public CommDevice()
Description : Constructor
Signature : private IntPtr OpenPort()
Description : Opens a port in order to perform serial communication.
Signature : private void SetupComm()
Description : Sets buffer sizes.
Signature : private void SetConnectionTimeOut()
Description : Set connection time-out for serial communication.
Signature : private void SetDCB()
Description : Device control block
Signature : private void CreateCommThread()
Description : Creates a thread in order to processing events.
Signature : public void Init()
Description : Serial port initialization routines.
Signature : public void Send(char[,] Data)
Description : Sends byte array in 1024 bytes segments.
Signature : public void Send(byte Data)
Description : Sends a char to the serial port.
Signature : public void ClosePort()
Description : Closes COM port.
Signature : public void Dispose()
Description : Flushes objects.
Signature : public string Port
Description : COM port number.
Signature : public uint BaudRate
Description : Communication baud rate.

You can use Smart Device Print Engine in your continuous forms such as sales, order, invoice, etc... Class supports ASCII prints. But, I�m planning to add, Bluetooth support into class, in the near future. I hope it will be necessary for developers who want to add print-out support to their own projects. Also, this article gives information about serial communication through Pocket Devices.

I used Smart Device Print Engine class at mobile sales project which needs to printout invoices on the field. We are still using the class, successfully.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionPRINTING ON ZEBRA QL420, FROM SYMBOL PPT8800
riccardo amico
7:21 3 Feb '10  
I HAVE DEVELOPED AN APPLICATION THAT SHOULD PRINT INVOICES, ORDERS AND OTHER REPORTS, ON THE PLATFORM DESCRIBED ABOVE.
ALL IS READY TO USE IT BUT A DETAIL, THAT IS - IN FACT - THE VERY PRINTING

DO YOU THINK IT IS POSSIBLE TO FIND ( AND BUY ) A SIMPLE APPLICATION THAT PERMIT TO PRINT A SIMPLE TEXT FILE, FOR INSTANCE NAMED "TOPRINT.TXT", SO I COULD INSTALL IT BESIDE MY PROGRAM ON THE DEVICE

THANK YOU
BEST REGARDS
GeneralUSB printer in windows CE
Sajjad Izadi
9:03 28 Nov '09  
I have a USB printer connected to my windows CE device and i've got a similar problem with the job. do you know how i can manage my USB printer?
much appreciated.
GeneralPrinting using Bluetooth printer
marcin.klus
11:27 26 May '09  
Is it possible to use the described library to print on a Bluetooth printer? For example Citizen CMP.
Generalmobile printing
EBall
11:33 3 Apr '09  
I am unable to get any of the examples to work. I am looking for something to help me troubleshoot my printer/mobile device connection. I have a smartdevice app that works on a symbol mobile device. I need to print to a Zebra mobile printer but can't find a way to get the symbol and Zebra to communicate. I have a cable that hooks from the serial port of the symbol to the zebra.
GeneralPrint logo or image
Member 1947104
3:07 23 Feb '09  
Can you please give me code to preview and print logo or image from CFPrinting.SmartDevicePrintEngine?
I need a sample code to preview and print logo or image for CF.
GeneralPrint logo
Member 1947104
3:07 23 Feb '09  
Can you please give me code to preview and print logo or image from CFPrinting.SmartDevicePrintEngine?
I need a sample code to preview and print logo or image for CF.
Questionprinter
osanchez84
10:37 26 Dec '08  
excelent work!!!! but
How can i print with a bluetooth printer?
please help me
any idea
tHANKS
THIS I MY EMAIL osvaldo.sanchez84@hotmail.com
GeneralAumentar tamaño de la letra
lycan0714
5:23 3 Dec '08  
Tengo un problema a la hora de imprimir, y es que necesito aumentar el tamaño de la letra. Cuando imprimo solo tiene un tamaño fijo y es que estoy creando una aplicación para imprimir etiquetas de precios y el precio debe ser mas grande que los demás string como puedo hacer esto.
GeneralChange font style, size ;
fab.as
2:15 18 Sep '08  
How can I change the fonts style, size for a Canon BJC 85?
Thank you.
GeneralGreat work. Thanks!!!!
Member 4600262
3:09 3 Sep '08  
Mr. Orkun GEDiK, I want to thank you for your work. It was very helpful to me.

I had to convert the code to VB, and I used a converter from c# to vb.
(Unfortunately, I' m not a c programmer!)
I also had a problem with Greek characters, and I got over it by sending the escape characters. The SubPrint, is now as below (it's VB!):
(I used an o'Neil thermal printer with the Line Printer mode, so the hex values used are for this printer)


Public Sub Print()
' Init device
commDevice.Init()

commDevice.Send(&H1B) '
The ESC character
commDevice.Send(&H77) ' The 'w' character, indicating the font selection command for
'
the O'Neil Thermal Printer
commDevice.Send(&H69) '
The 'i' character, indicating the Greek font code for
' the O'Neil Thermal Printer

commDevice.Send(PageBuffer)
' End of page
FormFeed()
'
Close COM port
commDevice.ClosePort()
End Sub


Thanks again!

Demetris Mastoras
Athens, Greece
QuestionSPDE PRINTING BMP IMAGE
ramana venkata paidi
22:47 28 Aug '08  
Hi
Orkun GEDiK

I am trying to open BMP File and send through the SDPE to printer, but i am getting som junk print out
even i have send escape sequence of the printer given in printer manual
ESC * Select Bit image mode
ESC # Print logo
Please give me the sample code to use SPDE to print bmp files on serial printer.
Regards
Ramana
GeneralCan I connect to wireless printer by using thid application?
Member 3925480
22:49 5 Jun '08  
I want to connect to wireless printer from My Windows Mobile .
Can i use this application to do it.
I donot understand where to start by using this.
Please help me

Thank u in Advance

Poorna

GeneralBARCODE PRINTING THROUGH ASP.NET FROM SAP
modii6202
22:28 3 Jun '08  
hello sir my name is varun...i m working as asp.net programmer here....i need ur suggestion...i made a website on asp.net...in website i add a pplication in which i called a sap fuction in asp.net through RFC this is the barcode code...which is given below....but i fetch this code in text box when i print that code on barcode printer it gives the error....so please help me how i execute that code on barcode on client side through asp.net


{D0619,0991,0599|}
{AY;-03,0|}
{C|}
{XB00;0067,0047,9,3,03,0,0186,+0000000000,000,0,00|}
{XB01;0067,0334,9,3,03,0,0186,+0000000000,000,0,00|}
{PV00;0067,0284,0042,0064,B,00,B|}
{PV01;0067,0571,0042,0064,B,00,B|}
{PV02;0067,0319,0020,0034,B,00,B|}
{PV03;0067,0599,0020,0032,B,00,B|}
{RB00;PF3221187 A 21087089 46|}
{RB01;PF3221187 A 21087089 46|}
{RV00;PF3221187 A 21087089 46|}
{RV01;PF3221187 A 21087089 46|}
{RV02;ROLL NO.: D-37700 GROUP : 2A|}
{RV03;ROLL NO.: D-37700 GROUP : 2A|}
{XS;I,0001,0001C4200|}
{D0619,0991,0599|}
{AY;+07,0|}
{C|}
{LC;0033,0041,0976,0288,1,3|}
{PC000;0042,0066,05,05,E,00,B|}
{PC001;0042,0102,05,05,E,00,B|}
{PC002;0042,0142,05,05,E,00,B|}
{PC003;0042,0182,05,05,E,00,B|}
{PC004;0042,0223,05,05,E,00,B|}
{PC005;0042,0266,05,05,E,00,B|}
{PC006;0193,0075,05,05,M,00,B|}
{PC007;0193,0106,05,05,M,00,B|}
{PC008;0193,0146,05,05,M,00,B|}
{PC009;0193,0186,05,05,M,00,B|}
{PC010;0193,0226,05,05,M,00,B|}
{PC011;0193,0275,05,05,M,00,B|}
{PC012;0634,0076,05,05,E,00,B|}
{PC013;0753,0086,05,05,M,00,B|}
{PC014;0633,0113,05,05,E,00,B|}
{PC015;0753,0116,05,05,M,00,B|}
{PC016;0633,0152,05,05,E,00,B|}
{PC017;0753,0156,05,05,M,00,B|}
{PC018;0633,0192,05,05,E,00,B|}
{PC019;0753,0196,05,05,M,00,B|}
{PC020;0633,0236,05,05,E,00,B|}
{PC021;0753,0245,05,05,M,00,B|}
{LC;0621,0035,0621,0285,0,3|}
{LC;0034,0320,0977,0568,1,3|}
{PC022;0043,0346,05,05,E,00,B|}
{PC023;0043,0382,05,05,E,00,B|}
{PC024;0043,0422,05,05,E,00,B|}
{PC025;0043,0463,05,05,E,00,B|}
{PC026;0043,0503,05,05,E,00,B|}
{PC027;0043,0546,05,05,E,00,B|}
{PC028;0193,0355,05,05,M,00,B|}
{PC029;0193,0386,05,05,M,00,B|}
{PC030;0193,0425,05,05,M,00,B|}
{PC031;0193,0466,05,05,M,00,B|}
{PC032;0193,0506,05,05,M,00,B|}
{PC033;0193,0555,05,05,M,00,B|}
{PC034;0634,0356,05,05,E,00,B|}
{PC035;0754,0365,05,05,M,00,B|}
{PC036;0634,0392,05,05,E,00,B|}
{PC037;0753,0396,05,05,M,00,B|}
{PC038;0634,0432,05,05,E,00,B|}
{PC039;0753,0436,05,05,M,00,B|}
{PC040;0634,0472,05,05,E,00,B|}
{PC041;0753,0476,05,05,M,00,B|}
{PC042;0634,0516,05,05,E,00,B|}
{PC043;0753,0525,05,05,M,00,B|}
{PC044;0634,0266,05,05,E,00,B|}
{PC045;0634,0547,05,05,E,00,B|}
{PC046;0753,0275,05,05,M,00,B|}
{PC047;0753,0556,05,05,M,00,B|}
{LC;0621,0314,0621,0564,0,3|}
{RC000;Sort No.|}
{RC001;Shade No|}
{RC002;Shade Name|}
{RC003;Lot No.|}
{RC004;Width|}
{RC005;Gross Wt|}
{RC006;PF3221187|}
{RC007;S122770|}
{RC008;COFFEE GROUNDS|}
{RC009;30090550|}
{RC010;140.00 cm|}
{RC011;14.40|}
{RC012;Bale No|}
{RC013;21087089|}
{RC014;Roll No|}
{RC015;D-37700|}
{RC016;Metre |}
{RC017;46|}
{RC018;Grade|}
{RC019;A|}
{RC020;Group No|}
{RC021;2A|}
{RC022;Sort No.|}
{RC023;Shade No|}
{RC024;Shade Name|}
{RC025;Lot No.|}
{RC026;Width|}
{RC027;Gross Wt|}
{RC028;PF3221187|}
{RC029;S122770|}
{RC030;COFFEE GROUNDS|}
{RC031;30090550|}
{RC032;140.00 cm|}
{RC033;14.40|}
{RC034;Bale No|}
{RC035;21087089|}
{RC036;Roll No|}
{RC037;D-37700|}
{RC038;Metre |}
{RC039;46|}
{RC040;Grade|}
{RC041;A|}
{RC042;Group No|}
{RC043;2A|}
{RC044;Design No|}
{RC045;Design No|}
{RC046;|}
{RC047;|}
{XS;I,0001,0001C4201|}


please send me this information on my email id which is vmodi6202@gmail.com or v_modi6202@yahoo.co.in
i really need that soulution so please send as soon as possible
Generalwat's the vb.net code to print a document through a device application
arpit05225gcse
19:28 29 May '08  
can u plz tell me the code that how i can print a document from a device application.???
Questionhow to put logo in the printed page
reham
5:45 24 Mar '08  
Thank Soooo much about the great work , i tested it with windows CE 5 through bluethooth and work propery but now i have question ,

if we want to add logo to the printed page before print it like as any company logo in any invoice how we develop it ....

i'm in need of help


reham
GeneralGreat work!
Win32nipuh
6:19 19 Feb '08  
Thank you.

What about Bluetooth support?
It will be very interesting.

I am trying to find solution how to print from pocket PC to mobile printer via Bluetooth...

Regards,
Oleg
Generalturkish characters problem
Member 4359850
3:27 18 Dec '07  
hi Orkun,

first of all, congratulations for your work and share with us, it is an excelent work. but i have problems with writing turkish characters (s, S, I, i, g, G, etc...). although i convert to utf8, i can not accomplished.

any help will be greatly appreciated.

thanks in advance...
GeneralSending Escape Characters to Printer
torpilla227
6:17 27 Nov '07  
Hello Orkun,

I asked before on how to print graphics through SDPE and you said to use escape characters. We did that but now we have a long string to enter in in ordr for us to print our graphic. The problem is that we are always getting the PageOverflow error, and to fix that we increase the size of the page. However, after the graphic prints, the printer prints out a lot of blank space to make up for the size of the page we had to increase.

Is there any way to send our Escape command and string to the printer without writing to the logical page?

Thanks
GeneralPrint good. But how? [modified]
0Umix
20:36 25 Nov '07  
Please tell me. How...
... change font style, size;
... margins paper.

thasks.


-- modified at 0:34 Friday 30th November, 2007
GeneralQuery in using SmartDevicePrintEngine class (CFPrinting.dll)
nikeyhere
1:32 2 Nov '07  
Hi,
I have used this code in my smart device project. Here in this code the bytes that is to be printed is sent to the printer, there is no acknowledgement back to the printer, whether bytes have been printed or not.
This methods of printbyte and print works fine with no errors if , no printer attached, it works fine even if attached printer is not working, or if the paper in the printer is empty still method executes with no error. I tried to change the printebyte method with return type bool to check if successfully printed or not, but worthless efforts. Can u please help me out for this, how to acknowledge if the printer has successfully executed or not.
Thanks in advance.Smile


Nikey Patel

GeneralRe: Query in using SmartDevicePrintEngine class (CFPrinting.dll)
Orkun GEDiK
11:48 2 Nov '07  
Hi,

Bluetooth device sends a return code for feedback about what happened at printer side. To do that you need to read incoming bluetooth COM port on pocket pc device. You can find the port number on bluetooth manager. The operation performs as same as sending bytes to the printer, but you need to read data instead of write. I hope that this information is good enough for you.

Orkun GEDiK
Senior SAP R/3 Basis and Development Consultant
ASTRON

GeneralHow to modify these code to print Traditional/Simplified word
kueihungyeh
8:41 12 Sep '07  
Thanks your grate job.
This code work fine to print for "single byte" word,
but when I try to print "Double bytes" word it doesn't work.
Any idea about how to output double bytes word.

Thanks in advance.
QuestionLand Scape Printing
jon5555
7:49 12 Sep '07  
Plz guide me how to print landscape using this smart device print engine as soon as possible.

hiiii

Generalthere is an error
melodytr
6:30 9 Aug '07  
An unhandled exception of type 'System.TypeLoadException' occurred in System.Windows.Forms.dll

Additional information: Could not load type System.Windows.Forms.Form from assembly System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=B77A5C561934E089.

when i try direct print i get this error message.

have you got an idea about it

thanks
Generalwindows ce 4.2?
melodytr
10:48 6 Aug '07  
thank you for the source about printing.But i have a one question about ce printing.I am trying to run that code at windows ce 4.2 emulator but it doesn't work. What can i do about that ?

Can you help me please ?

Thank you.


Last Updated 20 Apr 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010