TextFragment
Inheritance: java.lang.Object, com.aspose.pdf.BaseParagraph com.aspose.pdf.TextFragment, com.aspose.pdf.BaseParagraph, com.aspose.pdf.TextFragment
All Implemented Interfaces: com.aspose.ms.System.ICloneable
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, {@code TextFragment} object contains list of {@code TextSegment} objects. In details: Text of pdf document in {@code com.aspose.pdf} is represented by two basic objects: {@code TextFragment} and {@code 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.getPages().get(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 {@code TextFragment} object provides single logic operation set over physical {@code TextSegment} objects set that represent user's query. In text search scenario, {@code TextFragment} is logical "hello world" text representation, and {@code TextSegment} object collection represents all physical segments that construct "hello world" text object. So, {@code TextFragment} is close to logical text representation. And {@code TextSegment} is close to physical text representation. Obviously each {@code TextSegment} object may have it's own font, coloring, positioning properties. {@code TextFragment} provides simple way to change text with it's properties: set font, set font size, set font color etc. Meanwhile {@code TextSegment} objects are accessible and users are able to operate with {@code TextSegment} objects independently.Note that changing TextFragment properties may change inner {@code 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 {@code Segments} collection unchanged, please change inner segments individually.
Constructors
| Constructor | Description |
|---|---|
| TextFragment | Initializes new instance of the {@code TextFragment} object. |
| TextFragment | Initializes new instance of the {@code TextFragment} object. |
| TextFragment | Initializes new instance of the {@code TextFragment} object. |
| TextFragment | Initializes new instance of the {@code TextFragment} object. |
Methods
| Method | Description |
|---|---|
| cloneWithSegments | Clone the fragment with all segments. |
| deepClone | Clone the fragment. |
| getBaselinePosition | Gets text position for text, represented with {@code TextFragment} object. The YIndent of the Position structure represents baseline coordinate of the text fragment. |
| getEndNote | Gets the paragraph end note.(for pdf generation only) |
| getFootNote | Gets the paragraph foot note.(for pdf generation only) |
| getForm | Gets form object that contains the TextFragment The value can be null in case the TextFragment object doesn’t belong to a form. |
| getHorizontalAlignment | Gets a horizontal alignment of text fragment. |
| getPage | Gets page that contains the TextFragment The value can be null in case the TextFragment object doesn’t belong to any page. |
| getPosition | Gets text position for text, represented with {@code TextFragment} object. |
| getRectangle | Gets rectangle of the TextFragment |
| getReplaceOptions | Gets text replace options. The options define behavior when fragment text is replaced to more short/long. |
| getSegments | Gets text segments for current {@code TextFragment}. |
| getText | Gets {@code string} text object that the {@code TextFragment} object represents. |
| getTextEditOptions | Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font. |
| getTextState | Gets or sets text state for the text that {@code TextFragment} object represents. |
| getVerticalAlignment | Gets a vertical alignment of text fragment. |
| getWrapLinesCount | Gets wrap lines count for this paragraph(for pdf generation only) |
| isolateTextSegments | Gets {@code TextSegment}(s) representing specified part of the {@code TextFragment} text. |
| setBaselinePosition | Sets text position for text, represented with {@code TextFragment} object. The YIndent of the Position structure represents baseline coordinate of the text fragment. |
| setEndNote | Sets the paragraph end note.(for pdf generation only) |
| setFootNote | Sets the paragraph foot note.(for pdf generation only) |
| setHorizontalAlignment | Sets a horizontal alignment of text fragment. |
| setHyperlink | Sets the fragment hyperlink |
| setMarkedContentProperties | |
| setPosition | Sets text position for text, represented with {@code TextFragment} object. |
| setRectangle | Gets rectangle of the TextFragment |
| setSegments | Represent setSegments method |
| setText | Sets {@code string} text object that the {@code TextFragment} object represents. |
| setTextEditOptions | Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font. |
| setVerticalAlignment | Sets a vertical alignment of text fragment. |
| setWrapLinesCount | Sets wrap lines count for this paragraph(for pdf generation only) |
TextFragment
public TextFragment()
Initializes new instance of the {@code TextFragment} object.
TextFragment
Initializes new instance of the {@code TextFragment} object.
TextFragment
Initializes new instance of the {@code TextFragment} object.
TextFragment
Initializes new instance of the {@code TextFragment} object.
cloneWithSegments
public Object cloneWithSegments()
Clone the fragment with all segments.
Returns: The cloned object
deepClone
public Object deepClone()
Clone the fragment.
Returns: The cloned object
getBaselinePosition
public Position getBaselinePosition()
Gets text position for text, represented with {@code TextFragment} object. The YIndent of the Position structure represents baseline coordinate of the text fragment.
Returns: Position value
getEndNote
public Note getEndNote()
Gets the paragraph end note.(for pdf generation only)
Returns: Note value
getFootNote
public Note getFootNote()
Gets the paragraph foot note.(for pdf generation only)
Returns: Note value
getForm
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.
Returns: XForm value
getHorizontalAlignment
public HorizontalAlignment getHorizontalAlignment()
Gets a horizontal alignment of text fragment.
Returns: HorizontalAlignment value @see HorizontalAlignment
getPage
public Page getPage()
Gets page that contains the TextFragment The value can be null in case the TextFragment object doesn’t belong to any page.
Returns: Page object
getPosition
public Position getPosition()
Gets text position for text, represented with {@code TextFragment} object.
Returns: Position value
The example demonstrates how to view placement of a text, represented by {@code 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())); @see TextFragmentAbsorber @see IDocument @see TextSegmentgetRectangle
public Rectangle getRectangle()
Gets rectangle of the TextFragment
Returns: Rectangle object
getReplaceOptions
public final TextReplaceOptions getReplaceOptions()
Gets text replace options. The options define behavior when fragment text is replaced to more short/long.
Returns: TextReplaceOptions instance
getSegments
public TextSegmentCollection getSegments()
Gets text segments for current {@code TextFragment}.
Returns: TextSegmentCollection value
The example demonstrates how to navigate all {@code TextSegment} objects inside {@code TextFragment}. // 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 : ({@code Iterable})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, {@code TextSegment} objects are children of {@code TextFragment} object. Advanced users may access segments directly to perform more complex text edit scenarios. For details, please look at {@code TextFragment} object description.
@see TextFragmentAbsorber @see IDocument @see TextSegmentgetText
public String getText()
Gets {@code string} text object that the {@code TextFragment} object represents.
Returns: String value
The example demonstrates how to search a text and replace first occurrence represented with {@code 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”); @see TextFragmentAbsorber @see IDocumentgetTextEditOptions
public final TextEditOptions getTextEditOptions()
Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font.
Returns: TextEditOptions instance
getTextState
public TextFragmentState getTextState()
Gets or sets text state for the text that {@code TextFragment} object represents.
Returns: TextFragmentState object
The example demonstrates how to change text color and font size of the text with {@code 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
@see TextFragmentAbsorber @see IDocumentgetVerticalAlignment
public VerticalAlignment getVerticalAlignment()
Gets a vertical alignment of text fragment.
Returns: int value @see VerticalAlignment
getWrapLinesCount
public int getWrapLinesCount()
Gets wrap lines count for this paragraph(for pdf generation only)
Returns: int value
isolateTextSegments
public TextSegmentCollection isolateTextSegments(int startIndex, int length)
Gets {@code TextSegment}(s) representing specified part of the {@code TextFragment} text.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| startIndex | Position in text from which new {@code TextSegment}(s) will start. | |
| length | Length of the text that will isolated into {@code TextSegment}(s). |
Returns: {@code TextSegmentCollection} containing text segments represeting text substring starting at a specifing position and having a specified length.
setBaselinePosition
Sets text position for text, represented with {@code TextFragment} object. The YIndent of the Position structure represents baseline coordinate of the text fragment.
setEndNote
Sets the paragraph end note.(for pdf generation only)
setFootNote
Sets the paragraph foot note.(for pdf generation only)
setHorizontalAlignment
Sets a horizontal alignment of text fragment.
setHyperlink
Sets the fragment hyperlink
setMarkedContentProperties
setPosition
Sets text position for text, represented with {@code TextFragment} object.
setRectangle
Gets rectangle of the TextFragment
setSegments
Represent setSegments method
setText
Sets {@code string} text object that the {@code TextFragment} object represents.
setTextEditOptions
Gets or sets text edit options. The options define special behavior when requested symbol cannot be written with font.
setVerticalAlignment
Sets a vertical alignment of text fragment.
setWrapLinesCount
public void setWrapLinesCount(int value)
Sets wrap lines count for this paragraph(for pdf generation only)
Parameters:
| Parameter | Type | Description |
|---|---|---|
| value | int value |