Using Images, Text Frames, Charts, Barcodes and Drawings

Images, text frames, charts, barcodes and drawings are represented through Image, TextFrame, ChartFrame, BarcodeFrameand DrawingFrame objects. The FrameBase class which is the base class of these objects handles the alignment in the text implementing properties like Location, TextDistances, InsertionMode, TextPosition and ChangeZOrder. All objects of the same kind are contained in collections, named ImageCollection, TextFrameCollection, ChartCollection, BarcodeCollectionand DrawingCollection, which can be used to add and to remove items or to get a particular item from the collection. Furthermore a FrameCollection has been defined containing all images, text frames, charts, barcodes and drawings. This collection is useful to get a particular item, regardless of the kind of object.

A chart frame is always associated with a chart control of the type System.Windows.Forms.DataVisualization.Charting.Chart. Therefore charts can only be used, when the assembly whichimplements this control is available. The Charting namespace is part of the .net Framework since version 4 and can be downloaded and installed for the .net Framework version 3.5.

A barcode frame is associated with a barcode control of the type TXTextControl.Barcode.TXBarcodeControl or TXTextControl.WPF.Barcode.TXBarcodeControl and can be only used, if the optional product assemblies for TX Barcode .NET are available.

A drawing frame is associated with a drawing control of the type TXTextControl.Drawing.TXDrawingControl or TXTextControl.WPF.Drawing.TXDrawingControl, which are located in the TXDrawing.dll, TXDrawing.Windows.Forms.dll and TXDrawing.WPF.dll assemblies.

TX Text Control supports three ways of inserting an image, a text frame, a chart, a barcode or a drawing into a document:

  • Inline, which means that it is treated in the text like a single character.
  • Anchored to a paragraph, which means that it is moved with the paragraph it is anchored to. Text can either flow aroundthe item or the item is displayed above or below the text. The frame's location is calculated from the top-left corner of the paragraph. If the image, text frame, chart, barcode or drawing is selected and moved by the end-user, the anchor position is also moved. The new anchor position becomes the top-left corner of the nearest paragraph.
  • At a fixed geometric position of the document. Text can either flow around around the item or the item is displayed above or below the text. The frame is not moved with the text and there is no anchor position in the text. The location is calculated from the top-left corner of a certain page or of the complete document.

Adding and Removing Images, Text Frames, Charts, Barcodes and Drawings

For each kind of object, image, textframe, chart, barcode and drawing, Text Control manages one collection containing all objects of that kind. The following describes the collection of images, the collections of the other work in the same way.

The image collection can be used to add and to remove images and to get a particular image from the collection. There are several Add methods which can be used to insert an image in the document.

The following two methods insert an image inline, which means that it is treated in the text like a single character:

public bool Add();
public bool Add(Image image, int textPosition);
Public Function Add() As Boolean
Public Function Add(ByVal image As Image, ByVal textPosition As Integer) As Boolean

The first version has no parameters and opens a File Open dialog box providing all image formats TX Text Control supports. The user can select an image, close the dialog box and the image is added to the collection and automatically inserted in the document at the current input position. The second method works in the same way, but inserts the image at the specified text position. To avoid, that adialog box is opened, the Image.FileName and the Image.FilterIndex property of the specified Image object must be set.

The following two methods insert an image anchored to a paragraph, which means that it is moved with the paragraph it is anchored to:

public bool Add(Image image, HorizontalAlignment alignment, int textPosition, ImageInsertionMode insertionMode);
public bool Add(Image image, Point location, int textPosition, ImageInsertionMode insertionMode);
Public Function Add(ByVal image As Image, ByVal alignment As HorizontalAlignment, ByVal textPosition As Integer, _
    ByVal insertionMode As ImageInsertionMode) As Boolean
Public Function Add(ByVal image As Image, ByVal location As Point, ByVal textPosition As Integer, _
    ByVal insertionMode As ImageInsertionMode) As Boolean

The text position can be any position in the paragraph. If -1 is specified, the image is anchored to the paragraph containing the current text input position. The location of the image relative to the paragraph is determined through a horizontal alignment or through a horizontal and a vertical offset. The position and extension of a paragraph is determined through its left and its right paragraph indent.The insertionMode parameter determines the textflow. The following values are possible:

  • AboveTheText: The image is inserted at a certain geometrical location above the text. This means that the image overwrites the text.
  • BelowTheText: The image is inserted at a certain geometrical location below the text. This means that the text overwrites the image.
  • DisplaceCompleteLines: The text stops at the top and continues at the bottom of the image.
  • DisplaceText: The text flows around the image and empty areas at the left and right side are filled.

Both methods open a file open dialog box only, when the Image.FileName and the Image.FilterIndex property of the specified Image object has not been set. The Image.Alignment property is only available for images anchored to a paragraph, for inline images and fixed positioned images a horizontal alignment is not defined.

The following code shows an example for inserting an image anchored to the fifth paragraph of a document:

Paragraph p = textControl1.Paragraphs[5];
textControl1.Images.Add(new Image(), HorizontalAlignment.Left, p.Start-1, ImageInsertionMode.DisplaceText);
Dim p As Paragraph = TextControl1.Paragraphs.Item(5)
TextControl1.Images.Add(New Image(), HorizontalAlignment.Left, p.Start - 1, ImageInsertionMode.DisplaceText)

