# Charts and Barcodes

> Insert QR codes and barcodes in a WPF TextControl with the WPF TXBarcodeControl and BarcodeFrame, format them on the ribbon; charts are Windows Forms only.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Document Editor > Charts and Barcodes
- **2665 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/llms-full.txt

---

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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt), 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/charts-barcodes/llms-full.txt) in Document Processing, and for barcodes that a mail merge fills, [Images and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/images-barcodes/llms-full.txt).

### 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.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/charts-class-diagram.ff05f52b.webp)

### How Barcodes Work

A barcode in a document consists of two objects:

- A [TXBarcodeControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/llms-full.txt) of `TXTextControl.DataVisualization` places it in the document, with a name, an ID, a size and a position like any other frame.

[TextControl.Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/barcodes-property/llms-full.txt) returns the [BarcodeCollection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodecollection/llms-full.txt) of the main text. Barcodes are placed inline, anchored to a paragraph or at a fixed position, as [Images](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/images/#how-images-are-placed) 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.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/barcodes-class-diagram.df5a7e7c.webp)

The package `TXTextControl.TextControl.WPF.SDK` contains the barcode assemblies *TXBarcode.dll* and *TXBarcode.WPF.dll*; [Integrating WPF Barcodes and Drawings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/barcodes-drawings/llms-full.txt) describes them.

### Insert and Format Barcodes in the Ribbon

**Barcode** in the **Illustrations** group of the **Insert** tab, a [RibbonInsertTab](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribboninserttab/llms-full.txt), 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribbonframelayouttab/llms-full.txt). Besides **Arrange**, **Size** and **Properties**, it shows the **Colors & Alignment** group:

| Command | Changes |
|---|---|
| **Foreground Color** | The color of the bars or modules |
| **Background Color**, **Transparency** | The background and how transparent it is |
| **Horizontal Alignment**, **Vertical Alignment** | The position of the code inside its frame |
| **Rotation** | The angle of the barcode |

The barcode type and the encoded text are set in the barcode dialog box, which [BarcodeLayoutDialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/barcodelayoutdialog-method/llms-full.txt) 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.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/barcode-layout-dialog.771a6b03.webp)

### 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:

```csharp
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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-barcode/barcodetype/enumeration/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/uppertextlength-property/llms-full.txt) limits the number of characters and [Text](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/text-property/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/forecolor-property/llms-full.txt) takes a WPF color, here `Colors.DarkBlue` of `System.Windows.Media`. [ShowText](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/showtext-property/llms-full.txt) prints the text of a one-dimensional barcode with it.
- The [BarcodeFrame constructor](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/constructor/llms-full.txt) takes the barcode control. The frame's `Size` is a `System.Drawing.Size` in twips, here 1 × 1 inch.
- [BarcodeCollection.Add](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodecollection/add-method/llms-full.txt) with `-1` inserts the barcode inline at the input position. The overload with only a [FrameInsertionMode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/frameinsertionmode/enumeration/llms-full.txt) lets the user draw it with the mouse.
- [BarcodeCollection.GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodecollection/getitem-method/llms-full.txt) without a parameter returns the selected barcode, or `null`. [BarcodeFrame.Barcode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/barcode-property/llms-full.txt) is its control, and changes appear after [Refresh](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/refresh-method/llms-full.txt).

The static [IsTextValid](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/istextvalid-method/llms-full.txt) checks text from a user before you assign it and returns an error message when the text doesn't fit the barcode type. [BarcodeDoubleClicked](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/barcodedoubleclicked-event/llms-full.txt) and the other barcode events are declared on the `WPF:TextControl` element in XAML; see [Events](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/events/llms-full.txt). The blog article [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/) shows barcode types and templates with barcodes.

### Next Steps

- [Images](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/images/llms-full.txt): position frames and choose the text flow.
- [Text Frames and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/text-frames-shapes/llms-full.txt): text boxes and drawings.
- [Images and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/images-barcodes/llms-full.txt): merge data into barcodes of a template.
- [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/charts-barcodes/llms-full.txt): barcodes with `ServerTextControl`.

---

## Nearby

- **Previous:** [Text Frames and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/text-frames-shapes/llms-full.txt)
- **Next:** [Headers and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/headers-footers/llms-full.txt)
