We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

SearchTextAndDrawRectangle Example not working

I am evaluating ASPOSE.PDF was trying to use SearchTextAndDrawRectangle to draw rectangle around a searched text. Search brings in the result i.e. I get value in TextSegment but the DrawBox function does not draws rectangle in output file.
Aspose.Pdf.License license = new Aspose.Pdf.License();
// Instantiate license file
// Set the value to indicate that license will be embedded in the application
license.Embedded = true;
// ExStart:SearchTextAndDrawRectangle
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Text();

        // Open document
        Document document = new Document(dataDir + "SearchAndGetTextFromAll.pdf");

        // Create TextAbsorber object to find all the phrases matching the regular expression

        TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber(@"method");

        TextSearchOptions textSearchOptions = new TextSearchOptions(true);

        textAbsorber.TextSearchOptions = textSearchOptions;


        var editor = new PdfContentEditor(document); 

        foreach (TextFragment textFragment in textAbsorber.TextFragments)


            foreach (TextSegment textSegment in textFragment.Segments)


                    DrawBox(editor, textFragment.Page.Number, textSegment, System.Drawing.Color.Red);


        dataDir = dataDir + "SearchTextAndDrawRectangle_out.pdf";
        // ExEnd:SearchTextAndDrawRectangle
        Console.WriteLine("\nRectangle drawn successfully on searched text.\nFile saved at " + dataDir);


private static void DrawBox(PdfContentEditor editor, int page, TextSegment segment, System.Drawing.Color color)

        var lineInfo = new LineInfo();

        lineInfo.VerticeCoordinate = new[] {

        (float)segment.Rectangle.LLX, (float)segment.Rectangle.LLY,

        (float)segment.Rectangle.LLX, (float)segment.Rectangle.URY,

        (float)segment.Rectangle.URX, (float)segment.Rectangle.URY,

        (float)segment.Rectangle.URX, (float)segment.Rectangle.LLY


        lineInfo.Visibility = true;

        lineInfo.LineColor = color;

        editor.CreatePolygon(lineInfo, page, new System.Drawing.Rectangle(0, 0, 0, 0), null);



Thanks for contacting support.

Please use following code snippet in order to add Rectangle around found text inside PDF document. For your reference, I have attached a sample PDF as well.

Document doc = new Document();
Page pg = doc.Pages[1];
var tf = new TextFragment("some text on page");

doc.Save(dataDir + "RectangleGraph.pdf");
doc = new Document(dataDir + "RectangleGraph.pdf");
Page page = doc.Pages[1];

TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber(@"text");

tf = textAbsorber.TextFragments[1];

var canvas = new Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(tf.Rectangle.ToRect().Left - (float)page.PageInfo.Margin.Left, tf.Rectangle.ToRect().Bottom + (float)page.PageInfo.Margin.Bottom - (float)tf.Rectangle.Height, (float)tf.Rectangle.Width, (float)tf.Rectangle.Height); //new Aspose.Pdf.Drawing.Rectangle(0, 700, 100, 750);
var c = ColorTranslator.FromHtml("#FFFF00");
rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
doc.Save(dataDir + "RectangleGraph.pdf");

RectangleGraph.pdf (2.2 KB)

In case of any further assistance, please feel free to let us know.