public class TextFragment extends BaseParagraph
Represents fragment of Pdf text.
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("input.pdf");
// Find font that will be used to change document text font
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.getPages().get(1).accept(absorber);
// Change text and font of the first text occurrence
absorber.getTextFragments().get_Item(1).setText ( "hi world");
absorber.getTextFragments().get_Item(1).getTextState().setFont ( font);
// Save document
doc.save("output.pdf");
In a few words,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 thatTextFragmentobject contains list ofTextSegmentobjects. In details: Text of pdf document incom.aspose.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);
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.
| Constructor and Description |
|---|
TextFragment()
Initializes new instance of the
TextFragment object. |
TextFragment(String text)
Creates
TextFragment object with single TextSegment object inside. |
TextFragment(String text,
TabStops tabStops)
Creates
TextFragment object with single TextSegment object inside and
predefined TabStops positions. |
TextFragment(TabStops tabStops)
Initializes new instance of the
TextFragment object with predefined TabStops
positions. |
| Modifier and Type | Method and Description |
|---|---|
Object |
cloneWithSegments()
Clone the fragment with all segments.
|
Object |
deepClone()
Clone the fragment.
|
Position |
getBaselinePosition()
Gets text position for text, represented with
TextFragment object. |
Note |
getEndNote()
Gets the paragraph end note.(for pdf generation only)
|
Note |
getFootNote()
Gets the paragraph foot note.(for pdf generation only)
|
XForm |
getForm()
Gets form object that contains the TextFragment
The value can be null in case the TextFragment object doesn't belong to a form. |
HorizontalAlignment |
getHorizontalAlignment()
Gets a horizontal alignment of text fragment.
|
Page |
getPage()
Gets page that contains the TextFragment
The value can be null in case the TextFragment object doesn't belong to any page. |
Position |
getPosition()
Gets text position for text, represented with
TextFragment object. |
Rectangle |
getRectangle()
Gets rectangle of the TextFragment
|
TextReplaceOptions |
getReplaceOptions()
Gets text replace options.
|
TextSegmentCollection |
getSegments()
Gets text segments for current
TextFragment. |
String |
getText()
Gets
string text object that the TextFragment object represents. |
TextEditOptions |
getTextEditOptions()
Gets or sets text edit options.
|
TextFragmentState |
getTextState()
Gets or sets text state for the text that
TextFragment object represents. |
VerticalAlignment |
getVerticalAlignment()
Gets a vertical alignment of text fragment.
|
int |
getWrapLinesCount()
Gets wrap lines count for this paragraph(for pdf generation only)
|
TextSegmentCollection |
isolateTextSegments(int startIndex,
int length)
Gets
TextSegment(s) representing specified part of the TextFragment text. |
void |
setBaselinePosition(Position value)
Sets text position for text, represented with
TextFragment object. |
void |
setEndNote(Note value)
Sets the paragraph end note.(for pdf generation only)
|
void |
setFootNote(Note value)
Sets the paragraph foot note.(for pdf generation only)
|
void |
setHorizontalAlignment(HorizontalAlignment value)
Sets a horizontal alignment of text fragment.
|
void |
setHyperlink(Hyperlink value)
Sets the fragment hyperlink
|
void |
setMarkedContentProperties(String name,
int id) |
void |
setPosition(Position value)
Sets text position for text, represented with
TextFragment object. |
void |
setRectangle(Rectangle value)
Gets rectangle of the TextFragment
|
void |
setSegments(TextSegmentCollection value)
Represent setSegments method
|
void |
setText(String value)
Sets
string text object that the TextFragment object represents. |
void |
setTextEditOptions(TextEditOptions value)
Gets or sets text edit options.
|
void |
setVerticalAlignment(VerticalAlignment value)
Sets a vertical alignment of text fragment.
|
void |
setWrapLinesCount(int value)
Sets wrap lines count for this paragraph(for pdf generation only)
|
getHyperlink, getMargin, getZIndex, isFirstParagraphInColumn, isInLineParagraph, isInNewPage, isKeptWithNext, setFirstParagraphInColumn, setInLineParagraph, setInNewPage, setKeptWithNext, setMargin, setZIndexpublic TextFragment()
Initializes new instance of the TextFragment object.
public TextFragment(TabStops tabStops)
Initializes new instance of the TextFragment object with predefined TabStops
positions.
tabStops - Tabulation positionspublic TextFragment(String text)
Creates TextFragment object with single TextSegment object inside. Specifies
text string inside the segment.
text - Text fragment's text.public final TextReplaceOptions getReplaceOptions()
Gets text replace options. The options define behavior when fragment text is replaced to more short/long.
public String getText()
Gets string text object that the TextFragment object represents.
The example demonstrates how to search a text and replace first occurrence represented with
TextFragment 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 font of the first text occurrence
absorber.getTextFragments().get_Item(1).setText ( "hi world");
// Save document
doc.save("D:\\Tests\\output.pdf");
TextFragmentAbsorber,
IDocumentpublic void setText(String value)
Sets string text object that the TextFragment object represents.
value - String value
The example demonstrates how to search a text and replace first occurrence represented with
TextFragment 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 font of the first text occurrence
absorber.getTextFragments().get_Item(1).setText ( "hi world");
// Save document
doc.save("D:\\Tests\\output.pdf");
TextFragmentAbsorber,
IDocumentpublic VerticalAlignment getVerticalAlignment()
Gets a vertical alignment of text fragment.
getVerticalAlignment in class BaseParagraphVerticalAlignmentpublic void setVerticalAlignment(VerticalAlignment value)
Sets a vertical alignment of text fragment.
setVerticalAlignment in class BaseParagraphvalue - int valueVerticalAlignmentpublic HorizontalAlignment getHorizontalAlignment()
Gets a horizontal alignment of text fragment.
getHorizontalAlignment in class BaseParagraphHorizontalAlignmentpublic void setHorizontalAlignment(HorizontalAlignment value)
Sets a horizontal alignment of text fragment.
setHorizontalAlignment in class BaseParagraphvalue - HorizontalAlignment valueHorizontalAlignmentpublic void setHyperlink(Hyperlink value)
Sets the fragment hyperlink
setHyperlink in class BaseParagraphvalue - hyperlink(for pdf generator).public TextFragmentState getTextState()
Gets or sets text state for the text that TextFragment object represents.
The example demonstrates how to change text color and font size of the text with TextState 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 occurrence
absorber.getTextFragments().get_Item(1).getTextState().setForegroundColor(Color.RED);
// Change font size of the first text occurrence
absorber.getTextFragments().get_Item(1).getTextState().setFontSize ( 15);
// Save document
doc.save("D:\\Tests\\output.pdf");
Provides a way to change following properties of the text: Font FontSize FontStyle ForegroundColor BackgroundColor
TextFragmentAbsorber,
IDocumentpublic TextSegmentCollection getSegments()
Gets text segments for current TextFragment.
The example demonstrates how to navigate allTextSegmentobjects insideTextFragment. // 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); // Navigate all text segments and out their text and placement info for (TextSegment segment : (Iterable<TextSegment>)absorber.getTextFragments().get_Item(1).getSegments()) { System.out.println("segment text: "+ segment.getText())); System.out.println("segment X indent: "+ segment.getPosition().getXIndent())); System.out.println("segment Y indent: "+ segment.getPosition().getYIndent())); }
In a few words, TextSegment objects are children of TextFragment object.
Advanced users may access segments directly to perform more complex text edit scenarios. For
details, please look at TextFragment object description.
TextFragmentAbsorber,
IDocument,
TextSegmentpublic void setSegments(TextSegmentCollection value)
Represent setSegments method
value - TextSegmentCollection valuepublic Position getPosition()
Gets text position for text, represented with TextFragment object.
The example demonstrates how to view placement of a text, represented by TextFragment 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);
// View text and placement info of first text occurrence
TextFragment firstOccurrence = absorber.getTextFragments().get_Item(1);
System.out.println("fragment text: " + firstOccurrence.getText()));
System.out.println("fragment X indent: "+ firstOccurrence.getPosition().getXIndent()));
System.out.println("fragment Y indent: "+ firstOccurrence.getPosition().getYIndent()));
TextFragmentAbsorber,
IDocument,
TextSegmentpublic void setPosition(Position value)
Sets text position for text, represented with TextFragment object.
value - Position value
The example demonstrates how to view placement of a text, represented by
TextFragment
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);
// View text and placement info of first text occurrence
TextFragment firstOccurrence = absorber.getTextFragments().get_Item(1);
System.out.println("fragment text: " + firstOccurrence.getText()));
System.out.println("fragment X indent: "+ firstOccurrence.getPosition().getXIndent()));
System.out.println("fragment Y indent: "+ firstOccurrence.getPosition().getYIndent()));
TextFragmentAbsorber,
IDocument,
TextSegmentpublic Position getBaselinePosition()
Gets text position for text, represented with TextFragment object. The YIndent of the
Position structure represents baseline coordinate of the text fragment.
public void setBaselinePosition(Position value)
Sets text position for text, represented with TextFragment object. The YIndent of the
Position structure represents baseline coordinate of the text fragment.
value - Position valuepublic Rectangle getRectangle()
Gets rectangle of the TextFragment
public void setRectangle(Rectangle value)
Gets rectangle of the TextFragment
value - Rectangle instancepublic Page getPage()
Gets page that contains the TextFragment
The value can be null in case the TextFragment object doesn't belong to any page.
public XForm getForm()
Gets form object that contains the TextFragment
The value can be null in case the TextFragment object doesn't belong to a form.
public int getWrapLinesCount()
Gets wrap lines count for this paragraph(for pdf generation only)
public void setWrapLinesCount(int value)
Sets wrap lines count for this paragraph(for pdf generation only)
value - int valuepublic Note getEndNote()
Gets the paragraph end note.(for pdf generation only)
public void setEndNote(Note value)
Sets the paragraph end note.(for pdf generation only)
value - Note valuepublic Note getFootNote()
Gets the paragraph foot note.(for pdf generation only)
public void setFootNote(Note value)
Sets the paragraph foot note.(for pdf generation only)
value - Note valuepublic final TextEditOptions getTextEditOptions()
Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font.
public final void setTextEditOptions(TextEditOptions value)
Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font.
value - TextEditOptions instancepublic TextSegmentCollection isolateTextSegments(int startIndex, int length)
Gets TextSegment(s) representing specified part of the TextFragment text.
startIndex - Position in text from which new TextSegment(s) will start.length - Length of the text that will isolated into TextSegment(s).TextSegmentCollection containing text segments represeting text substring starting at a
specifing position and having a specified length.public void setMarkedContentProperties(String name, int id)
public Object deepClone()
Clone the fragment.
deepClone in interface com.aspose.ms.System.ICloneabledeepClone in class BaseParagraphpublic Object cloneWithSegments()
Clone the fragment with all segments.
Copyright © 2025 Aspose. All Rights Reserved.