FindAndHighlight.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.Annotations;
using System.Collections.Generic;
using GrapeCity.Documents.Common;

namespace DsPdfWeb.Demos
{
    // This example shows how to find all occurrences of a word in a PDF document
    // and highlight all those occurrences using the text markup highlight annotation.
    // This example creates a text markup annotation per occurrence. A different approach
    // illustrated in MarkupPerPage is to create a text markup annotation per page.
    //
    // The PDF used in this example was downloaded from https://www.frontiersin.org/articles/10.3389/fendo.2022.1005722/full.
    public class FindAndHighlight
    {
        public int CreatePDF(Stream stream)
        {
            // Load the PDF:
            var doc = new GcPdfDocument();
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "fendo-13-1005722.pdf"));
            doc.Load(fs);

            // Find all occurrences of the word "childbirths":
            var found = doc.FindText(new FindTextParams("childbirths", true, false), null);

            // Add a text markup annotation to highlight each occurrence:
            foreach (var f in found)
            {
                var markup = new TextMarkupAnnotation()
                {
                    Page = doc.Pages[f.PageIndex],
                    MarkupType = TextMarkupType.Highlight,
                    Color = Color.Yellow
                };
                List<Quadrilateral> area = new List<Quadrilateral>();
                foreach (var b in f.Bounds)
                    area.Add(b);
                markup.Area = area;
            }

            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}