Click here to Skip to main content
11,928,403 members (44,059 online)
Click here to Skip to main content
Add your own
alternative version


80 bookmarked

Smart Device Print Engine for Compact Framework

, 19 Apr 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Printing support for .NET Compact Framework.


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.


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.


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

// Create page with size parameters

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 PageWidthDescription : Page width.
Signature : public int PageHeightDescription : Page height.
Signature : public string PortDescription : 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 PortDescription : COM port number.
Signature : public uint BaudRateDescription : 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.


  • Version 1.0 - First release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Orkun GEDiK
Software Developer (Senior) ASTRON
Turkey Turkey
I'm working at Yasar Holding IT department as senior SAP R/3 BASIS and Development Consultant and system integrator. I have experience on C/C++ for 6 years. Implementing and inviting new technologies is most biggest part of my life. Object oriented paradigm is a philosophy of my vision.

You may also be interested in...

Comments and Discussions

QuestionPersian Characters Pin
Member 254319516-Sep-15 5:53
memberMember 254319516-Sep-15 5:53 
QuestionWhat about print a image? Pin
Sumon152423-Nov-14 23:26
memberSumon152423-Nov-14 23:26 
QuestionQuestion on port. Pin
Member 111281077-Oct-14 3:55
memberMember 111281077-Oct-14 3:55 
Questionquestion Pin
Member 1110422723-Sep-14 8:59
memberMember 1110422723-Sep-14 8:59 
SuggestionNice work and idea for starting a PrintEngine Pin
hjgode17-Sep-14 20:11
memberhjgode17-Sep-14 20:11 
QuestionBarkod Basmak Pin
nihaterdil20-Dec-12 4:52
membernihaterdil20-Dec-12 4:52 
QuestionNot Printing On Emulator Pin
webber9913-Dec-12 6:47
memberwebber9913-Dec-12 6:47 
AnswerRe: Not Printing On Emulator Pin
S.VDavis20-Dec-12 7:05
memberS.VDavis20-Dec-12 7:05 
GeneralRe: Not Printing On Emulator Pin
Member 111281077-Oct-14 3:22
memberMember 111281077-Oct-14 3:22 
QuestionHow to print Chinese character Pin
willsoft_jin6-Mar-12 4:27
memberwillsoft_jin6-Mar-12 4:27 
QuestionTürkçe Karakterler Pin
yamanbas20-Oct-11 6:37
memberyamanbas20-Oct-11 6:37 
GeneralNot Printing Pin
rakesh.hosagoudar25-May-11 22:23
memberrakesh.hosagoudar25-May-11 22:23 
GeneralTurkish Charset / Charset Settings / Türkçe Karakter [modified] Pin
tetrat1-Jul-10 22:32
membertetrat1-Jul-10 22:32 
riccardo amico3-Feb-10 7:21
memberriccardo amico3-Feb-10 7:21 
GeneralUSB printer in windows CE Pin
Sajjad Izadi28-Nov-09 9:03
memberSajjad Izadi28-Nov-09 9:03 
GeneralPrinting using Bluetooth printer Pin
marcin.klus26-May-09 11:27
membermarcin.klus26-May-09 11:27 
Generalmobile printing Pin
EBall3-Apr-09 11:33
memberEBall3-Apr-09 11:33 
GeneralPrint logo or image Pin
Member 194710423-Feb-09 3:07
memberMember 194710423-Feb-09 3:07 
GeneralPrint logo Pin
Member 194710423-Feb-09 3:07
memberMember 194710423-Feb-09 3:07 
Questionprinter Pin
osanchez8426-Dec-08 10:37
memberosanchez8426-Dec-08 10:37 
GeneralAumentar tamaño de la letra Pin
lycan07143-Dec-08 5:23
memberlycan07143-Dec-08 5:23 
GeneralChange font style, size ; Pin
fab.as18-Sep-08 2:15
memberfab.as18-Sep-08 2:15 
GeneralGreat work. Thanks!!!! Pin
Member 46002623-Sep-08 3:09
memberMember 46002623-Sep-08 3:09 
ramana venkata paidi28-Aug-08 22:47
memberramana venkata paidi28-Aug-08 22:47 
ziaddtahaa8-Feb-11 1:02
memberziaddtahaa8-Feb-11 1:02 
ziaddtahaa8-Feb-11 1:04
memberziaddtahaa8-Feb-11 1:04 
QuestionCan I connect to wireless printer by using thid application? Pin
Member 39254805-Jun-08 22:49
memberMember 39254805-Jun-08 22:49 
modii62023-Jun-08 22:28
membermodii62023-Jun-08 22:28 
Generalwat's the code to print a document through a device application Pin
arpit05225gcse29-May-08 19:28
memberarpit05225gcse29-May-08 19:28 
Questionhow to put logo in the printed page Pin
reham24-Mar-08 5:45
memberreham24-Mar-08 5:45 
GeneralGreat work! Pin
Win32nipuh19-Feb-08 6:19
memberWin32nipuh19-Feb-08 6:19 
Generalturkish characters problem Pin
Member 435985018-Dec-07 3:27
memberMember 435985018-Dec-07 3:27 
GeneralSending Escape Characters to Printer Pin
torpilla22727-Nov-07 6:17
membertorpilla22727-Nov-07 6:17 
GeneralPrint good. But how? [modified] Pin
0Umix25-Nov-07 20:36
member0Umix25-Nov-07 20:36 
GeneralQuery in using SmartDevicePrintEngine class (CFPrinting.dll) Pin
nikeyhere2-Nov-07 1:32
membernikeyhere2-Nov-07 1:32 
GeneralRe: Query in using SmartDevicePrintEngine class (CFPrinting.dll) Pin
Orkun GEDiK2-Nov-07 11:48
memberOrkun GEDiK2-Nov-07 11:48 
QuestionHow to modify these code to print Traditional/Simplified word Pin
kueihungyeh12-Sep-07 8:41
memberkueihungyeh12-Sep-07 8:41 
QuestionLand Scape Printing Pin
jon555512-Sep-07 7:49
memberjon555512-Sep-07 7:49 
Generalthere is an error Pin
melodytr9-Aug-07 6:30
membermelodytr9-Aug-07 6:30 
Questionwindows ce 4.2? Pin
melodytr6-Aug-07 10:48
membermelodytr6-Aug-07 10:48 
QuestionPictures? Pin
torpilla2273-Aug-07 8:37
membertorpilla2273-Aug-07 8:37 
AnswerRe: Pictures? Pin
Orkun GEDiK4-Aug-07 2:10
memberOrkun GEDiK4-Aug-07 2:10 
QuestionRe: Pictures? Pin
acoulter16-Aug-07 7:05
memberacoulter16-Aug-07 7:05 
AnswerRe: Pictures? Pin
Anagnostes29-Oct-07 11:42
memberAnagnostes29-Oct-07 11:42 
GeneralRe: Pictures? Pin
acoulter30-Oct-07 0:40
memberacoulter30-Oct-07 0:40 
QuestionTesting the code on Pocket PC 2002 emulator Pin
Coral281-Aug-07 9:21
memberCoral281-Aug-07 9:21 
AnswerRe: Testing the code on Pocket PC 2002 emulator Pin
Orkun GEDiK1-Aug-07 22:41
memberOrkun GEDiK1-Aug-07 22:41 
GeneralRe: Testing the code on Pocket PC 2002 emulator Pin
jyoti1216-May-10 22:57
memberjyoti1216-May-10 22:57 
Generalsource in [modified] Pin
vehbi emiroglu4-Jul-07 5:15
membervehbi emiroglu4-Jul-07 5:15 
GeneralExcellent Work Pin
acoulter3-Jul-07 1:36
memberacoulter3-Jul-07 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 20 Apr 2004
Article Copyright 2004 by Orkun GEDiK
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid