TextFragment Class
Represents fragment of Pdf text.
Inheritance Hierarchy

Namespace: Aspose.Pdf.Text
Assembly: Aspose.PDF (in Aspose.PDF.dll) Version: 20.3
Syntax
public class TextFragment : BaseParagraph

The TextFragment type exposes the following members.

Constructors
  NameDescription
Public methodTextFragment
Initializes new instance of the TextFragment object.
Public methodTextFragment(String)
Creates TextFragment object with single TextSegment object inside. Specifies text string inside the segment.
Public methodTextFragment(TabStops)
Initializes new instance of the TextFragment object with predefined TabStops positions.
Public methodTextFragment(String, TabStops)
Creates TextFragment object with single TextSegment object inside and predefined TabStops positions.
Properties
  NameDescription
Public propertyBaselinePosition
Gets text position for text, represented with TextFragment object. The YIndent of the Position structure represents baseline coordinate of the text fragment.
Public propertyEndNote
Gets or sets the paragraph end note.(for pdf generation only)
Public propertyFootNote
Gets or sets the paragraph foot note.(for pdf generation only)
Public propertyForm
Gets form object that contains the TextFragment
Public propertyHorizontalAlignment
Gets or sets a horizontal alignment of text fragment.
(Overrides BaseParagraphHorizontalAlignment.)
Public propertyHyperlink
Sets the fragment hyperlink
(Overrides BaseParagraphHyperlink.)
Public propertyIsFirstParagraphInColumn
Gets or sets a bool value that indicates whether this paragraph will be at next column. Default is false.(for pdf generation)
(Inherited from BaseParagraph.)
Public propertyIsInLineParagraph
Gets or sets a paragraph is inline. Default is false.(for pdf generation)
(Inherited from BaseParagraph.)
Public propertyIsInNewPage
Gets or sets a bool value that force this paragraph generates at new page. Default is false.(for pdf generation)
(Inherited from BaseParagraph.)
Public propertyIsKeptWithNext
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)
(Inherited from BaseParagraph.)
Public propertyMargin
Gets or sets a outer margin for paragraph (for pdf generation)
(Inherited from BaseParagraph.)
Public propertyPage
Gets page that contains the TextFragment
Public propertyCode examplePosition
Gets or sets text position for text, represented with TextFragment object.
Public propertyRectangle
Gets rectangle of the TextFragment
Public propertyCode exampleSegments
Gets text segments for current TextFragment.
Public propertyCode exampleText
Gets or sets String text object that the TextFragment object represents.
Public propertyCode exampleTextState
Gets or sets text state for the text that TextFragment object represents.
Public propertyVerticalAlignment
Gets or sets a vertical alignment of text fragment.
(Overrides BaseParagraphVerticalAlignment.)
Public propertyWrapLinesCount
Gets or sets wrap lines count for this paragraph(for pdf generation only)
Public propertyZIndex
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.
(Inherited from BaseParagraph.)
Methods
  NameDescription
Public methodClone
Clone the fragment.
(Overrides BaseParagraphClone.)
Public methodCloneWithSegments
Clone the fragment with all segments.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodIsolateTextSegments
Gets TextSegment(s) representing specified part of the TextFragment text.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString (Inherited from Object.)
Remarks
In a few words, TextFragment object contains list of TextSegment objects. In details: Text of pdf document in Aspose.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.
C#
Document doc = new Document(docFile);
TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
doc.Pages[1].Accept(absorber);
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.
Examples
The example demonstrates how to find text on the first PDF document page and replace the text and it's font.
C#
// 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
doc.Pages[1].Accept(absorber);

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

// Save document
doc.Save(@"D:\Tests\output.pdf");
See Also