# Charts and Barcodes

> Insert Code 128 barcodes with the WPF TXBarcodeControl, validate and caption their text, update or remove them, and see why charts need Windows Forms.

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

---

Barcodes and QR codes carry order numbers, product codes and links in printed documents. In a WPF application, TX Text Control creates them with a WPF barcode control and places them in the document like images, where your code can change and remove them again. Charts are a different matter: they are Windows Forms controls and can't be used with the WPF `TextControl`, as the last section explains.

For the user's side, see [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/llms-full.txt). [Integrating WPF Barcodes and Drawings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/barcodes-drawings/llms-full.txt) describes the barcode control as an element of your windows, and [Images and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/images-barcodes/llms-full.txt) the barcodes a mail merge fills with data. Frames are positioned as [Images, Text Frames, and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/images-shapes/#how-frames-are-placed) describes. *MainWindow.xaml.cs* needs these `using` directives:

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

### How Barcodes Work

A barcode in a document is a [TXBarcodeControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/llms-full.txt), which holds the type, the encoded text and the WPF colors, placed by a [BarcodeFrame](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/llms-full.txt) like an image. [How Barcodes Work](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/#how-barcodes-work) describes both classes and the formats that keep barcodes, and [Insert a Barcode From Code](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/#insert-a-barcode-from-code) inserts a QR code step by step.

[Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/barcodes-property/llms-full.txt) of the `TextControl` returns the [BarcodeCollection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodecollection/llms-full.txt) of the text part with the input focus. [HeaderFooter.Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/headerfooter/barcodes-property/llms-full.txt) holds the barcodes of a header, which appear on every page. Text Control's feature comparison lists 1D and 2D barcodes for the Enterprise and Professional editions.

A document with barcodes looks like this packing slip, with a QR code at the top and a linear barcode in each item row:

![A packing slip with a QR code in the upper right corner and a linear barcode with its digits at the end of each item row.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/packing-slip-barcodes.4a025714.webp)

### Insert a Barcode With Text

The following handler checks an invoice number and inserts it inline as a Code 128 barcode with its text and a caption below the bars:

```csharp
private void InsertBarcodeButton_Click(object sender, RoutedEventArgs e)
{
    string invoiceNumber = "INV-2025-001";

    // Check the text before it is assigned
    string errorMessage;

    if (!TXBarcodeControl.IsTextValid(BarcodeType.Code128, invoiceNumber, out errorMessage))
    {
        MessageBox.Show(errorMessage);
        return;
    }

    TXBarcodeControl code128 = new TXBarcodeControl();
    code128.BarcodeType = BarcodeType.Code128;
    code128.UpperTextLength = invoiceNumber.Length;
    code128.Text = invoiceNumber;
    code128.ShowText = true;
    code128.AdditionalText = "Invoice number";
    code128.TextAlignment = TXTextControl.Barcode.TextAlignment.Bottom;

    // Inline, like a single character, at the input position
    BarcodeFrame frame = new BarcodeFrame(code128)
    {
        Name = "invoice-number",
        Size = new System.Drawing.Size(4320, 1080) // 3 x 0.75 inch in twips
    };

    textControl1.Barcodes.Add(frame, -1);
}
```

- The static [IsTextValid](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/istextvalid-method/llms-full.txt) checks a text for a barcode type and returns an error message for an invalid one. Use it for values the user types, instead of catching the exception of `Text`.
- [ShowText](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/showtext-property/llms-full.txt) shows the encoded text of a one-dimensional barcode, and [AdditionalText](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/additionaltext-property/llms-full.txt) adds a text of your own.
- [TextAlignment](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/textalignment-property/llms-full.txt) puts both texts at the `Bottom`, the default, or the `Top`. The enumeration is written as `TXTextControl.Barcode.TextAlignment`, because `System.Windows` has a `TextAlignment` as well.
- [ForeColor](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/forecolor-property/llms-full.txt) and [BackColor](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/backcolor-property/llms-full.txt), not set here, take a nullable `System.Windows.Media.Color`. The default of `ForeColor`, `null`, stands for the system's window text color.
- `System.Drawing.Size` is written in full because `System.Windows` has a `Size` of its own.

To anchor the code at the right edge of the paragraph with the input position, with the text flowing around it, use `textControl1.Barcodes.Add(frame, TXTextControl.HorizontalAlignment.Right, -1, FrameInsertionMode.DisplaceText)`; `DisplaceText` is a value of [FrameInsertionMode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/frameinsertionmode/enumeration/llms-full.txt), and `TXTextControl.HorizontalAlignment` is qualified because WPF has a `HorizontalAlignment` too. For a code on every page, add the frame to the header: `header.Barcodes.Add(frame, TXTextControl.HorizontalAlignment.Right, -1, FrameInsertionMode.AboveTheText)`. For one page, use the overload with a page number and a location, `textControl1.Barcodes.Add(frame, 1, new System.Drawing.Point(8640, 720), FrameInsertionMode.AboveTheText)`.

### Change and Remove Barcodes

[BarcodeFrame.Barcode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/barcode-property/llms-full.txt) returns the barcode control of a frame as an `object`; cast it to the WPF `TXBarcodeControl`. A changed barcode appears in the document after [Refresh](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/refresh-method/llms-full.txt):

```csharp
private void UpdateBarcodeButton_Click(object sender, RoutedEventArgs e)
{
    // null if no barcode has this name
    BarcodeFrame frame = textControl1.Barcodes.GetItem("invoice-number");

    if (frame == null)
    {
        return;
    }

    TXBarcodeControl barcode = (TXBarcodeControl)frame.Barcode;
    barcode.Text = "INV-2025-002";

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

[BarcodeCollection.GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodecollection/getitem-method/llms-full.txt) finds a barcode by its name or ID and returns `null` when there is none, and [Remove](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodecollection/remove-method/llms-full.txt) deletes it. The `TextControl` raises [BarcodeCreated](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/barcodecreated-event/llms-full.txt) and [BarcodeDeleted](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/barcodedeleted-event/llms-full.txt) when barcodes are created and deleted. Changes your code makes to a barcode don't go into the undo history of the editor unless you call [AddUndoUnit](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/barcodeframe/addundounit-method/llms-full.txt).

[SaveImage](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf-barcode/txbarcodecontrol/saveimage-method/llms-full.txt) of the WPF barcode control writes the barcode to a stream as an image, in a format of [TXTextControl.WPF.Barcode.ImageFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-barcode/wpf-barcode-imageformat/enumeration/llms-full.txt) such as `PNG`. The class has further `SaveImage` overloads with the `ImageFormat` of `System.Drawing.Imaging`. Because of them, a WPF project needs the NuGet package *System.Drawing.Common* for any call of `SaveImage`, also one with the WPF `ImageFormat`; without it, the compiler reports error CS0012, "The type 'ImageFormat' is defined in an assembly that is not referenced".

### Charts in WPF Applications

Charts are Windows Forms controls, and the WPF `TextControl` has no `Charts` collection, as [Charts in WPF](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/#charts-in-wpf) explains. [ChartFrame](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/chartframe/llms-full.txt), [ChartCollection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-datavisualization/chartcollection/llms-full.txt) and [ServerTextControl.Charts](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/charts-property/llms-full.txt) appear in the reference of this product as well, but a chart frame needs the Windows Forms chart control `System.Windows.Forms.DataVisualization.Charting.Chart` in any case. To show numbers graphically in a WPF application, use a table, as [Tables and Calculations](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/tables/llms-full.txt) describes, or an image of a chart created elsewhere, inserted as [Images, Text Frames, and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/images-shapes/llms-full.txt) shows.

### Next Steps

- [Images, Text Frames, and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/images-shapes/llms-full.txt): the other kinds of frames.
- [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/charts-barcodes/llms-full.txt): barcodes from the user's side.
- [Integrating WPF Barcodes and Drawings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/barcodes-drawings/llms-full.txt): the WPF barcode control in your windows.
- [Images and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/images-barcodes/llms-full.txt): fill barcodes of a template with data.

---

## Nearby

- **Previous:** [Images, Text Frames, and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/images-shapes/llms-full.txt)
- **Next:** [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt)
