static void Main(string[] args) { Console.WriteLine("UnderlinesAndHighlights Sample:"); using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sInput = "../../Resources/Sample_Input/sample.pdf"; String sOutput = "../HighlightAndUnderlineAnnotations-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Document doc = new Document(sInput); Console.WriteLine("Opened a document " + sInput); Page docpage = doc.GetPage(0); // // Highlight occurrences of the word "cloudy" on the page. // Underline occurrences of the word "rain" on the page. // // For a more in-depth example of using the WordFinder, see the TextExtraction sample. // List <Quad> cloudyQuads = new List <Quad>(); List <Quad> rainQuads = new List <Quad>(); WordFinderConfig wfc = new WordFinderConfig(); WordFinder wf = new WordFinder(doc, WordFinderVersion.Latest, wfc); IList <Word> words = wf.GetWordList(docpage.PageNumber); foreach (Word w in words) { // Store the Quads of all "Cloudy" words in a list for later use in // creating the annotation. if (w.Text.ToLower().Equals("cloudy") || ((w.Attributes & WordAttributeFlags.HasTrailingPunctuation) == WordAttributeFlags.HasTrailingPunctuation && w.Text.ToLower().StartsWith("cloudy"))) { cloudyQuads.AddRange(w.Quads); } // Store the Quads of all "Rain" words if (w.Text.ToLower().Equals("rain") || ((w.Attributes & WordAttributeFlags.HasTrailingPunctuation) == WordAttributeFlags.HasTrailingPunctuation && w.Text.ToLower().StartsWith("rain"))) { rainQuads.AddRange(w.Quads); } } HighlightAnnotation highlights = new HighlightAnnotation(docpage, cloudyQuads); highlights.Color = new Color(1.0, 0.75, 1.0); highlights.NormalAppearance = highlights.GenerateAppearance(); UnderlineAnnotation underlines = new UnderlineAnnotation(docpage, rainQuads); underlines.Color = new Color(0.0, 0.0, 0.0); underlines.NormalAppearance = underlines.GenerateAppearance(); // Read back the text that was annotated. Console.WriteLine("Cloudy text: {0}", highlights.GetAnnotatedText(true)); Console.WriteLine("Rainy text: {0}", underlines.GetAnnotatedText(false)); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { Console.WriteLine("RegexTextSearch Sample:"); using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sInput = Library.ResourceDirectory + "Sample_Input/RegexTextSearch.pdf"; String sOutput = "RegexTextSearch-out.pdf"; // Highlight occurrences of the phrases that match this regular expression. // Uncomment only the one you are interested in seeing displayed with highlights. // Phone numbers String sRegex = "((1-)?(\\()?\\d{3}(\\))?(\\s)?(-)?\\d{3}-\\d{4})"; // Email addresses //String sRegex = "(\\b[\\w.!#$%&'*+\\/=?^`{|}~-]+@[\\w-]+(?:\\.[\\w-]+)*\\b)"; // URLs //String sRegex = "((https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9]+\\.[^\\s]{2,}|www\\.[a-zA-Z0-9]+\\.[^\\s]{2,}))"; if (args.Length > 0) { sInput = args[0]; } using (Document doc = new Document(sInput)) { int nPages = doc.NumPages; Console.WriteLine("Input file: " + sInput); WordFinderConfig wordConfig = new WordFinderConfig(); // Need to set this to true so phrases will be concatenated properly wordConfig.NoHyphenDetection = true; // Create a DocTextFinder with the default wordfinder parameters using (DocTextFinder docTextFinder = new DocTextFinder(doc, wordConfig)) { // Retrieve the phrases matching a regular expression IList <DocTextFinderMatch> docMatches = docTextFinder.GetMatchList(0, nPages - 1, sRegex); foreach (DocTextFinderMatch wInfo in docMatches) { // Show the matching phrase Console.WriteLine(wInfo.MatchString); // Get the quads IList <DocTextFinderQuadInfo> QuadInfo = wInfo.QuadInfo; // Iterate through the quad info and create highlights foreach (DocTextFinderQuadInfo qInfo in QuadInfo) { Page docpage = doc.GetPage(qInfo.PageNum); // Highlight the matched string words var highlight = new HighlightAnnotation(docpage, qInfo.Quads); highlight.NormalAppearance = highlight.GenerateAppearance(); } } // Save the document with the highlighted matched strings doc.Save(SaveFlags.Full, sOutput); } } } }