Drawing a 2D barcode in CorelDRAW

Making Barcodes in CorelDRAW

Some methods of making barcodes in CorelDraw:

Requirements

Placing a Barcode in CorelDRAW Using Active Document

1. Execute Edit->Insert New Object or Object->Insert New Object (depending on the CorelDRAW version):

The Object menu in CorelDRAW

2. Select StrokeScribe Document from the list of available objects:

The 'Insert new' dialog

3. Double-click the barcode. Change any barcode property you want:

Barcode properties in CorelDRAW

ActiveX-based Barcode in CorelDRAW

Do not use this example with CorelDraw X7 and upper.

1. Press Alt+F11 to open VBA editor.

2. Expand the project tree (press Ctrl+R if you don't see it) and look for the ThisDocument item.

3. Double-click the ThisDocument and paste the following code into the code window:

Sub barcode() Dim sh As Shape Dim ss As StrokeScribe Set sh = Application.ActiveLayer.CreateOLEObject("STROKESCRIBE.StrokeScribeCtrl.1") Set ss = sh.OLE ss.Text = "ABCD" ss.Alphabet = QRCODE ActiveDocument.ReferencePoint = cdrTopRight ActiveDocument.Unit = cdrMillimeter sh.SizeHeight = 20 sh.SizeWidth = 20 Dim pw As Double, ph As Double ActivePage.GetSize pw, ph sh.PositionX = pw sh.PositionY = ph End Sub

4. Add a reference to StrokeScribe ActiveX into your VBA project.

5. Run the code.

A vertor graphics of a barcode imported into CorelDRAW

Barcodes as EMF Pictures

In this example, we use the SavePicture method of the StrokeScribeClass to store barcode picture in a temporary file in vector format. Then, we load the file back into the document.

The Code Example

Sub test() Dim ss As StrokeScribeClass ' Go to VBA Menu->Tools->References and check the "StrokeScribeClass" Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1") ss.Alphabet = QRCODE ' This makes a QR Code ss.Text = "SOME TEXT" ss.Transparent = True pic = Environ("TEMP") & "\barcode.emf" rc = ss.SavePicture(pic, EMF, 30, 30) ' A 30x30mm QR Code drawing in vector format If rc > 0 Then Debug.Print "SavePicture failed, error code=", ss.Error End If Application.Optimization = True Dim doc As Document Set doc = ActiveDocument doc.Unit = cdrMillimeter doc.DrawingOriginX = -doc.ActivePage.SizeWidth / 2 ' Moving the drawing origin to the top-left corner of the page doc.DrawingOriginY = doc.ActivePage.SizeHeight / 2 doc.ReferencePoint = cdrTopLeft Dim lr As Layer Set lr = doc.ActivePage.Layers.Find("Barcode") ' Destroying a previously created barcode layer If Not lr Is Nothing Then lr.Delete End If Set lr = doc.ActivePage.CreateLayer("Barcode") ' A new layer that will contain the barcode picture lr.Import pic ' Inserting the barcode image into the newly created layer Kill pic ' We do not need the temporary file anymore Dim sh As Shape Set sh = lr.Shapes(1) ' The first and only shape on the layer is the imported EMF picture sh.Fill.UniformColor.CMYKAssign 0, 100, 100, 0 sh.SetPosition 0, 0 Application.Optimization = False ActiveWindow.Refresh Application.Refresh End Sub

To run the code, add a reference to StrokeScribeClass into your VBA project.

A barcode made of rectangular shapes in CorelDRAW

Drawing a Barcode Using Native CorelDRAW Shapes

This example is based on the ZebraBits property of the StrokeScribeClass which produces a binary string representing barcode modules. For example, the "1101" means two black modules, one white and one black module.

The code draws black modules using native CorelDraw shapes and groups these shapes into one object so it can be easily moved or transformed.

The Code Example

Sub test() Const left As Integer = 0 ' the barcode position relative to the top-left corner of the page (in millimeters) Const top As Integer = 0 Const modw As Integer = 1 ' the barcode module size, 1x1mm Const modh As Integer = 1 Dim doc As Document Set doc = ActiveDocument Dim lr As Layer Set lr = doc.ActivePage.Layers.Find("Barcode") ' this deletes a previously created barcode layer If Not lr Is Nothing Then lr.Delete End If Set lr = doc.ActivePage.CreateLayer("Barcode") ' a new layer that will contain barcode shapes doc.Unit = cdrMillimeter ' or =cdrInch doc.DrawingOriginX = -doc.ActivePage.SizeWidth / 2 ' moving the drawing origin to the top-left corner of the page doc.DrawingOriginY = doc.ActivePage.SizeHeight / 2 doc.ReferencePoint = cdrTopLeft Application.Optimization = True Dim ss As StrokeScribeClass Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1") ' Go to VBA Menu->Tools->References and check the "StrokeScribeClass" ss.Alphabet = QRCODE ss.Text = "TEXT" Dim zebra As String zebra = ss.ZebraBits Dim sr As New ShapeRange Dim x, y, i As Integer Dim z As String x = left y = top i = 1 For i = 1 To Len(zebra) z = Mid(zebra, i, 1) Select Case z Case "0" ' we do not draw white barcode modules x = x + modw Case "1" ' black barcode modules Dim sh As Shape Set sh = lr.CreateRectangle(x, y, x + modw, y - modh) sh.Outline.Type = cdrNoOutline sh.Fill.UniformColor.CMYKAssign 0, 100, 100, 0 sh.Name = "barcode" & i sr.Add sh x = x + modw Case Chr(10) x = left y = y - modh End Select Next i sr.Rotate 20 sr.Group Application.Optimization = False ActiveWindow.Refresh Application.Refresh End Sub

To run the code, add a reference to StrokeScribeClass into your VBA project.

A barcode created using specialized font in CorelDRAW

Font-based Barcode Drawing

In this example, we use the FontOut property of StrokeScribeClass that produces a text string that displays a barcode when formatted with a special TrueType font.

The Code Example

Sub test() Dim doc As Document Set doc = ActiveDocument Dim lr As Layer Set lr = doc.ActivePage.ActiveLayer ' we will place a barcode on the currently active layer Dim sh As Shape Set sh = lr.Shapes.FindShape("barcode") ' this deletes a previously created barcode shape If Not sh Is Nothing Then sh.Delete End If doc.Unit = cdrMillimeter doc.DrawingOriginX = 0 ' moving the drawing origin to the center of the page doc.DrawingOriginY = 0 doc.ReferencePoint = cdrTopLeft Application.Optimization = True Dim ss As StrokeScribeClass Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1") ' Go to VBA Menu->Tools->References and check the "StrokeScribeClass" ss.Alphabet = QRCODE ss.Text = "TEXT" Dim s As String s = ss.FontOut Set sh = lr.CreateParagraphText(0, 0, 50, 50, s, , , "StrokeScribe 2D") sh.Text.FitTextToFrame sh.Fill.UniformColor.CMYAssign 100, 100, 100 sh.Outline.Width = 0.01 sh.Outline.Color = sh.Fill.UniformColor sh.Name = "barcode" Application.Optimization = False ActiveWindow.Refresh Application.Refresh End Sub

To run the code, add a reference to StrokeScribeClass into your VBA project.