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
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
Hi. thank you Tanya for the reply.
I can't find the sample project.. can you reattach it, please?
Please, excuse me for this - it seems like something failed. The sample project should be attached to this post.
Regards,
Tanya
Telerik
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; } } }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

