How to search and highlight text using PDFNet?

Q: I have a requirement similar to "How to search and highlight text
using PDFNet" (

Unfortunately i am getting errors trying to compile your code on the
page. I was able to compile, execute and adapt quite a bit of your
samples , but my particular requirement is to highlight certain
strings in the pdf and add bookmarks to the pages where this string
exists. The first part of the requirement where i have to highlight a
search string is a major requirement which i am not able to get past.

The following is the location where i am getting the error:

Obj quads = Obj.CreateArray();
A: This code snippet is using some APIs that are deprecated starting
with PDFNet v4. For instructions on how to move old code to the latest
API, please see

The following is the updated 'PDF highlight' sample:

// The following sample illustrates how to programmatic highlight
// The sample is using TextExtractor to extract words and PDFDraw
class to
// rasterize pages with highlight annotations. The sample also saves
// PDF documents that includes highlighted text.
// If you are looking for interactive text selection and highlighting,
// class already includes built-in tool modes for text search and
// For a concrete example of how to use these functions, please take a
look at
// the latest version of PDFView sample project.

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

namespace TextHighlightTestCS
class PDFTextHighligh
  // Use PDFNet to generate appearance stream for highlight
  static Obj CreateHighlightAppearance(PDFDoc doc, Rect bbox, ColorPt
   // Create a button appearance stream
   ElementBuilder build = new ElementBuilder();
   ElementWriter writer = new ElementWriter();

   // Draw background
   Element element = build.CreateRect(bbox.x1 - 2, bbox.y1, bbox.x2 +
2, bbox.y2);
   GState gs = element.GetGState();
   Obj stm = writer.End();


   // Set the bounding box
   stm.PutRect("BBox", bbox.x1, bbox.y1, bbox.x2, bbox.y2);
   stm.PutName("Subtype", "Form");
   return stm;

  // Create a Highlight Annotation.
  static Annot CreateHighlightAnnot(PDFDoc doc, Rect bbox, ColorPt
   Annot a = Annot.Create(doc, Annot.Type.e_Highlight, bbox);
   a.SetAppearance(CreateHighlightAppearance(doc, bbox,

   Obj quads = a.GetSDFObj().PutArray("QuadPoints");
   return a;

  static void Main(string[] args)

   // Relative path to the folder containing test files.
   const string input_path = "../../../../TestFiles/";
   const string output_path = "../../../../TestFiles/Output/";

    PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf");

    // Highlight all "Robin" instances in the input document.
    ColorPt highlight_color = new ColorPt(1, 1, 0); // Yellow

    TextExtractor txt = new TextExtractor(); // Used to extract words
    Rect word_bbox = new Rect();

    PDFDraw pdfdraw = new PDFDraw(96); // Used to export PDF pages to

    PageIterator itr = doc.GetPageIterator();
    for (; itr.HasNext(); itr.Next())
     Page page = itr.Current();
     txt.Begin(page); // Read the page.

     // Example 2. Extract words one by one.
     TextExtractor.Word word;
     String word_str;
     for (TextExtractor.Line line = txt.GetFirstLine();
line.IsValid(); line=line.GetNextLine())
      for (word=line.GetFirstWord(); word.IsValid();
       word_str = word.GetString().ToUpper(); // For case-insensitive
       if (word_str.StartsWith("ROBIN") ||
        word_bbox = word.GetBBox();
        // Console.WriteLine("{0} \t bbox: {1}, {2}, {3}, {4}\n",
word, word_bbox.x1, word_bbox.y1, word_bbox.x2, word_bbox.y2);
        page.AnnotPushBack(CreateHighlightAnnot(doc, word_bbox,


     string outname = string.Format("{0}out{1:d}.jpg", output_path,
     pdfdraw.Export(page, outname, "jpg");


    doc.Save(output_path + "output.pdf",
   catch (PDFNetException e)

We are noticing that images compressed using JBIG2 are only readable by
Adobe Reader/Acrobat 8 (via Web plugin). Is there a way to make the
compression capable with Acrobat 7 and above?

Greg Dillon

JBIG2 compression is available starting with PDF 1.4 (i.e. Acrobat 5)
and it does not require a special plug-in. PDFNet SDK can also
compress & decompress embedded JBIG2 images.

The above link is no longer active. The updated link is: