PDF Editor select text

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
maggarwal
Posts: 11
Joined: Tue Jan 29, 2013 1:34 am

PDF Editor select text

Post 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);
                }
            }
        }
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: PDF Editor select text

Post 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
maggarwal
Posts: 11
Joined: Tue Jan 29, 2013 1:34 am

Re: PDF Editor select text

Post 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.
Alex
Site Admin
Posts: 2303
Joined: Thu Jul 10, 2008 2:21 pm

Re: PDF Editor select text

Post 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
Post Reply