Creating a DataMatrix GS1 barcode and saving to postscript, PDF, or image for free in C#.

I’ve been working on an interesting project where I had to learn Pitney Bowes Streamweaver in order to sort/merge and barcode postscript with very little knowledge of postscript and no training or knowledge of Streamweaver. I rolled my own in C# in order to understand how the product worked for parsing but didn’t know much about creating the data matrix barcode and placing it into the postscript, which left me relying on Streamweaver no matter what. My employer is continuing to use Streamweaver until we rewrite this functionality into Adobe ES (hopefully), but if you have small amounts of postscripts to barcode for printing or whatever maybe this will be helpful. Adobe has a free book to get you up to speed on the Postscript language and this post was helpful as well.

I wrote a small console app that will produce a barcode written out in Postscript, JPG, PDF, Hex and ASCII85 encoded. This program has few features and less options as I was just playing with different approaches, these turned out to be the best, but if you need more just ask and I can help as much as possible.

barcode

Download DataMatrix Barcode Generator

Before I go further I’d like to acknowledge and link the folks who did the real work!

Datamatrix.Net
Ghostscript
Ghostscript.Net
iTextSharp
Ascii85 encoding code

Here’s a sample of the code.

using System.Drawing;
using System.Drawing.Imaging;

using DataMatrix.net;
using Ghostscript.NET.Processor;

// Examples
namespace datamatrix
{
    class Program
    {

        public static void convertPDFToPs(string inputFile, string outputFile)
        {
            List<string> cmds = new List<string>();
            string postscriptOut = string.Format("-sOutputFile={0}", outputFile);

            using (GhostscriptProcessor processor = new GhostscriptProcessor())
            {
                cmds.Add("-ignored");
                cmds.Add("-q");
                cmds.Add("-dSAFER");
                cmds.Add("-sDEVICE=ps2write");
                cmds.Add("-dASCII85EncodePages=true");
                cmds.Add(postscriptOut);
                cmds.Add("-f");
                cmds.Add(inputFile);

                processor.StartProcessing(cmds.ToArray(), null);
            }
        }
		
	 public static string generateBarcode(string outputImageFile)
        {
	    string imageHexString = "";  // OUTPUT: Hexdecimal string.

            DmtxImageEncoder encoder = new DmtxImageEncoder();
            DmtxImageEncoderOptions options = new DmtxImageEncoderOptions();
            Bitmap encodedBitmap = new Bitmap(38, 38, PixelFormat.Format8bppIndexed);

            options.ModuleSize = 2; 
            options.MarginSize = 1;
            options.BackColor = Color.Transparent;
            options.ForeColor = Color.Black;
			
            options.Scheme = DmtxScheme.DmtxSchemeAsciiGS1;
 
            encodedBitmap = encoder.EncodeImage(gs1Code, options);
            encodedBitmap.SetResolution(96.0F, 96.0F);

            using (var stream = new MemoryStream())
            {
                encodedBitmap.Save(stream, ImageFormat.Jpeg);
                stream.Seek(0, SeekOrigin.Begin);
                buffer = stream.ToArray();

                // Get hexdecimal
                imageHexString = BitConverter.ToString(buffer).Replace("-", "");

                // Save the image to disk, save the barcode as Jpg,Png, whatever image.
                encodedBitmap.Save(outputImageFile, ImageFormat.Jpeg); // Save the barcode as Jpg,Png, whatever image.
            }  

	 return imageHexString
	 }
    }
}

Some conversions in Python.

from PIL import Image # Pillow : https://python-pillow.github.io/
import binascii
 
# Convert hex to jpg 
out_path  = r'c:\barcode\from_hex.jpg'
jpg_hex = u"the hex string" # Hex output from datamatrix in text file.

image_data = binascii.a2b_hex(jpg_hex)
with open(out_path, 'wb') as output:
    output.write(image_data)


# Convert jpg to eps 
jpg_file = r'c:\barcode\DarkArtistry.com.jpg'
eps_out = jpg_file.replace('jpg','eps')
image_data=Image.open(jpg_file)
image_data.save(eps_out)

Adding jpeg hex to an Adobe postscript. You should be able to paste this into an existing postscript, just be careful where you place it; directly after %%Page might be a good start.

