static void DrawGraphics(PdfGraphics graph, PdfTextSearchResults result, SolidBrush brush) { for (int i = 0; i < result.Rectangles.Count; i++) { RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Top - (float)result.Rectangles[i].Top), new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height)); graph.FillRectangle(brush, rect); } graph.AddToPageForeground(result.Page, 72, 72); }
//This method uses annotations to highlight text public static void HighlightResult(PdfDocumentProcessor processor, PdfTextSearchResults result) { for (int i = 0; i < result.Rectangles.Count; i++) { PdfTextMarkupAnnotationData annotation = processor.AddTextMarkupAnnotation(result.PageNumber, result.Rectangles[i], PdfTextMarkupAnnotationType.Highlight); if (annotation != null) { annotation.Color = new PdfRGBColor(0.2, 0.6, 0); } } }
static void FindWord(string textToFind, PdfDocumentProcessor processor, SolidBrush brush) { PdfTextSearchResults result = processor.FindText(textToFind); while (result.Status == PdfTextSearchStatus.Found) { using (PdfGraphics graph = processor.CreateGraphics()) { DrawGraphics(graph, result, brush); } result = processor.FindText(textToFind); } processor.SaveDocument("result.pdf"); Process.Start("result.pdf"); }
static void Main(string[] args) { // Create a PDF document processor. using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { // Define search words. string[] words = { "DX-B5000", "DX-RX800" }; // Load a PDF document. documentProcessor.LoadDocument(@"..\..\Document.pdf"); // Specify the search parameters. PdfTextSearchParameters searchParameters = new PdfTextSearchParameters(); searchParameters.CaseSensitive = true; searchParameters.WholeWords = true; // Get bookmark list. IList <PdfBookmark> bookmarks = documentProcessor.Document.Bookmarks; foreach (string word in words) { // Get the search results from the FindText method called with search text and search parameters. PdfTextSearchResults results = documentProcessor.FindText(word, searchParameters); // If the desired text is found, create a destination that corresponds to the found text // to be displayed at the upper corner of the window. if (results.Status == PdfTextSearchStatus.Found) { PdfXYZDestination destination = new PdfXYZDestination(results.Page, 0, results.Rectangles[0].Top, null); // Create a bookmark with the search word shown as title and the destination. PdfBookmark bookmark = new PdfBookmark() { Title = word, Destination = destination }; // Add the bookmark to the bookmark list. bookmarks.Add(bookmark); } } // Save the modified document. documentProcessor.SaveDocument(@"..\..\Result.pdf"); } }
static void Main(string[] args) { //Create a PDF document processor. using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { //Define search words string[] words = { "Get", "DX-RX809", "HD", "DX-B5000" }; //Load a PDF document documentProcessor.LoadDocument(@"..\..\Document.pdf"); //Specify the search parameters PdfTextSearchParameters searchParameters = new PdfTextSearchParameters(); searchParameters.CaseSensitive = true; searchParameters.WholeWords = true; //Comment the following "using" statement if you use annotations using (var brush = new SolidBrush(Color.FromArgb(130, 55, 155, 255))) foreach (string word in words) { //Get the search results from the FindText method call with search text and search parameters PdfTextSearchResults result = documentProcessor.FindText(word, searchParameters); //Highlight the result while (result.Status == PdfTextSearchStatus.Found) { using (PdfGraphics graphics = documentProcessor.CreateGraphics()) { HighlightResult(graphics, result, brush); } //Use this method call to add annotations: //HighlightResult(documentProcessor, result); result = documentProcessor.FindText(word, searchParameters); } } //Save the document documentProcessor.SaveDocument(@"..\..\Result.pdf"); } }