com.aspose.pdf

Interfaces

Classes

Enums

Exceptions

com.aspose.pdf

Class TextSegment



  • 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 with TextState object of TextSegment 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");  
      
     

     In a few words, 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.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 Detail

      • TextSegment

        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);
         
      • TextSegment

        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);
         
        Parameters:
        text - Text segment's text.
    • Method Detail

      • getStartCharIndex

        public int getStartCharIndex()

        Gets starting character index of current segment in the show text operator (Tj, TJ) segment.

        Returns:
        int value
      • getText

        public String getText()

        Gets string text object that the TextSegment object represents.

        Returns:
        String value
      • setText

        public void setText(String value)

        Sets string text object that the TextSegment object represents.

        Parameters:
        value - String value
      • getTextState

        public 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

        Returns:
        TextState value
      • setTextState

        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

        Parameters:
        value - TextState value
      • getPosition

        public Position getPosition()

        Gets text position for text, represented with TextSegment object.

        Returns:
        Position value
      • setPosition

        public void setPosition(Position value)

        Sets text position for text, represented with TextSegment object.

        Parameters:
        value - Position value
      • getRectangle

        public Rectangle getRectangle()

        Gets rectangle of the TextSegment

        Returns:
        Rectangle object
      • getBaselinePosition

        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.

        Returns:
        Position value
      • setBaselinePosition

        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.

        Parameters:
        value - Position value
      • getTextEditOptions

        public TextEditOptions getTextEditOptions()

        Gets text edit options. The options define special behavior when requested symbol cannot be written with font.

        Returns:
        TextEditOptions value
      • setTextEditOptions

        public void setTextEditOptions(TextEditOptions value)

        Sets text edit options. The options define special behavior when requested symbol cannot be written with font.

        Parameters:
        value - TextEditOptions value
      • getCharacters

        public CharInfoCollection getCharacters()

        Gets collection of CharInfo objects that represent information on characters in the text segment.

        Returns:
        CharInfoCollection object
      • getHyperlink

        public Hyperlink getHyperlink()

        Gets or sets the segment hyperlink(for pdf generator).

        Returns:
        Hyperlink object
      • setHyperlink

        public void setHyperlink(Hyperlink value)

        Gets or sets the segment hyperlink(for pdf generator).

        Parameters:
        value - Hyperlink object