%%BeginBarcode
%%PageBoundingBox: 0 0 38 38
DisplayImage
0 0 translate
20 20 scale
12
38 38   %% Width Height
1
1
1
8       %% Bit
F8FFFAFFE8FFFFF3FFEFFFFDF3F9FFF4FFF7FFF7FFFFFFFBF0FFFBFFFFFFFFF8FFFCFFF1
FFFFFFFF00F8FF0000FFF8000AFFFF0407FFFF0F00FFFF0000FFFF0000F2FF0004FFF817
00FFFBF6F2FA00FFF40200FFFF0D00ECFF0600FFFA0105FEFC0002F9FA0014FFFF0800FF
F90009FFFFEFFAFF00FFFF040CEBFC0000FFF900000103FFFAFFFF000406010600000000
04FCFFEFFB0001FAFFF800FFFA0000FFFF0500FFFF00160000FFFCFDFF00060000100700
030200FFFFFFFF0C00FCFAFF10FCFFFFE5FFF8FFF90006F8FF00020207060400030105F5
F600000000FFF2FDFFF1FBFFFFFF00FFFFF3FFFFFAFFFF0C00FFF8090011000004000000
09FFFF0507010CFBFFFFFBF4FFFAFEFF010003FFFF00060000FFFF0004FFFF0013FBFF07
03FEFD0000000CF3FE060000020007F9FFFE000B00FFF40E050008FCFF0000FFFF0009FF
FB000FFCF515000110F8FF020006000405FAFFEF00030AFBFF00000B00FFFCFFFF000004
00FFFFF6FE0007F5FFF4F9FFFB000AFFEBFFEDFFECFF010008F4FF0800030BFEFFF9FF00
001205EFFFF4FF0001F5FFFFFCF9FF0000F1FFFFFFFFFFF904F6FDFFFEF1FFF0FFFCF614
00FFFF000400000000FEFB100000030100FFFFFFFF0000FFFFE402FFFAFDFFFFECFFFFFD
FF0000FEFF000003010C00FFF1001A01001B08FFFFE5FF0800EFFEFF080000F8FF000FFF
FF00000311F8FF0300FFF700000219F8F40900000BFFFFFFF7F5FFFFFFF2050004FFF602
05F3F416000000FFF90012FFFF00000000FFEF0E031300F9F1FFF6FFEFFBFEFB000401F7
FFFAFF00030200FFFF0002FFF0F9FEFFFA04080000FFF7FAFFFFFF02000000FFF2FF0800
09FFEDFFF802100009F8FA0600FFEDFFFEF9F901000005F6FFFFFEFFFF08000900FFFFFF
050000000009070100F9FAFFFF040BFFFAF7FF08000B0BFFF2FFFFF3FD0000F8FFF7FFFF
FBED000900080700000109FFFDF5FF0000FFFAFEFF000F0006E4FFF1FDFCFF0016F9FFFF
FEFBFAFF00FFF600000C02FEECFFFF00020A00FFFFFFF40A00FFFCFFFFFFFFF4FFFFED07
000500F1FAFE06FEFF00000000FFFFF2FA07000000FFFAFBF60800F8FFF3F4FFFBFFF5FF
FF02000007FFFFF8070000FFFF0001FFEFFFFFF8FFFFF4F9FD070000090B00FFFF000209
00FBF6F2FFFFF9FFFBFF001809F2FF0200F4F2FFF5FFFFF5FFF8FF0700030C0006F5FF00
000307FFFFF4FFFFF3F4FFF416F1FF0500FCFFFCFF0001FEEFFFF8FFFF000EFCF9FFF5FF
F0FFFFF4F6FFF70100000BF5FFF010F0FF0200FFFAF9FF0000FCFFFDFFFAFF0300FDFEFF
F9FFF6FAFFFFF8FFFF00010000FFFFFF00FFF80014000C0800FBFFFBF00000FFF4FBFFF6
FFFCFF00001600000902000802FFF7FFFAF800FFFB010008000005FFFFFFFD0302F8FFFF
FFFFF4FFFF07000017000300040009F2FDF8FFFF0000080006FBFE11000000F5FFFFFF00
0A00000301FFF0FFFFFCF300020D0FE9FF0D0BF4FAFA0B00000001FFFF00040200FFFFFC
FF000002000002FFFFFFFFFEFF05000000FFF80600F7FFF800FFFF0000FBFF030B010000
05040011000106000DFDF4FFFB0000001FFDFFFFF3FFFFF9F3FF00FFFF1000FDEB0B0200
1D00090000050500020802F3FFF3FE110F0400F9E8FFFFF0FBFFFFFE0403000500FFFF00
11FAF6FFF0FFFF000006030000FFFCFFFC0000FFFB0D0E00000805FAFFFA030900010DF6
FF0000FFFEF3FFFAFF0200030A0000FCF7FFEC1002FDEB0D0200050000FFFEFA07FCFF07
000800FFF7FDEDFFFF0002FFFF000008050F08E8FF0000FFFF0001FFF7FFFFF9FFF509FF
FE00000006FFFFFFFFFFE70607F7F90A08000C0000FFFF0001FFF41006EEF8FFEEFFFEFF
0000030005010400000000001300000008000501040E000B0000090300000001100000FF
F7F5020507000000010901000E00000005000001010D00000000000A0700040000010000
0FF3F9FFFCFFF7F8FFFAFFFFF1EEFFF9FFFFF7FFFAFBFFF7FFFFFFFFFFFCEEFAFFF4F5FF
FFFFFEFF
%%EndBarcode 

If you need to convert XML to PostScript check out this excellent tool from Apache:

https://xmlgraphics.apache.org/fop/

~ by mohawke on December 5, 2015.