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 withTextState
object ofTextSegment
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 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");
TextSegment
objects are children of TextFragment
object.
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.
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.
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.
|
Position |
getPosition()
Gets text position for text, represented with
TextSegment object. |
Rectangle |
getRectangle()
Gets rectangle of the TextSegment
|
String |
getText()
Gets or sets
string text object that the TextSegment object represents. |
TextEditOptions |
getTextEditOptions()
Gets or sets text edit options.
|
TextState |
getTextState()
Gets or sets text state for the text that
TextSegment object represents. |
void |
setBaselinePosition(Position value) |
void |
setPosition(Position value) |
void |
setText(String value) |
void |
setTextEditOptions(TextEditOptions value) |
void |
setTextState(TextState value) |
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.Pages[1]; // create text fragment TextFragment tf = new TextFragment("main text"); tf.Position = 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 String getText()
Gets or sets string
text object that the TextSegment
object represents.
public void setText(String value)
public TextState getTextState()
Gets or sets text state for the text that TextSegment
object represents.
public void setTextState(TextState value)
public Position getPosition()
Gets text position for text, represented with TextSegment
object.
public void setPosition(Position value)
public 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)
public TextEditOptions getTextEditOptions()
Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font.
public void setTextEditOptions(TextEditOptions value)
public CharInfoCollection getCharacters()
Gets collection of CharInfo objects that represent information on characters in the text segment.
Copyright © 2020 Aspose. All Rights Reserved.