The remaining two methods insert an image at a fixed geometric position in the document, which means that there is noanchor position and the image remains always at the same location:

public bool Add(Image image, int page, Point location, ImageInsertionMode insertionMode);
public bool Add(Image image, Point location, ImageInsertionMode insertionMode);
Public Function Add(ByVal image As Image, ByVal page As Integer, ByVal location As Point, _
    ByVal insertionMode As ImageInsertionMode) As Boolean
Public Function Add(ByVal image As Image, ByVal location As Point, _
    ByVal insertionMode As ImageInsertionMode) As Boolean

The first method defines the geometric position through a certain page and a location relative to the top-left corner on that page.The second method defines the position relative to the top-left corner of the complete document. This also includes all gaps between thepages. The horizontal value is relative to the left edge of all pages and the vertical value is relative to the top of the document including the gap above the first page.Regardless of which method has been used to insert the image the Image.Location property returns the location relative to the complete document. The insertionMode parameter determines the textflow with the same values as descibed for images anchored to a paragraph. Both methods open a file open dialog box only, when the Image.FileName and the Image.FilterIndex property of the specified Image object has not been set.

Loading, Saving and Exporting Images

TX Text Control can load images saved with the following image formats, while the SVG format is only supported by managed code TextControl components available for Windows Forms, WPF and ASP.NET.:

  • FilterIndex 1: Windows Bitmap (.BMP)
  • FilterIndex 2: Tagged Image Format (.TIF)
  • FilterIndex 3: Windows Metafile (.WMF)
  • FilterIndex 4: Portable Network Graphics (.PNG)
  • FilterIndex 5: JPEG File Interchange Format (.JPG, .JPEG)
  • FilterIndex 6: Graphics Interchange Format (.GIF)
  • FilterIndex 7: Windows Enhanced Metafile (.EMF)
  • FilterIndex 8: Scalable Vector Graphics (.SVG)

If an image has been added to a document with one of the ImageCollection.Add methods and this document is saved, a reference to the image file is saved. This reference is the image's file name including its absolute path on the file system. If such a document is loaded and the image to where the saved file reference points cannot be found, a rectangle with the image's size and a text string with the file name is displayed. The LoadSettings.ImageSearchPath property determines an additional path where to find images.

Images which are pasted from the clipboard have no file reference, therefore these images are saved in a document with their binary data. By default TX Text Control saves the image's binary data in its original file format.

Alternatively, a programmer can specify with the SaveSettings.ImageSaveMode or with the Image.SaveMode property whether images are saved through a file reference or with their binary data. The SaveSettings.ImageSaveMode property determines this for all images of the document and the Image.SaveMode property determines it for a certain image. If ImageSaveMode.SaveAsFileReference is specified for an image and a file for this image does not exist, a new file is created.

The SaveSettings.ImageExportFilterIndex and the Image.ExportFilterIndex properties can be used to save images in another than their original format. The SaveSettings.ImageExportFilterIndex property determines the export format for all images of the document and the Image.ExportFilterIndex property determines the export format for a certain image. TX Text Control supports the following formats to export images:

  • ExportFilterIndex 1: Windows Bitmap (.BMP)
  • ExportFilterIndex 2: Windows Metafile (.WMF)
  • ExportFilterIndex 3: Portable Network Graphics (.PNG)
  • ExportFilterIndex 4: JPEG File Interchange Format (.JPG, .JPEG)
  • ExportFilterIndex 5: Windows Enhanced Metafile (.EMF)
  • ExportFilterIndex 6: Tagged Image Format (.TIF)
  • ExportFilterIndex 7: Graphics Interchange Format (.GIF)

If an export format has been specified and a text filter does not support this format, the image is stored in the default format of the text filter. The following lists the image formats supported by each text filter. The first format (in bold) is the default format, used when the specified format is not possible:

  • RTF Format (Rich Text Format): PNG, BMP, WMF, JPG, EMF
  • Microsoft Word Format (.DOC version): WMF, BMP, PNG, JPG, EMF, TIF
  • Microsoft Word Format (Office Open XML version): PNG, BMP, WMF, JPG, EMF, TIF, GIF, SVG
  • HTML Format (Hypertext Markup Language): PNG, BMP, JPEG, GIF
  • Adobe Portable Document Format (PDF): BMP, JPG, the vector graphics EMF, WMF and SVG are converted to PostScript
  • TX Text Control Format: WMF, BMP, PNG, JPG, EMF, TIF, GIF, SVG

If images are exported in another format, either the binary data is embedded in the document or new files are created and references to these files are embedded in the document. TX Text Control creates these files with the following rules:

  • The SaveSettings.ImageSavePath property determines the location on the file system where the image files are created.
  • If the SaveSettings.ImageSavePath property has not been set, TX Text Control creates a sub-directory of the directory where the text document is stored. The name of this directory has the same name as the text document and _Imagesis appended. For example if the text document is c:\temp\sample.rtf, the name of the sub-directory is c:\temp\sample_Images\.
  • The image's filename is determined through the Image.ExportFileName property or can be set through the built-in TextControl.ImageAttributesDialog dialog box by the end-user.
  • If an export filename has not been specified, TX Text Control uses the original filename and exchanges the extension according to the exported format.
  • If a filename does not exist, TX Text Control uses the text document's filename and appends a number. For the example above, if the export format is Windows Bitmap, the images' filenames are sample_1.bmp, sample_2.bmp and so on.