TX Text Control .NET for Windows Forms

Integrating Charts and Barcodes

Topic

Charts and barcodes are objects in a TX Text Control document, placed like images and saved with the document. Each one is backed by a control that draws it: a chart by the chart control of System.Windows.Forms.DataVisualization, a barcode by the barcode control of TX Text Control. The barcode control is in the TX Text Control package; the chart control has to be added to .NET projects separately. What each needs in your project, how the ribbon offers them to users and where your code inserts and updates them is described below, together with barcodes as SVG images.

What users do with charts and barcodes in the editor, such as formatting and positioning them, is the subject of Charts and Barcodes in the Document Editor section.

What Each Object Is Made of

ChartBarcode
The object in the documentChartFrameBarcodeFrame
The control that draws itSystem.Windows.Forms.DataVisualization.Charting.ChartTXBarcodeControl in TXTextControl.Barcode
All of them in the text part with the input focusTextControl.ChartsTextControl.Barcodes
The assembliesTXTextControl.dll and the chart assemblyTXBarcode.dll and TXBarcode.Windows.Forms.dll, in the package

The frame handles the position of the object in the text: inline like a character, anchored to a paragraph or at a fixed place on the page. The control holds the content: the data and the appearance of the chart, the type and the text of the barcode. Your code reaches the control through the frame's Chart or Barcode property.

Add the Chart Control to the Project

A chart frame can only be used when the assembly with the chart control is available. How it gets into your project depends on the framework:

  • .NET Framework. System.Windows.Forms.DataVisualization is part of the .NET Framework since version 4. Add a reference to the assembly System.Windows.Forms.DataVisualization in Reference Manager, under Assemblies.
  • .NET 8 or higher. Microsoft did not port the chart control to .NET. You install a package built from Text Control's fork of a .NET port, as Migrating from .NET Framework to .NET describes step by step.

With the chart control in the project, the chart commands of the ribbon work, and your code can create charts. Barcodes need no extra step: their assemblies come with TXTextControl.TextControl.WinForms.SDK.

Offer Charts and Barcodes in the Ribbon

The ribbon offers charts and barcodes through three of its predefined tabs: the Chart and Barcode buttons of RibbonInsertTab, RibbonChartLayoutTab for a selected chart and RibbonFrameLayoutTab for the position, text wrapping and size of a selected frame. What their buttons do is shown in Charts and Barcodes on the Charts and Barcodes page of the Document Editor section.

A form of your own needs no more than the tabs it uses. In the blog article Using Charts with TX Text Control .NET for Windows 31.0 in .NET 6, a .NET application has a ribbon with only the Insert and the Chart Layout tab; once the chart package is installed, both tabs recognize the chart control and enable their chart commands:

A Windows Forms application with a ribbon of two tabs, Insert and Chart Layout, where the Minor Gridlines menu of the X-Axis group shows its color palette above a selected 3-D area chart.

The TX Words sample shows these tabs as contextual tabs, which appear only while a chart or a frame is selected. Built-in Ribbon Tabs shows how to do this, and Visual Studio Designer Integration how to add the tabs in the designer. Without a ribbon, ChartLayoutDialog and BarcodeLayoutDialog open dialog boxes for the layout of the selected object.

Create Charts and Barcodes in Code

Your code creates the control, wraps it in its frame and adds the frame to Charts or Barcodes. A chart control needs at least one chart area and one series before it is wrapped. The code is described on other pages:

ToRead
Insert a chart bound to a DataTableAdd Charts in Document Processing
Fill the named charts of a loaded document with new dataFill the Charts of a Template With Data
Insert a QR code or a linear barcodeInsert a QR Code and Insert a Barcode With Text
Change the chart or barcode the user has selectedCharts and Barcodes in the Document Editor section
Fill charts and barcodes of a template in a mail mergeImages and Barcodes

The Charts sample of TX Text Control inserts a chart this way and lets the user change it in a property grid:

A Windows Forms sample with a column chart in the document and a panel at the right with the chart name points and a property grid of the chart's appearance.

The TXBarcodeControl is not placed on the form: it lives inside the frame. The events ChartCreated and BarcodeCreated of the TextControl tell your code when a chart or barcode arrives through the clipboard or a loaded document; they don't occur for objects your code adds with Add.

Create a Barcode as SVG With TXBarcodeCore

TXBarcodeCore, introduced in version 33.0, is a barcode without a control. It has the type, text and appearance properties of TXBarcodeControl and returns the barcode as an SVG string, for example for a web page, an email or an image file. A BarcodeFrame takes a TXBarcodeControl, as the reference of its constructor states, so a barcode that users should edit in the document is inserted as in the previous section. The SVG of a TXBarcodeCore goes into a document as an image:

using System;
using System.Drawing;
using System.IO;
using TXTextControl.Barcode;

private void insertSvgBarcodeButton_Click(object sender, EventArgs e)
{
    const string orderNumber = "ORD-2025-0042";

    // Initialize the new instance before using it
    TXBarcodeCore barcode = new TXBarcodeCore();
    barcode.Create();
    barcode.BarcodeType = BarcodeType.Code128;
    barcode.UpperTextLength = orderNumber.Length;
    barcode.Text = orderNumber;

    // The barcode as an SVG image
    string svg = barcode.GetSVG(new Size(4320, 1080));
    File.WriteAllText("order-barcode.svg", svg);

    // 8 is the import filter for SVG
    textControl1.Images.Add(new TXTextControl.Image("order-barcode.svg", 8), -1);
}

The code works as follows:

  • Create initializes the object and must be called before any other member.
  • BarcodeType is set first, because UpperTextLength, the maximum number of characters, and Text throw an ArgumentException when the value isn't valid for the type.
  • GetSVG returns the SVG in the given size, which also becomes the Size of the object.
  • The image is inserted inline at the input position, as Images, Text Frames, and Shapes describes. It is an image in the document, not a barcode that the barcode tabs of the ribbon can change.

Before you set Text from user input, the static IsTextValid tells whether a text is valid for a barcode type and returns the error message if not. GetDefaultText returns a valid sample text, and GetMinimumTextLength and GetMaximumTextLength the shortest and longest text of a type. Save writes the settings of the barcode to a file, a Stream, a TextWriter or an XmlWriter, and Load reads them into a TXBarcodeCore again.

Shapes have a counterpart without a control as well, TXDrawingCore, described in Render Shapes as SVG With TXDrawingCore.

Next Steps

  • Charts and Barcodes: charts and barcodes from the user's side of the editor.
  • Charts and Barcodes in Document Processing: the same objects in documents created with ServerTextControl.
  • Images and Barcodes: merge data into barcodes and charts of a template.
  • The blog article Using Charts with TX Text Control .NET for Windows 31.0 in .NET 6: the chart package for .NET with screenshots.