I do some work for Universities, some of which use codabar for their libraries so I've implemented it and included it below. I've got an works internal project that I'm using pdfsharp extensively for, so I'm trying to give a little back
It's based on the code39 code with a few alterations to cope with the variable thickness of the characters in codabar so if it says code39 anywhere I just missed it.
All of the characters read correctly using my Quick Check 810 but a few websites disagree with the definition of the '+' code that I've used but every reader that I've tried have read barcodes produced with this correctly. I've also compared them to the results with online generators and they match.
Code:
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Klaus Potzesny (mailto:Klaus.Potzesny@pdfsharp.com)
//
// Copyright (c) 2005-2009 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;
namespace PdfSharp.Drawing.BarCodes
{
/// <summary>
/// Imlpementation of the codabar bar code.
/// </summary>
public class Codabar : ThickThinBarCode
{
/// <summary>
/// Initializes a new instance of Codabar.
/// </summary>
public Codabar()
: base("", XSize.Empty, CodeDirection.LeftToRight)
{
}
/// <summary>
/// Initializes a new instance of Codabar.
/// </summary>
public Codabar(string code)
: base(code, XSize.Empty, CodeDirection.LeftToRight)
{
}
/// <summary>
/// Initializes a new instance of Codabar.
/// </summary>
public Codabar(string code, XSize size)
: base(code, size, CodeDirection.LeftToRight)
{
}
/// <summary>
/// Initializes a new instance of Codabar.
/// </summary>
public Codabar(string code, XSize size, CodeDirection direction)
: base(code, size, direction)
{
}
/// <summary>
/// Returns an array of size 7 that represents the thick (true) and thin (false) lines and spaces
/// representing the specified digit.
/// </summary>
/// <param name="ch">The character to represent.</param>
private static string ThickThinLines(char ch)
{
return Lines["0123456789-$:/.+ABCD".IndexOf(ch)];
}
static string[] Lines = new string[]
{
"0000011", // '0'
"0000110", // '1'
"0001001", // '2'
"1100000", // '3'
"0010010", // '4'
"1000010", // '5'
"0100001", // '6'
"0100100", // '7'
"0110000", // '8'
"1001000", // '9'
"0001100", // '-'
"0011000", // '$'
"1000101", // ':'
"1010001", // '/'
"1010100", // '.'
"0010101", // '+'
"0011010", // 'A'
"0101001", // 'B'
"0001011", // 'C'
"0001110", // 'D'
};
/// <summary>
/// Calculates the thick and thin line widths,
/// taking into account the required rendering size.
/// </summary>
internal override void CalcThinBarWidth(BarCodeRenderInfo info)
{
string bars = "";
foreach (char ch in startChar + text + endChar)
{
bars += ThickThinLines(ch); }
// 1's represent a thick gap or bar, 0 is thin gap or bar
int thinbars = bars.Replace("1", "").Length;
int thickbars = bars.Replace("0", "").Length + text.Length + 1; //add thick gap for each character in the code and 1 extra for the start char
double thinLineAmount = (this.wideNarrowRatio * thickbars) + thinbars ;
info.ThinBarWidth = this.Size.Width / thinLineAmount;
}
/// <summary>
/// Checks the code to be convertible into an standard codabar.
/// </summary>
/// <param name="text">The code to be checked.</param>
protected override void CheckCode(string text)
{
if (text == null)
throw new ArgumentNullException("text");
if (text.Length == 0)
throw new ArgumentException(BcgSR.InvalidCodabar(text));
foreach (char ch in text)
{
if ("0123456789-$:/.+ABCD".IndexOf(ch) < 0)
throw new ArgumentException(BcgSR.InvalidCodabar(text));
}
}
/// <summary>
/// Renders the bar code.
/// </summary>
protected internal override void Render(XGraphics gfx, XBrush brush, XFont font, XPoint position)
{
XGraphicsState state = gfx.Save();
BarCodeRenderInfo info = new BarCodeRenderInfo(gfx, brush, font, position);
InitRendering(info);
info.CurrPosInString = 0;
//info.CurrPos = info.Center - this.size / 2;
info.CurrPos = position - CodeBase.CalcDistance(AnchorType.TopLeft, this.anchor, this.size);
RenderStart(info);
while (info.CurrPosInString < this.text.Length)
{
RenderNextChar(info);
RenderGap(info, true);
}
RenderStop(info);
if (TextLocation != TextLocation.None)
RenderText(info);
gfx.Restore(state);
}
private void RenderNextChar(BarCodeRenderInfo info)
{
RenderChar(info, this.text[info.CurrPosInString]);
++info.CurrPosInString;
}
private void RenderChar(BarCodeRenderInfo info, char ch)
{
string thickThinLines = ThickThinLines(ch);
int idx = 0;
while (idx < 7)
{
if ((idx & 1) == 0)
{
RenderBar(info, thickThinLines.Substring(idx, 1).Contains("1"));
}
else
{
RenderGap(info, thickThinLines.Substring(idx, 1).Contains("1"));
}
idx += 1;
}
//RenderGap(info, false);
}
private void RenderStart(BarCodeRenderInfo info)
{
RenderChar(info, StartChar);
RenderGap(info, true);
}
private void RenderStop(BarCodeRenderInfo info)
{
RenderChar(info, EndChar);
}
}
}
add this to the PdfSharp.Drawing.Barcode folder within the PdfSharp project as 'Codabar.cs'.
An addition is required to the BcgSR.cs file for the extra error handling which is
Code:
internal static string InvalidCodabar(string code)
{
return String.Format("Contains an valid character for codabar at index: {0}.", code);
}
I've tested the results with a Quick Check 810 barcode analyser and it gave me a readability of 88% when printing the codes "A1234567890-$:/.+C" or ""B1234567890-$:/.+D" , the full codabar character set, at a size of 50mm x 10mm on a 600dpi epson laser printer using a WideNarrowRatio of 3 (normal for codabar) I found that a ratio of 2 will read but very rarely and often incorrectly with a standard desktop reader and not at all with my QC810.
edit:
I forgot to mention that you also need to set the StartChar and EndChar to one of 'A', 'B', 'C' or 'D' to use it. Other valid characters are '1234567890-$:/.+'