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:

[C#]
public bool Add();
public bool Add(Image image, int textPosition);
[Visual Basic]
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 a dialog 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:

[C#]
public bool Add(Image image, HorizontalAlignment alignment, int textPosition, ImageInsertionMode insertionMode);
public bool Add(Image image, Point location, int textPosition, ImageInsertionMode insertionMode);
[Visual Basic]
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:

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:

[C#]
Paragraph p = textControl1.Paragraphs[5];
textControl1.Images.Add(new Image(), HorizontalAlignment.Left, p.Start-1, ImageInsertionMode.DisplaceText);
[Visual Basic]
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 no anchor position and the image remains always at the same location:

[C#]
public bool Add(Image image, int page, Point location, ImageInsertionMode insertionMode);
public bool Add(Image image, Point location, ImageInsertionMode insertionMode);
[Visual Basic]
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 the pages. 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.