Click here to Skip to main content
11,630,646 members (75,614 online)
Click here to Skip to main content

Drawing a Postnet barcode

, 8 Feb 2006 CPOL 37.1K 533 18
Rate this:
Please Sign up or sign in to vote.
Drawing a Postnet barcode based on Neil Van Eps' and Rainman_63's articles
Sample Image - Postnet.gif


This article is based on Neil Van Eps' series of articles that show how to draw barcodes. In this article, the code will draw the 1D barcode of Postnet.

Postnet Basics

PostNet was developed by the United States Postal Service (USPS) to allow faster sorting and routing of mail. Postnet is the familiar, funny-looking barcode often printed on envelopes and business return mail.

Encode Table

This table indicates how to encode each digit of a PostNet barcode. Note that the encoding is expressed as "0" (half bar) or "1" (full bar).

0 11000
1 00011
2 00101
3 00110
4 01001
5 01010
6 01100
7 10001
8 10010
9 10100

Structure of Postnet Barcode

A PostNet barcode has the following structure:

  1. Frame bar, encoded as a single 1
  2. 5, 9, or 11 data characters properly encoded (see encoding table below)
  3. Check digit, encoded using encoding table below
  4. Final frame bar, encoded as a single 1

Computing the Checksum Digit

PostNet barcodes always include a modulo 10 check digit.

Before a PostNet symbol may be encoded, a checksum digit must be calculated to be subsequently appended to the end of the barcode. The checksum digit is a simple modulo 10 calculation of the sum of all the digits that are being coded.

The steps for calculating the check digit are as follows:

  1. Sum the total of all individual digits being encoded. For example, 80122-1905 would be calculated as 8 + 0 + 1 + 2 + 2 + 1 + 9 + 0 + 5 = 28.
  2. Determine the value that, when added to the sum, produces a value evenly divisible by 10. For example, 28 + 2 = 30, so the value is 2.
  3. The check digit is the value calculated in step 2. In this case the check digit is 2.

CEAN13 Class

My CEAN13 is based on the CBarcode class, that has been introduced in Neil Van Eps' series of articles.

Here is the CPostnet class declaration. It's implemented to draw a Postnetbarcode.

class CRationalCodabar : public CBarcode  
 void DrawBitmap();
 void BitmapToClipboard();
 virtual ~CRationalCodabar();

 CString RetrievePattern(char c);
 void DrawPattern(CString csPattern);

CPostnet::DrawBitmap() Details

void CPostnet::DrawPattern( CString csPattern )
 int   i,nXPixel,nYPixel;
 CDC   oDC;

 // attach to the device context

 // initialize X pixel value
 nXPixel = m_nStartingXPixel;
 for (i=0;i<csPattern.GetLength();i++)
 {  // X value for loop
  for (nXPixel=m_nStartingXPixel;nXPixel<m_nStartingXPixel+m_nNarrowBarPixelWidth;
    // if this is a bar
   if (csPattern.GetAt(i)=='b')
    for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight;nYPixel++)
    for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight/2;nYPixel++)

   for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight;nYPixel++)

  // advance the starting position
  m_nStartingXPixel+= 2*m_nNarrowBarPixelWidth;

 // detach from the device context

Other Resources

Here are some resources about barcodes:


  • 9th February, 2006: Initial post


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


About the Author

Iven Xu
Web Developer
China China
My home page:

You may also be interested in...

Comments and Discussions

Questionthanks ! you can also try this Pin
jacaboo15-Aug-13 16:08
memberjacaboo15-Aug-13 16:08 
QuestionPostnet barcode control Pin
Zacbr29-Apr-12 14:50
memberZacbr29-Apr-12 14:50 
BugChecksum problem Pin
axisback19-Nov-11 3:31
memberaxisback19-Nov-11 3:31 
General[Message Deleted] Pin
cabbage_kongxin10-Feb-06 19:17
membercabbage_kongxin10-Feb-06 19:17 
GeneralRe: Oh my god Pin
Iven Xu11-Feb-06 1:07
memberIven Xu11-Feb-06 1:07 
GeneralRe: Oh my god Pin
Ravi Bhavnani11-Feb-06 6:53
memberRavi Bhavnani11-Feb-06 6:53 
QuestionDo you happen to know... Pin
Pandele Florin8-Feb-06 22:07
memberPandele Florin8-Feb-06 22:07 
AnswerRe: Do you happen to know... Pin
Iven Xu11-Feb-06 1:28
memberIven Xu11-Feb-06 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.150723.1 | Last Updated 9 Feb 2006
Article Copyright 2006 by Iven Xu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid