Page 1 of 1

PDF Editor select text

Posted: Fri Apr 12, 2013 10:16 pm
by maggarwal
Hi,

I want to programmatically select the word which mouse pointer is pointing to.

I tried drawing a Rectangle on the page but it didn't work (no rectangle).

Can you please help.

Code: Select all

        
private void ShowSymbolInfo(Point p)
        {
            if (imageViewer1.Image == null)
                return;
            
            lock (_document)
            {
                PdfPage page = FocusedPage;
                if (page == null)
                    return;

                PointF pt = imageViewer1.PointToImage(p);
                Resolution resolution = imageViewer1.RenderingSettings.Resolution;
                if (resolution.IsEmpty())
                    resolution = page.DefaultResolution;
                page.PointToUnit(ref pt, resolution);
                try
                {
                    var region = page.TextRegion.GetWordSubregion(pt);

                    if (region != null)
                    {
                    using (var gr = PdfGraphics.FromPage(page))
                    {
                        var brush = new PdfBrush(Color.Gray, GraphicsStateBlendMode.Multiply);
                        gr.FillRectangle(brush, region.Rectangle);
                    }
                        statusLabel.Text = string.Format("Text: '{0}'",
                                                         region.TextContent);
                    }
                    else
                    {
                        if (statusLabel.Text.Length > 8 && statusLabel.Text.Substring(0, 6) == "Symbol")
                            statusLabel.Text = "";
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }

Re: PDF Editor select text

Posted: Mon Apr 15, 2013 9:40 am
by Alex
Hello,

Please read how to extract text from a particular area of PDF page here:
viewtopic.php?f=20&t=1988

Best regards, Alexander

Re: PDF Editor select text

Posted: Mon Apr 15, 2013 4:42 pm
by maggarwal
Hi Alex,

Thanks for the link, but my problem is that I cannot draw a rectangle around the text I am trying to capture. I can retrieve the text using

Code: Select all

var region = page.TextRegion.GetWordSubregion(pt);
but I also want to show a rectangle around the text for user to see. I tried drawing the rectangle using following piece of code

Code: Select all

using (var gr = PdfGraphics.FromPage(page))
{
     var brush = new PdfBrush(Color.Gray, GraphicsStateBlendMode.Multiply);
     gr.FillRectangle(brush, region.Rectangle);
}
but nothing get's drawn on the page.

Re: PDF Editor select text

Posted: Wed Apr 17, 2013 10:45 am
by Alex
Hello,

I think you use wrong coordinates of rectangle when you fill the rectangle on PDF page.

Please read the "Programming => Document and Image Formats => PDF => Overview => Drawing graphics on PDF page" article in the documentation:
http://www.vintasoft.com/docs/vsimaging-dotnet/

for information about coordinate system used for drawing of PDF page.

Best regards, Alexander