TextSegment class

Represents segment of Pdf text.

public sealed class TextSegment


TextSegment()Creates TextSegment object.
TextSegment(string)Creates TextSegment object.


BaselinePosition { get; set; }Gets text position for text, represented with TextSegment object. The YIndent of the Position structure represents baseline coordinate of the text segment.
Characters { get; }Gets collection of CharInfo objects that represent information on characters in the text segment.
EndCharIndex { get; }Gets ending character index of current segment in the show text operator (Tj, TJ) segment.
Hyperlink { get; set; }Gets or sets the segment hyperlink(for pdf generator).
Position { get; set; }Gets text position for text, represented with TextSegment object.
Rectangle { get; }Gets rectangle of the TextSegment
StartCharIndex { get; }Gets starting character index of current segment in the show text operator (Tj, TJ) segment.
Text { get; set; }Gets or sets String text object that the TextSegment object represents.
TextEditOptions { get; set; }Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font.
TextState { get; set; }Gets or sets text state for the text that TextSegment object represents.


static MyHtmlEncode(string)Encodes string as html.


In a few words, TextSegment objects are children of TextFragment object. In details: Text of pdf document in Pdf is represented by two basic objects: TextFragment and TextSegment The differences between them is mostly context-dependent. Let’s consider following scenario. User searches text “hello world” to operate with it, change it’s properties, look etc.

Document doc = new Document(docFile);
TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");

Phisycally pdf text’s representation is very complex. The text “hello world” may consist of several phisycally independent text segments. The Aspose.Pdf text model basically establishes that TextFragment object provides single logic operation set over physical TextSegment objects set that represent user’s query. In text search scenario, TextFragment is logical “hello world” text representation, and TextSegment object collection represents all physical segments that construct “hello world” text object. So, TextFragment is close to logical text representation. And TextSegment is close to physical text representation. Obviously each TextSegment object may have it’s own font, coloring, positioning properties. TextFragment provides simple way to change text with it’s properties: set font, set font size, set font color etc. Meanwhile TextSegment objects are accessible and users are able to operate with TextSegment objects independently.


The example demonstrates how to change text color and font size of the text with TextState object of TextSegment object.

// Open document
Document doc = new Document(@"D:\Tests\input.pdf");

// Create TextFragmentAbsorber object to find all "hello world" text occurrences
TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");

// Accept the absorber for first page

// Change foreground color of the first text segment of the first text occurrence
absorber.TextFragments[1].Segments[1].TextState.ForegroundColor = Color.FromRgb(System.Drawing.Color.Red);
// Change font size of the first text segment of the first text occurrence
absorber.TextFragments[1].Segments[1].TextState.FontSize = 15;

// Save document

See Also