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);
}