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("D:\Tests\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("D:\Tests\output.pdf");
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.
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 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. |
Modifier and Type | Method and Description |
---|---|
Object |
deepClone()
Clone the fragment.
|
Position |
getBaselinePosition()
Gets text position for text, represented with
TextFragment object. |
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. |
int |
getHorizontalAlignment()
Gets or sets 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. |
NewParagraphPlacementInfo |
getPlacementInfo()
Gets placement info
|
Position |
getPosition()
Gets text position for text, represented with
TextFragment object. |
Rectangle |
getRectangle()
returns rectangle of the TextFragment
|
TextSegmentCollection |
getSegments()
Gets text segments for current
TextFragment . |
String |
getText()
Gets or sets
string text object that the TextFragment object represents. |
TextFragmentState |
getTextState()
Gets or sets text state for the text that
TextFragment object represents. |
int |
getWrapLinesCount()
Gets wrap lines count for this paragraph(for pdf generation only)
|
void |
setBaselinePosition(Position value) |
void |
setHorizontalAlignment(int value)
Sets a horizontal alignment of paragraph
|
void |
setPlacementInfo(NewParagraphPlacementInfo value)
Sets placement info
|
void |
setPosition(Position value) |
void |
setSegments(TextSegmentCollection value) |
void |
setText(String value) |
void |
setVerticalAlignment(int value)
Sets a vertical alignment of paragraph
|
void |
setWrapLinesCount(int value)
Sets wrap lines count for this paragraph(for pdf generation only)
|
getMargin, isKeptWithNext, isKeptWithNext, setMargin
public TextFragment()
Initializes new instance of the TextFragment
object.
public TextFragment(String text)
Creates TextFragment
object with single TextSegment
object inside.
Specifies text string inside the segment.
text
- Text fragment's text.public String getText()
Gets or sets string
text object that the TextFragment
object represents.
The example demonstrates how to search a text and replace first occurrence represented withTextFragment
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
,
IDocument
public void setText(String value)
public void setVerticalAlignment(int value)
BaseParagraph
setVerticalAlignment
in class BaseParagraph
value
- New vertical alignment value.public int getHorizontalAlignment()
Gets or sets a horizontal alignment of text fragment.
public void setHorizontalAlignment(int value)
BaseParagraph
setHorizontalAlignment
in class BaseParagraph
value
- New value of horizontal alignment.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 withTextState
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");
TextFragmentAbsorber
,
IDocument
public TextSegmentCollection getSegments()
Gets text segments for current TextFragment
.
The example demonstrates how to navigate allTextSegment
objects 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 : absorber.getTextFragments().get_Item(1).getSegments()) { System.out.println(string.Format("segment text: {0}", segment.Text)); System.out.println(string.Format("segment X indent: {0}", segment.Position.XIndent)); System.out.println(string.Format("segment Y indent: {0}", segment.Position.YIndent)); }
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
,
TextSegment
public void setSegments(TextSegmentCollection value)
public Position getPosition()
Gets text position for text, represented with TextFragment
object.
The example demonstrates how to view placement of a text, represented byTextFragment
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(string.Format("fragment text: {0}", firstOccurrence.Text)); System.out.println(string.Format("fragment X indent: {0}", firstOccurrence.Position.XIndent)); System.out.println(string.Format("fragment Y indent: {0}", firstOccurrence.Position.YIndent));
TextFragmentAbsorber
,
IDocument
,
TextSegment
public void setPosition(Position value)
public 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)
public Rectangle getRectangle()
returns rectangle of the TextFragment
public Page getPage()
public XForm getForm()
public NewParagraphPlacementInfo getPlacementInfo()
public void setPlacementInfo(NewParagraphPlacementInfo value)
public Object deepClone()
Clone the fragment.
deepClone
in class BaseParagraph
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 valueCopyright © 2018 Aspose. All Rights Reserved.