How do I change the color of text under a given region on a PDF page?

Q: I would like to change the color of text that is located in certain
regions on a PDF page.
How can I use PDFNet API to implement this functionality?
-------
A: You can recolor and modify PDF text as shown in the following
utility function.

static void ChangeTextColor(Page page, List<Rect> rects, ColorPt
textColor)
{
  Element element = null;
  ElementReader reader = new ElementReader();
  ElementWriter writer = new ElementWriter();

  reader.Begin(page);
  writer.Begin(page, ElementWriter.WriteMode.e_replacement);

  Rect bbox = new Rect();
  while ((element = reader.Next()) != null)
  {
    if (element.GetType() == Element.Type.e_text)
    {
      element.GetBBox(bbox);
      RectangleOverlap overlap = GetOverlap(rects, bbox);

      // Since we will break a text run into pieces, explicitly
      // position each run to match the old location.
      Matrix2D tmtx = element.GetTextMatrix();
      CharIterator itr = element.GetCharIterator();
      element.SetTextMatrix(tmtx * new Matrix2D(1, 0, 0, 1,
itr.Current().x, itr.Current().y));
      element.SetPosAdjustment(0);

      if (overlap == RectangleOverlap.None)
      {
        writer.WriteElement(element);
      }
      else if (overlap == RectangleOverlap.Complete)
      {
        GState gs = element.GetGState();
        gs.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
        gs.SetFillColor(textColor);
        writer.WriteElement(element);
      }
      else
      {
        GState gs = element.GetGState();
        pdftron.PDF.Font font = gs.GetFont();
        double font_sz = gs.GetFontSize();
        Rect bbox1 = font.GetBBox();
        double bbox_height = bbox1.y2 - bbox1.y1;
        double decent = font_sz * bbox1.y1 / bbox_height;
        double ascent = font_sz * bbox1.y2 / bbox_height;
        double horiz_scale = gs.GetHorizontalScale() / 100.0;
        Matrix2D mtx = element.GetCTM() * tmtx;
        ColorSpace element_cs =
element.GetGState().GetFillColorSpace();
        ColorPt element_fill_color =
element.GetGState().GetFillColor();

        for (; itr.HasNext(); itr.Next())
        {
          Rect gbox = GetGlyphBBox(itr, font, horiz_scale, font_sz,
ascent, decent);
          gbox = GetBBoxTransfRect(gbox, mtx);

          if (GetOverlap(rects, gbox) != RectangleOverlap.None)
          {

element.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
            element.GetGState().SetFillColor(textColor);
          }
          else
          {
            element.GetGState().SetFillColorSpace(element_cs);
            element.GetGState().SetFillColor(element_fill_color);
          }

          CharData c = itr.Current();
          byte[] cdata = new byte[c.bytes];
          switch (cdata.Length)
          {
            case 4: cdata[3] = c.get_char_data(3); goto case 3;
            case 3: cdata[2] = c.get_char_data(2); goto case 2;
            case 2: cdata[1] = c.get_char_data(1); goto case 1;
            case 1: cdata[0] = c.get_char_data(0); break;
          }
          element.SetTextData(cdata, cdata.Length);

          element.SetTextMatrix(tmtx * new Matrix2D(1, 0, 0, 1,
itr.Current().x, itr.Current().y));
          element.SetPosAdjustment(0);

          writer.WriteElement(element);
        }
      }
    }
    else
    {
      writer.WriteElement(element);
    }
  }

  writer.End();
  reader.End();
  writer.Dispose();
  reader.Dispose();
}

To download the full sample project please use the following link:
  http://www.pdftron.com/pdfnet/samplecode/data/RecolorPDFText.zip

Please note that RecolorPDFText in the Files section of this forum is
now obsolete (since Google Groups no longer supports file uploads).