public final class TextSegment extends Object
Represents segment of Pdf text.
The example demonstrates how to change text color and font size of the text withTextStateobject ofTextSegmentobject. // 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 doc.getPages().get(1).accept(absorber); // Change foreground color of the first text segment of the first text occurrence absorber.getTextFragments().get(1).getSegments().get(1).getTextState().setForegroundColor ( java.awt.Color.RED); // Change font size of the first text segment of the first text occurrence absorber.getTextFragments().get(1).getSegments().get_Item(1).getTextState().setFontSize ( 15); // Save document doc.save("D:\\Tests\\output.pdf");
In a few words,TextSegmentobjects are children ofTextFragmentobject. In details: Text of pdf document inAspose.Pdfis represented by two basic objects:TextFragmentandTextSegmentThe 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"); doc.getPages().get(1).accept(absorber);
Physically pdf text's representation is very complex. The text "hello world" may consist of
several physically 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.
| Constructor and Description |
|---|
TextSegment()
Creates TextSegment object.
|
TextSegment(String text)
Creates TextSegment object.
|
| Modifier and Type | Method and Description |
|---|---|
Position |
getBaselinePosition()
Gets text position for text, represented with
TextSegment object. |
CharInfoCollection |
getCharacters()
Gets collection of CharInfo objects that represent information on characters in the text
segment.
|
int |
getEndCharIndex()
Gets ending character index of current segment in the show text operator (Tj, TJ) segment.
|
Hyperlink |
getHyperlink()
Gets or sets the segment hyperlink(for pdf generator).
|
Position |
getPosition()
Gets text position for text, represented with
TextSegment object. |
Rectangle |
getRectangle()
Gets rectangle of the TextSegment
|
int |
getStartCharIndex()
Gets starting character index of current segment in the show text operator (Tj, TJ) segment.
|
String |
getText()
Gets
string text object that the TextSegment object represents. |
TextEditOptions |
getTextEditOptions()
Gets text edit options.
|
TextState |
getTextState()
Gets or sets text state for the text that
TextSegment object represents. |
void |
setBaselinePosition(Position value)
Sets text position for text, represented with
TextSegment object. |
void |
setHyperlink(Hyperlink value)
Gets or sets the segment hyperlink(for pdf generator).
|
void |
setPosition(Position value)
Sets text position for text, represented with
TextSegment object. |
void |
setText(String value)
Sets
string text object that the TextSegment object represents. |
void |
setTextEditOptions(TextEditOptions value)
Sets text edit options.
|
void |
setTextState(TextState value)
Sets text state for the text that
TextSegment object represents. |
void |
setTextSuppressedUpdate(String value)
Sets
string text object that the TextSegment object represents wish suppressed update. |
public TextSegment()
Creates TextSegment object.
The example demonstrates how to create text fragment object, add a text segment to the text fragment
collection and append it to the Pdf page.
Document doc = new Document(inFile);
Page page = (Page)doc.getPages().get(1);
// create text fragment
TextFragment tf = new TextFragment("main text");
tf.setPosition ( new Position(100, 600));
// set it's text properties
tf.getTextState().setFontSize ( 5);
tf.getTextState().setFont ( FontRepository.FindFont("TimesNewRoman"));
tf.getTextState().setBackgroundColor ( Color.GRAY);
tf.getTextState().setForegroundColor ( Color.RED);
// add one more segment to text fragment's Segments collection
TextSegment segment2 = new TextSegment();
segment2.setText ( "another segment");
tf.getSegments().add(segment2);
// create TextBuilder object
TextBuilder builder = new TextBuilder(page);
// append the text fragment to the Pdf page
builder.appendText(tf);
//save document
doc.save(outFile);
public TextSegment(String text)
Creates TextSegment object.
The example demonstrates how to create text fragment object, add a text segment to the text fragment
collection and append it to the Pdf page.
Document doc = new Document(inFile);
Page page = (Page)doc.getPages().get(1);
// create text fragment
TextFragment tf = new TextFragment("main text");
tf.setPosition ( new Position(100, 600));
// set it's text properties
tf.getTextState().setFontSize ( 5);
tf.getTextState().setFont ( FontRepository.FindFont("TimesNewRoman"));
tf.getTextState().setBackgroundColor ( Color.GRAY);
tf.getTextState().setForegroundColor ( Color.RED);
// add one more segment to text fragment's Segments collection
TextSegment segment2 = new TextSegment("another segment");
tf.getSegments().add(segment2);
// create TextBuilder object
TextBuilder builder = new TextBuilder(page);
// append the text fragment to the Pdf page
builder.appendText(tf);
//save document
doc.save(outFile);
text - Text segment's text.public int getStartCharIndex()
Gets starting character index of current segment in the show text operator (Tj, TJ) segment.
public int getEndCharIndex()
Gets ending character index of current segment in the show text operator (Tj, TJ) segment.
public String getText()
Gets string text object that the TextSegment object represents.
public void setTextSuppressedUpdate(String value)
Sets string text object that the TextSegment object represents wish suppressed update.
value - String valuepublic void setText(String value)
Sets string text object that the TextSegment object represents.
value - String valuepublic TextState getTextState()
Gets or sets text state for the text that TextSegment object represents.
Provides a way to change following properties of the text: Font FontSize FontStyle ForegroundColor BackgroundColor
public void setTextState(TextState value)
Sets text state for the text that TextSegment object represents.
Provides a way to change following properties of the text: Font FontSize FontStyle ForegroundColor BackgroundColor
value - TextState valuepublic Position getPosition()
Gets text position for text, represented with TextSegment object.
public void setPosition(Position value)
Sets text position for text, represented with TextSegment object.
value - Position valuepublic Rectangle getRectangle()
Gets rectangle of the TextSegment
public Position getBaselinePosition()
Gets text position for text, represented with TextSegment object. The YIndent of the
Position structure represents baseline coordinate of the text segment.
public void setBaselinePosition(Position value)
Sets text position for text, represented with TextSegment object. The YIndent of the
Position structure represents baseline coordinate of the text segment.
value - Position valuepublic TextEditOptions getTextEditOptions()
Gets text edit options. The options define special behavior when requested symbol cannot be written with font.
public void setTextEditOptions(TextEditOptions value)
Sets text edit options. The options define special behavior when requested symbol cannot be written with font.
value - TextEditOptions valuepublic CharInfoCollection getCharacters()
Gets collection of CharInfo objects that represent information on characters in the text segment.
public Hyperlink getHyperlink()
Gets or sets the segment hyperlink(for pdf generator).
public void setHyperlink(Hyperlink value)
Gets or sets the segment hyperlink(for pdf generator).
value - Hyperlink objectCopyright © 2025 Aspose. All Rights Reserved.