This is a migrated thread and some comments may be shown as answers.

Area selecion coordinate

5 Answers 325 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Stefania
Top achievements
Rank 2
Stefania asked on 05 May 2016, 08:10 AM

Hi,

on a form I have a pdfviewer and I need to know where the user click on my pdf. The coordinates should be relative to the document not the all radpdfviewer area.

When I get the coordinate with iTextSharp I'll put some image on that area (this part I'm already able to do it)

 

Those are the possibilities that I thought:

- Select an area on pdfviewer and get the coordinates? (THIS ONE WOULD BE THE BEST)

OR

- get the text selection position (I took a look to the documentation but I'm not able do reproduce the example.. this.PdfViewer.Document.CaretPosition is missing)

 

Any other idea?

 

Thank you

5 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 09 May 2016, 04:47 PM
Hello Stefania,

We have a demo demonstrating a similar scenario using RadPdfViewer in combination with RadPdfProcessing. RadPdfViewer is used to visualize a PDF file and when the user clicks on it, the page index and location are obtained through the GetLocationFromViewPoint() method. The document is modified using the API of RadPdfProcessing, exported and opened. Please, find the sample project in the attachments. 

However, if you prefer to stick to the approach with selection, you could check our help article that describes how you could create a TextSelection. After you have already selected the text, you could use the GetSelectionGeometry() method of the Selection in order to check the start position of the geometry that describes the selection.
PathGeometry geometry = this.pdfViewer.Document.Selection.GetSelectionGeometry(this.pdfViewer.Document.Pages[0]);
Point startPoint = geometry.Figures.First().StartPoint;

Hope this helps.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Stefania
Top achievements
Rank 2
answered on 10 May 2016, 07:21 AM

Hi. thank you Tanya for the reply.

I can't find the sample project.. can you reattach it, please?

0
Accepted
Tanya
Telerik team
answered on 10 May 2016, 10:30 AM
Hello Stefania,

Please, excuse me for this - it seems like something failed. The sample project should be attached to this post.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Stefania
Top achievements
Rank 2
answered on 28 Oct 2016, 11:29 AM

Hi, I just found an issue on the area selection coordinate when the pdf page is in landscape.

How can I resolve it? (see the attachment)

Here is my code

XML

<telerik:RadPdfViewer Grid.Column="1"  x:Name="pdfViewer"/>

 

Code-behind

private IFixedDocumentPresenter presenter = null;
private double _xTL;
private double _yTL;
private double _xBR;
private double _yBR;
private int _pageN;
private string _fullfilename;
 
private UIElement Presenter
{
    get
    {
        return this.presenter as UIElement;
    }
}
 
private void Initialize()
{
    this.presenter = this.pdfViewer.FixedDocumentPresenter;
 
    if (this.Presenter != null)
    {
        this.Presenter.PreviewMouseDown += PresenterMouseDown;
    }
 
    this.pdfViewer.FixedDocumentPresenterChanged += FixedDocumentPresenterChanged;
}
 
 
private void FixedDocumentPresenterChanged(object sender, EventArgs e)
{
    if (this.Presenter != null)
    {
        this.Presenter.PreviewMouseDown -= PresenterMouseDown;
    }
 
    this.presenter = this.pdfViewer.FixedDocumentPresenter;
    
    if (this.Presenter != null)
    {
        this.Presenter.PreviewMouseDown += PresenterMouseDown;
    }
}
public int clickCnt = 0;
     private void PresenterMouseDown(object sender, MouseButtonEventArgs e)
     {
         clickCnt++;
         System.Windows.Point position = e.GetPosition(this.Presenter);
 
         RadFixedPage page;
         System.Windows.Point location;
         if (this.pdfViewer.FixedDocumentPresenter.GetLocationFromViewPoint(position, out page, out location))
         {
             int pageIndex = page.PageNo;
             var rotation = page.Rotation; //ALWAYS Rotate0
 
             _pageN = pageIndex;
 
             //negative coordinates error
             if (location.X < 0 || location.Y < 0)
             {
                 SSCGlobalMessageBox.Show("0212", "WO");
                 clickCnt = 0;
                 return;
             }
 
             if (clickCnt == 1)
             {                
                 _xTL = location.X;
                 _yTL = location.Y;
             }
             else if (clickCnt == 2)
             {             
                 //Y > perch√® 0,0 √® in alto a sx
                 if (_xTL > location.X || _yTL > location.Y)
                 {
                     SSCGlobalMessageBox.Show("0211", "WO");
                     clickCnt = 0;
                     return;
                 }
                 _xBR = location.X;
                 _yBR = location.Y;
 
                 //save area
                 DocumentTypeSettingsAreaEntity area = new DocumentTypeSettingsAreaEntity();
                 area.DocumentTypeId = (int)documentType.Id;
                 area.ProjectId = (int)documentType.ProjectId;
                 area.AxisX = (decimal)_xTL;// Convert.ToDecimal(XPointTL.Text);
                 area.AxisY = (decimal)_yTL;// Convert.ToDecimal(YPointTL.Text);
                 area.Pages = _pageN.ToString();//PdfPage.Text;
                 area.Width = (decimal)(_xBR - _xTL);//Convert.ToDecimal(XPointBR.Text) - Convert.ToDecimal(XPointTL.Text);
                 area.Height = (decimal)(_yBR - _yTL);//Convert.ToDecimal(YPointBR.Text) - Convert.ToDecimal(YPointTL.Text);
                 area.RECORD_STATUS = STATUS_TYPE.ADDED;
                 ((DocumentTypeEntity)this.DataContext).DocumentTypeSettingsAree.Add(area);
               
                 //preview
                 if (string.IsNullOrEmpty(_fullfilename))
                     return;
 
                 this.ManipulateDocument();
                 clickCnt = 0;
             }              
         }
     }

 

private void ManipulateDocument(bool newArea = false)
        {
            using (new WpfWaitCursor())
            {
                PdfFormatProvider pdfProcessingProvider = new PdfFormatProvider();
 
                using (System.IO.Stream inputPdfStream = new System.IO.FileStream(_fullfilename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
                {
                    RadFixedDocument document = pdfProcessingProvider.Import(inputPdfStream);
 
                    //FOREACH RECTANGLE SAVED
                    foreach (var record in (this.DataContext as DocumentTypeEntity).DocumentTypeSettingsAree)
                    {
                        if (string.IsNullOrEmpty(record.Pages))
                            continue;
                        System.Windows.Point location = new Point((double)record.AxisX, (double)record.AxisY);
 
                        HashSet<int> pages = new HashSet<int>();
 
                        #region TROVO LE PAGINE
 
                        if (record.Pages.Equals("*"))
                        {
                            int totpag = document.Pages.Count();
                            for (int c = 0; c < totpag; c++)
                            {
                                pages.Add(c + 1);
                            }
                        }
                        else if (record.Pages.Length == 1)
                        {
                            int pag;
                            int.TryParse(record.Pages, out pag);
 
                            pages.Add(pag);
                        }
                        else
                        {
                            string[] tmp = record.Pages.Split(',');
 
                            foreach (var p in tmp)
                            {
                                string[] tmp2 = p.Split('-');
 
                                if (tmp2.Length == 1)
                                    pages.Add(Convert.ToInt32(tmp2[0]));
                                else
                                {
                                    int pagFrom = Convert.ToInt32(tmp2[0]);
                                    int pagTo = Convert.ToInt32(tmp2[1]);
 
                                    for (int x = pagFrom; x <= pagTo; x++)
                                    {
                                        pages.Add(x);
                                    }
                                }
                            }
                        }
                        #endregion
 
                        foreach (var p in pages)
                        {
                            if (p <= document.Pages.Count()) //if page > total pages skip
                            {                               
                                int pageN = p - 1;//Convert.ToInt32(record.Pages) - 1;
                          /*      var rotation = document.Pages[pageN].Rotation;
                                double rotationGrade = 0;
 
                                switch(rotation)
                                {
                                    case Telerik.Windows.Documents.Fixed.Model.Data.Rotation.Rotate90:
                                        rotationGrade = 90;
                                        break;
 
                                    case Telerik.Windows.Documents.Fixed.Model.Data.Rotation.Rotate180:
                                        rotationGrade = 180;
                                        break;
                                    case Telerik.Windows.Documents.Fixed.Model.Data.Rotation.Rotate270:
                                        rotationGrade = 270;
                                        break;                                       
                                }
*/
                                 
                                FixedContentEditor editor = new FixedContentEditor(document.Pages[pageN]);
                                 
                                editor.Position.Translate(location.X, location.Y);
                              //  editor.Position.Rotate(rotationGrade);
 
                                Rect r = new Rect(new Size((double)record.Width, (double)record.Height));
                                 
                                if (newArea)
                                    editor.GraphicProperties.FillColor = new RgbColor(0, 255, 0);
                                else
                                    editor.GraphicProperties.FillColor = new RgbColor(255, 128, 0);
                                editor.DrawRectangle(r);
 
                                editor.GraphicProperties.FillColor = new RgbColor(0, 0, 0);
 
                                string description = "TEST";                             
                                editor.DrawText(description);
                            }
                        }
                    }
 
                    //load pdf
                    MemoryStream m_Stream = new MemoryStream();
                    pdfProcessingProvider.Export(document, m_Stream);
 
                    RadFixedDocument fixeddocument = new PdfFormatProvider(m_Stream, FormatProviderSettings.ReadOnDemand).Import();
                    pdfViewer.Document = fixeddocument;
 
                }
            }
        }
0
Deyan
Telerik team
answered on 02 Nov 2016, 10:14 AM
Hello Stefania,

We have managed to reproduce the incorrect positioning when page Rotation value is different from Rotate0. We are attaching a modified version of the sample project showing how to position the FixedContentEditor so that the added image is positioned correctly on pages with different rotations. You may take a look at AdjustEditorPosition() method in order to see how exactly these calculations are performed. The modified demo uses sample document with different page sizes and page rotations - Rotate0, Rotate180, and Rotate270 respectively. You may test that all scenarios are working correctly by clicking on different document pages.

I hope this is helpful. If you have any other questions or concerns please do not hesitate to contact us again.

Regards,
Deyan
Telerik by Progress
Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
Tags
PDFViewer
Asked by
Stefania
Top achievements
Rank 2
Answers by
Tanya
Telerik team
Stefania
Top achievements
Rank 2
Deyan
Telerik team
Share this question
or