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

Free Support Forum - aspose.com

SearchTextAndDrawRectangle Example not working

Hi,
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.
Code:
Aspose.Pdf.License license = new Aspose.Pdf.License();
// Instantiate license file
license.SetLicense(@“Aspose.Pdf.lic”);
// 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;

        document.Pages.Accept(textAbsorber); 

        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";
        document.Save(dataDir);
        // 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);

    }

@asif.khan

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();
doc.Pages.Add();
Page pg = doc.Pages[1];
var tf = new TextFragment("some text on page");
pg.Paragraphs.Add(tf);

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

TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber(@"text");
doc.Pages[1].Accept(textAbsorber);

tf = textAbsorber.TextFragments[1];

var canvas = new Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
page.Paragraphs.Add(canvas);
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);
canvas.Shapes.Add(rect);
doc.Save(dataDir + "RectangleGraph.pdf");

RectangleGraph.pdf (2.2 KB)

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