TextFragment class

Represents fragment of Pdf text.

public class TextFragment : BaseParagraph


Name Description
TextFragment() Initializes new instance of the TextFragment object.
TextFragment(string) Creates TextFragment object with single TextSegment object inside. Specifies text string inside the segment.
TextFragment(TabStops) Initializes new instance of the TextFragment object with predefined TabStops positions.
TextFragment(string, TabStops) Creates TextFragment object with single TextSegment object inside and predefined TabStops positions.


Name Description
BaselinePosition { get; set; } Gets text position for text, represented with TextFragment object. The YIndent of the Position structure represents baseline coordinate of the text fragment.
EndNote { get; set; } Gets or sets the paragraph end note.(for pdf generation only)
FootNote { get; set; } Gets or sets the paragraph foot note.(for pdf generation only)
Form { get; } Gets form object that contains the TextFragment
override HorizontalAlignment { get; set; } Gets or sets a horizontal alignment of text fragment.
override Hyperlink { set; } Sets the fragment hyperlink
IsFirstParagraphInColumn { get; set; } Gets or sets a bool value that indicates whether this paragraph will be at next column. Default is false.(for pdf generation)
IsInLineParagraph { get; set; } Gets or sets a paragraph is inline. Default is false.(for pdf generation)
IsInNewPage { get; set; } Gets or sets a bool value that force this paragraph generates at new page. Default is false.(for pdf generation)
IsKeptWithNext { get; set; } Gets or sets a bool value that indicates whether current paragraph remains in the same page along with next paragraph. Default is false.(for pdf generation)
Margin { get; set; } Gets or sets a outer margin for paragraph (for pdf generation)
Page { get; } Gets page that contains the TextFragment
Position { get; set; } Gets or sets text position for text, represented with TextFragment object.
Rectangle { get; } Gets rectangle of the TextFragment
ReplaceOptions { get; } Gets text replace options. The options define behavior when fragment text is replaced to more short/long.
Segments { get; set; } Gets text segments for current TextFragment.
Text { get; set; } Gets or sets String text object that the TextFragment object represents.
TextState { get; } Gets or sets text state for the text that TextFragment object represents.
override VerticalAlignment { get; set; } Gets or sets a vertical alignment of text fragment.
WrapLinesCount { get; set; } Gets or sets wrap lines count for this paragraph(for pdf generation only)
ZIndex { get; set; } Gets or sets a int value that indicates the Z-order of the graph. A graph with larger ZIndex will be placed over the graph with smaller ZIndex. ZIndex can be negative. Graph with negative ZIndex will be placed behind the text in the page.


Name Description
override Clone() Clone the fragment.
virtual CloneWithSegments() Clone the fragment with all segments.
IsolateTextSegments(int, int) Gets TextSegment(s) representing specified part of the TextFragment text.


In a few words, TextFragment object contains list of TextSegment objects. 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. Note that changing TextFragment properties may change inner Segments collection because TextFragment is an aggregate object and it may rearrange internal segments or merge them into single segment. If your requirement is to leave the Segments collection unchanged, please change inner segments individually.


The example demonstrates how to find text on the first PDF document page and replace the text and it’s font.

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

// Find font that will be used to change document text font
Aspose.Pdf.Txt.Font font = FontRepository.FindFont("Arial");

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

// Accept the absorber for first page

// Change text and font of the first text occurrence
absorber.TextFragments[1].Text = "hi world";
absorber.TextFragments[1].TextState.Font = font;

// Save document

See Also