Click here to Skip to main content
Click here to Skip to main content

Print complex script text through serial port from Windows Mobile system

By , 4 Feb 2012
 
I have a pocket PC with Windows mobile as OS. With it, I have a printer Zebra MZ320. My communication protocol is Bluetooth.

I connected the Bluetooth printer to the COM4 of the Windows mobile device.
I want now to print non ASCII code. The Windows mobile cannot support that, but at least it can print images.
So you have to convert your text and everything into image (the design depends on you) and print it.
First, take some files from Zebra website, the SDK and the ZebraUtilities, it's free after free registration.
If your printer is not Zebra, you can probably find a similar product from the vendor website.
Install the ZebraUtilities.cab on your Mobile device.
Use the DDLs from the SDK to your project by adding reference.
 
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO.Ports;
using System.Windows.Forms;
using ZSDK_API.ApiException;
using ZSDK_API.Comm;
using ZSDK_API.Printer;
 
namespace SmartFarsiTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void MenuExitClick(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void MenuPrintClick(object sender, EventArgs e)
        {
            CreateImage();
          PrintOverSerial();
        }
 
        private void PrintOverSerial()
        {
            try
            {
                // Instantiate connection
                ZebraPrinterConnection thePrinterConn = new SerialPrinterConnection("COM4", 9600, 8, Parity.None, StopBits.One, Handshake.None);
 
                // Open the connection - physical connection is established here.
                thePrinterConn.Open();
                var printer = ZebraPrinterFactory.GetInstance(thePrinterConn);
 
                printer.GetGraphicsUtil().PrintImage(@"\My Documents\LastPrint.jpg",50,50);
                thePrinterConn.Close();
            }
            catch (ZebraPrinterConnectionException e)
            {
                MessageBox.Show(e.StackTrace);
            }
        }
 
        private static void CreateImage()
        {
            const float TicketWidth = 320;
//Prepare 10 areas or as you want
            var PrintableArea1 = new RectangleF(0, 7, TicketWidth, 20);
            var PrintableArea2 = new RectangleF(0, PrintableArea1.Bottom, TicketWidth, 20);
            var PrintableArea3 = new RectangleF(0, PrintableArea2.Bottom, TicketWidth, 20);
            var PrintableArea4 = new RectangleF(0, PrintableArea3.Bottom, TicketWidth, 20);
            var PrintableArea5 = new RectangleF(0, PrintableArea4.Bottom, TicketWidth, 20);
            var PrintableArea6 = new RectangleF(0, PrintableArea5.Bottom, TicketWidth, 20);
            var PrintableArea7 = new RectangleF(0, PrintableArea6.Bottom, TicketWidth, 20);
            var PrintableArea8 = new RectangleF(0, PrintableArea7.Bottom, TicketWidth, 20);
            var PrintableArea9 = new RectangleF(0, PrintableArea8.Bottom, TicketWidth, 20);
            var PrintableArea10 = new RectangleF(0, PrintableArea9.Bottom,TicketWidth, 20);
 
            var objBrushForeColor = new SolidBrush(Color.Black);
            //back color
            var objBrushBackColor = new SolidBrush(Color.White);
 
            var objFont = new Font("Tahoma", 14, FontStyle.Regular);
            var objBitmap = new Bitmap(320, 300);
            var objGraphics = Graphics.FromImage(objBitmap);
 
            //draw a white rectangle
            objGraphics.FillRectangle(objBrushBackColor, 0, 0, 320, 300);
 
            //this is String format is for right to left text
//you can omit it for normal left to right
//of course you don't need to use this trick for ltr
            var sf = new StringFormat
                         {
                             Alignment = StringAlignment.Far,
                             FormatFlags = StringFormatFlags.NoWrap,
                             LineAlignment = StringAlignment.Far
                         };
 
            //draw the text
            objGraphics.DrawString("سفارش خرید کالا ", objFont, objBrushForeColor, PrintableArea1,sf);
            objGraphics.DrawString("شرکت پیام سامان", objFont, objBrushForeColor, PrintableArea2,sf );
            objGraphics.DrawString("تست", objFont, objBrushForeColor, PrintableArea3,sf);
            objGraphics.DrawString("زرشک پولو", objFont, objBrushForeColor, PrintableArea4,sf);
            objGraphics.DrawString("ماهی", objFont, objBrushForeColor, PrintableArea5,sf);
            objGraphics.DrawString("بادمجون", objFont, objBrushForeColor, PrintableArea6, sf);
            objGraphics.DrawString("کیبورد", objFont, objBrushForeColor, PrintableArea7, sf);
            objGraphics.DrawString("ماوس", objFont, objBrushForeColor, PrintableArea8, sf);
            objGraphics.DrawString("زیتون", objFont, objBrushForeColor, PrintableArea9, sf);
            objGraphics.DrawString("هویج", objFont, objBrushForeColor, PrintableArea10, sf);
            objGraphics.DrawLine(new Pen(Color.Black, 2), 0, (int)PrintableArea10.Bottom, 320, (int)PrintableArea10.Bottom);
 
            objBitmap.Save(@"\My Documents\LastPrint.jpg", ImageFormat.Jpeg);
        }
    }
}

License

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

About the Author

Ralala Yves
Software Developer
Madagascar Madagascar
Member
I'm from Madagascar and live in Iran.
I read Mathematics to the University of Madagascar.
And now I'm working to the company Payam-Saman in Iran.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionerror : IOExceptionmemberwillsoft_jin6 Mar '12 - 18:41 
AnswerRe: error : IOExceptionmemberRalala Yves6 Mar '12 - 19:11 
GeneralRe: error : IOException [modified]memberwillsoft_jin7 Mar '12 - 1:53 
GeneralRe: error : IOExceptionmemberRalala Yves7 Mar '12 - 1:58 
GeneralRe: error : IOExceptionmemberwillsoft_jin7 Mar '12 - 2:25 
GeneralRe: error : IOExceptionmemberRalala Yves7 Mar '12 - 17:56 
GeneralRe: error : IOExceptionmemberwillsoft_jin7 Mar '12 - 20:07 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 4 Feb 2012
Article Copyright 2012 by Ralala Yves
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid