/// <summary> /// Extracts text fragments from the 2nd page and highlights the glyphs in the fragment. /// </summary> /// <param name="document"></param> private static void ExtractTextAndHighlightGlyphs(PdfFixedDocument document) { PdfRgbColor penColor = new PdfRgbColor(); PdfPen pen = new PdfPen(penColor, 0.5); Random rnd = new Random(); byte[] rgb = new byte[3]; PdfContentExtractor ce = new PdfContentExtractor(document.Pages[1]); PdfTextFragmentCollection tfc = ce.ExtractTextFragments(); PdfTextFragment tf = tfc[1]; for (int i = 0; i < tf.Glyphs.Count; i++) { rnd.NextBytes(rgb); penColor.R = rgb[0]; penColor.G = rgb[1]; penColor.B = rgb[2]; PdfPath boundingPath = new PdfPath(); boundingPath.StartSubpath(tf.Glyphs[i].GlyphCorners[0].X, tf.Glyphs[i].GlyphCorners[0].Y); boundingPath.AddLineTo(tf.Glyphs[i].GlyphCorners[1].X, tf.Glyphs[i].GlyphCorners[1].Y); boundingPath.AddLineTo(tf.Glyphs[i].GlyphCorners[2].X, tf.Glyphs[i].GlyphCorners[2].Y); boundingPath.AddLineTo(tf.Glyphs[i].GlyphCorners[3].X, tf.Glyphs[i].GlyphCorners[3].Y); boundingPath.CloseSubpath(); document.Pages[1].Graphics.DrawPath(pen, boundingPath); } }
private static void HighlightSearchResults(PdfPage page, PdfTextSearchResultCollection searchResults, PdfColor color) { PdfPen pen = new PdfPen(color, 0.5); for (int i = 0; i < searchResults.Count; i++) { PdfTextFragmentCollection tfc = searchResults[i].TextFragments; for (int j = 0; j < tfc.Count; j++) { PdfPath path = new PdfPath(); path.StartSubpath(tfc[j].FragmentCorners[0].X, tfc[j].FragmentCorners[0].Y); path.AddPolygon(tfc[j].FragmentCorners); page.Graphics.DrawPath(pen, path); } } }