TX Text Control .NET for WPF

Charts and Barcodes

Topic

Shipping labels, tickets and invoices carry barcodes: a Code 128 bar with an order number, a QR code that opens a web page. The WPF TextControl places barcodes in the text like images, users insert and color them from the ribbon, and your code creates them from the WPF barcode control. Charts are a different matter in WPF, as the first section explains.

The handlers below go into MainWindow.xaml.cs of the window from Create Your First WPF Application, with a TextControl named textControl1; replace SampleApp with your namespace. For barcodes in documents that are processed without a user interface, see Charts and Barcodes in Document Processing, and for barcodes that a mail merge fills, Images and Barcodes.

Charts in WPF

Charts in TX Text Control are chart controls of System.Windows.Forms.DataVisualization.Charting, and the editor support for them is part of the Windows Forms product. The WPF TextControl has no Charts collection, no chart dialog box and no chart events, and the WPF Insert tab has no Chart button. The class diagram of the chart classes marks them as Windows Forms only:

Class diagram of the chart classes with the legend Windows Forms only: TextControl with Charts, ChartLayoutDialog and the chart events, ChartCollection, ChartFrame and the System.Windows.Forms.DataVisualization.Chart it holds.

How Barcodes Work

A barcode in a document consists of two objects:

  • A TXBarcodeControl of the TXTextControl.WPF.Barcode namespace holds the barcode type, the encoded text and the colors. It's a WPF FrameworkElement, and its colors are System.Windows.Media.Color values.
  • A BarcodeFrame of TXTextControl.DataVisualization places it in the document, with a name, an ID, a size and a position like any other frame.

TextControl.Barcodes returns the BarcodeCollection of the main text. Barcodes are placed inline, anchored to a paragraph or at a fixed position, as Images describes. The TX Text Control format and RTF keep them as barcodes, while DOCX, PDF and the other formats contain an image of the barcode.

Class diagram of the barcode classes: TextControl, WPF.TextControl and ServerTextControl with Barcodes, BarcodeLayoutDialog and the barcode events, BarcodeCollection, BarcodeFrame and the TXBarcodeControl and WPF.TXBarcodeControl with their properties.

The package TXTextControl.TextControl.WPF.SDK contains the barcode assemblies TXBarcode.dll and TXBarcode.WPF.dll; Integrating WPF Barcodes and Drawings describes them.

Insert and Format Barcodes in the Ribbon

Barcode in the Illustrations group of the Insert tab, a RibbonInsertTab, lists the barcode types, such as QRCode, Code128, EAN13, Datamatrix and PDF417, and a click inserts one at the input position.

A selected barcode is changed on the Frame Formatting tab, a RibbonFrameLayoutTab. Besides Arrange, Size and Properties, it shows the Colors & Alignment group:

CommandChanges
Foreground ColorThe color of the bars or modules
Background Color, TransparencyThe background and how transparent it is
Horizontal Alignment, Vertical AlignmentThe position of the code inside its frame
RotationThe angle of the barcode

The barcode type and the encoded text are set in the barcode dialog box, which BarcodeLayoutDialog opens for a selected barcode. Its third tab holds the colors, type and text; the first two set text wrapping and layout, and size and distances.

The Barcode Layout dialog box on its Type and Color tab, with the background color and transparency, the bar color, horizontal and vertical alignment, rotation, and the Encoding list open with QRCode, Code128, EAN13, UPCA, EAN8 and Interleaved2of5.

Insert a Barcode From Code

The following handlers insert a dark blue QR code with a link and change the text of the selected barcode:

using System.Windows;
using System.Windows.Media;
using TXTextControl;
using TXTextControl.Barcode;
using TXTextControl.DataVisualization;
using TXTextControl.WPF.Barcode;

namespace SampleApp;

public partial class MainWindow : Window
{
    private void InsertQrCode_Click(object sender, RoutedEventArgs e)
    {
        const string portalLink = "https://www.textcontrol.com";

        // Set the type first: UpperTextLength and Text are checked against it
        TXBarcodeControl qrCode = new TXBarcodeControl();
        qrCode.BarcodeType = BarcodeType.QRCode;
        qrCode.UpperTextLength = portalLink.Length;
        qrCode.Text = portalLink;
        qrCode.ForeColor = Colors.DarkBlue;

        BarcodeFrame frame = new BarcodeFrame(qrCode)
        {
            Name = "portal-link",
            Size = new System.Drawing.Size(1440, 1440) // 1 x 1 inch in twips
        };

        // Inline, like a single character, at the input position
        textControl1.Barcodes.Add(frame, -1);
    }

    private void ChangeBarcode(string text)
    {
        // The barcode the user has selected, or null
        BarcodeFrame frame = textControl1.Barcodes.GetItem();

        if (frame != null)
        {
            TXBarcodeControl barcode = (TXBarcodeControl)frame.Barcode;
            barcode.Text = text;

            // Changes to the barcode are shown after Refresh
            frame.Refresh();
        }
    }
}

The code works as follows:

  • BarcodeType is shared with Windows Forms and comes from TXTextControl.Barcode; its 20 values include the two-dimensional codes QRCode, Datamatrix, PDF417, MicroPDF, AztecCode and Maxicode, barcodes such as Code128, Code39, EAN13 and UPCA, and the postal zip code barcodes IntelligentMail and Postnet. The reference page says what each type encodes.
  • UpperTextLength limits the number of characters and Text is the encoded text. Both throw an ArgumentException for values the barcode type doesn't accept, which is why the type is set first.
  • ForeColor takes a WPF color, here Colors.DarkBlue of System.Windows.Media. ShowText prints the text of a one-dimensional barcode with it.
  • The BarcodeFrame constructor takes the barcode control. The frame's Size is a System.Drawing.Size in twips, here 1 × 1 inch.
  • BarcodeCollection.Add with -1 inserts the barcode inline at the input position. The overload with only a FrameInsertionMode lets the user draw it with the mouse.
  • BarcodeCollection.GetItem without a parameter returns the selected barcode, or null. BarcodeFrame.Barcode is its control, and changes appear after Refresh.

The static IsTextValid checks text from a user before you assign it and returns an error message when the text doesn't fit the barcode type. BarcodeDoubleClicked and the other barcode events are declared on the WPF:TextControl element in XAML; see Events. The blog article Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide shows barcode types and templates with barcodes.

Next Steps