Click here to Skip to main content
6,630,586 members and growing! (15,670 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

Smart Device Print Engine for Compact Framework

By Orkun GEDiK

Printing support for .NET Compact Framework
C#, Windows, .NET CF, .NET, WinMobile2003VS.NET2003, Dev
Posted:15 Apr 2004
Updated:19 Apr 2004
Views:189,700
Bookmarked:65 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
40 votes for this article.
Popularity: 7.09 Rating: 4.43 out of 5
2 votes, 5.0%
1

2
3 votes, 7.5%
3
9 votes, 22.5%
4
26 votes, 65.0%
5

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

  • Version 1.0 - First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Orkun GEDiK


Member
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.
Occupation: Software Developer (Senior)
Company: ASTRON
Location: Turkey Turkey

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 223 (Total in Forum: 223) (Refresh)FirstPrevNext
GeneralPrinting using Bluetooth printer Pinmembermarcin.klus11:27 26 May '09  
Generalmobile printing PinmemberEBall11:33 3 Apr '09  
GeneralPrint logo or image PinmemberMember 19471043:07 23 Feb '09  
GeneralPrint logo PinmemberMember 19471043:07 23 Feb '09  
Questionprinter Pinmemberosanchez8410:37 26 Dec '08  
GeneralAumentar tamaño de la letra Pinmemberlycan07145:23 3 Dec '08  
GeneralChange font style, size ; Pinmemberfab.as2:15 18 Sep '08  
GeneralGreat work. Thanks!!!! PinmemberMember 46002623:09 3 Sep '08  
QuestionSPDE PRINTING BMP IMAGE Pinmemberramana venkata paidi22:47 28 Aug '08  
GeneralCan I connect to wireless printer by using thid application? PinmemberMember 392548022:49 5 Jun '08  
GeneralBARCODE PRINTING THROUGH ASP.NET FROM SAP Pinmembermodii620222:28 3 Jun '08  
Generalwat's the vb.net code to print a document through a device application Pinmemberarpit05225gcse19:28 29 May '08  
Questionhow to put logo in the printed page Pinmemberreham5:45 24 Mar '08  
GeneralGreat work! PinmemberWin32nipuh6:19 19 Feb '08  
Generalturkish characters problem PinmemberMember 43598503:27 18 Dec '07  
GeneralSending Escape Characters to Printer Pinmembertorpilla2276:17 27 Nov '07  
GeneralPrint good. But how? [modified] Pinmember0Umix20:36 25 Nov '07  
GeneralQuery in using SmartDevicePrintEngine class (CFPrinting.dll) Pinmembernikeyhere1:32 2 Nov '07  
GeneralRe: Query in using SmartDevicePrintEngine class (CFPrinting.dll) PinmemberOrkun GEDiK11:48 2 Nov '07  
GeneralHow to modify these code to print Traditional/Simplified word Pinmemberkueihungyeh8:41 12 Sep '07  
QuestionLand Scape Printing Pinmemberjon55557:49 12 Sep '07  
Generalthere is an error Pinmembermelodytr6:30 9 Aug '07  
Generalwindows ce 4.2? Pinmembermelodytr10:48 6 Aug '07  
QuestionPictures? Pinmembertorpilla2278:37 3 Aug '07  
AnswerRe: Pictures? PinmemberOrkun GEDiK2:10 4 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Apr 2004
Editor: Nishant Sivakumar
Copyright 2004 by Orkun GEDiK
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project