Highlighting words in PDF based of the user input (in JAVA).

Q: We have the requirement to highlight the word in the PDF on the
basis of user input (In JAVA). We are using the PDFTron's
TextExtractor.Word class for this purpose. For example,

Scenario 1: if user specifies a word "chan", than we want that only
this text (i.e. 'chan' ) is highlighted in the PDF.

Right now we are actually getting that text like "change", "chance",
"changing" are also been highlighted in the PDF. We don't want these
texts to be highlighted.

We want to extract and highlight the exact word (i.e. chan) from PDF.

Also please find the code snippet with this message:

package src;

import java.util.Iterator;

import pdftron.Common.PDFNetException;
import pdftron.PDF.Annot;
import pdftron.PDF.ColorPt;
import pdftron.PDF.ColorSpace;
import pdftron.PDF.Element;
import pdftron.PDF.ElementBuilder;
import pdftron.PDF.ElementReader;
import pdftron.PDF.ElementWriter;
import pdftron.PDF.GState;
import pdftron.PDF.PDFDoc;
import pdftron.PDF.PDFDraw;
import pdftron.PDF.PDFNet;
import pdftron.PDF.Page;
import pdftron.PDF.Rect;
import pdftron.PDF.TextExtractor;

import pdftron.*;
import pdftron.Common.*;
import pdftron.Filters.*;
import pdftron.SDF.*;
import pdftron.PDF.*;
import pdftron.PDF.Rect.*;

public class PDFHighLight2 {

  public static void main(String[] args) {
     String input_path="C:\\testpdf\\";
       PDFNet.setResourcesPath("E:\\PDFNetC\\resources ");
       TextExtractor.Word word=null;
       java.io.File f = new java.io.File("C:\\f1.txt");
      Iterator itr=null;
       String stringword=null;
       Rect Word_BBox= null;
        PDFDoc doc = new PDFDoc(input_path + "0809030833.pdf");
        Word_BBox= new Rect();
          ColorPt highlight_color = new ColorPt(1, 1, 0); // Yellow
          TextExtractor txt = new TextExtractor();

//itr= doc.getPageIterator();
                Page pg =null;
         int pageNum=1;
        int totalPage=0;
       TextExtractor.Line line = null;

//pg = (Page)itr.next();

          while(pageNum<=totalPage -1)
      pg = (Page)doc.getPage(pageNum);
            for(int k = 0; k<=txt.getNumLines()-1;k++)
                for(int i = 0;i<=line.getNumWords()-1;i++)
            /* while (word.isValid())
            // for (int j=0;j<=i;j++)
             // {
                word= line.getWord(i);




               stringword= word.getString();

                 Word_BBox =word.getBBox();
        // pg.AnnotPushBack(CreateHighlightAnnot(doc, Word_BBox,
          // highlight_color));
                 pg.annotPushBack(CreateHighlightAnnot(doc, Word_BBox,

                // doc.save(input_path + "latest.pdf",0,null);



              // }

              // break;

                 line =line.getNextLine();
             // break;
          // if(pageNum==40)

           // break;

          // Used to extract words
          doc.save(input_path + "latest.pdf",0,null);

        } catch (PDFNetException e)



   public static Obj CreateHighlightAppearance(PDFDoc doc, Rect bbox,
ColorPt higlight_color,String word )
        Obj stm=null;
        ElementBuilder build=null;
        ElementWriter writer=null;
        Element element=null;
          // Create a button appearance stream
         build = new ElementBuilder();
           writer = new ElementWriter();


          // Draw background
       element = build.createRect(bbox.getX1()- 2, bbox.getY1(),
bbox.getX2() +
              2, bbox.getY2());

          byte[] bytes = word.getBytes();

        // element.setTextData("hello".getBytes());

         // if(element.isWindingFill())
         // element.setWindingFill(true);

       // element.setTextData(word.getBytes());

          GState gs = element.getGState();

         // gs.SetBlendMode(GState.BlendMode.e_bl_multiply);

    stm = writer.end();

          // Set the bounding box
         // stm.Put("Subtype", Obj.CreateName("Form"));
          }catch(Exception ex)
          return stm;

  public static Annot CreateHighlightAnnot(PDFDoc doc, Rect bbox,
       highlight_color,String word)
   Annot a=null;


           a = Annot.create(doc,Annot.e_Highlight,bbox);

          a.setAppearance(CreateHighlightAppearance(doc, bbox,
          }catch(Exception ex)

       /* Obj quads = Obj.CreateArray();
          a.GetSDFObj().Put("QuadPoints", quads);
          return a;

  public static boolean contains(String full, String searched) {
    if(full.indexOf(searched) != -1)
    return true;
    else { return false; }
A: Looking over your code it seems that these words are being
highlighted due to your string match condition:


You would need to change your string match condition. For example:

word.getString().Equals("Chan") ....

It the comparison logic is more complex you could use JAVA regular
expressions to test for a string match


In any case this issue doesn't seem to be related to PDFNet API