FindTransformed.cs
C# VB
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.IO;using System.Drawing;using System.Linq;using GrapeCity.Documents.Common;using GrapeCity.Documents.Pdf;
namespace DsPdfWeb.Demos{ // This sample loads the PDF file created by the Transforms sample, // finds all occurrences of a string in the loaded document, // and highlights these occurrences. Two points of interest about this sample: // - The texts in the original document are graphically transformed, // but the quadrilaterals supplied by the FindText method allows you to easily // highlight the finds even in that case. // - The sample inserts a new content stream at index 0 of the page, // this ensures that the highlighting is drawn UNDER the original content. // (The same approach may be used to add watermarks etc. to existing files.) public class FindTransformed { public int CreatePDF(Stream stream) { // The original file stream must be kept open while working with the loaded PDF, // see @{LoadPDF} for details: using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Transforms.pdf")); var doc = new GcPdfDocument(); doc.Load(fs); // Find all 'Text drawn at', using case-sensitive search: var finds = doc.FindText( new FindTextParams("Text drawn at", false, true), OutputRange.All);
// Highlight all finds: first, find all pages where the text was found var pgIndices = finds.Select(f_ => f_.PageIndex).Distinct(); // Loop through pages, on each page insert a new content stream at index 0, // so that our highlights go UNDER the original content: foreach (int pgIdx in pgIndices) { var page = doc.Pages[pgIdx]; PageContentStream pcs = page.ContentStreams.Insert(0); var g = pcs.GetGraphics(page); foreach (var find in finds.Where(f_ => f_.PageIndex == pgIdx)) { foreach (var ql in find.Bounds) { // Note the solid color used to fill the polygon: g.FillPolygon(ql, Color.CadetBlue); g.DrawPolygon(ql, Color.Blue); } } } // Done: doc.Save(stream); return doc.Pages.Count; } }}