using System; using System.Collections.Generic; using System.Linq; using System.Text; using pdftron.PDF.Tools; using pdftron.PDF; using System.Windows.Controls; using System.Windows; using System.Windows.Shapes; using System.Diagnostics; namespace PDFViewWPFCS { class CalibrationTool { private PDFViewWPF _PDFView; private System.Windows.Shapes.Rectangle _Rect; private bool _Dragging = false; private System.Windows.Point _DownPoint; private System.Windows.Point _MovePoint; private Canvas _DrawingCanvas; private int _PageNumber = -1; public CalibrationTool(PDFViewWPF view) { _PDFView = view; // Set ToolMode to custom, so that the PDFViewWPD doesn't try to do anything. _PDFView.SetToolMode(PDFViewWPF.ToolMode.e_custom); _DrawingCanvas = _PDFView.GetCanvas(); // Subscribing to this canvas works, subscribing directly to PDFViewWPF doesn't. _DrawingCanvas.MouseLeftButtonDown += _PDFView_MouseLeftButtonDown; _DrawingCanvas.MouseMove += _PDFView_MouseMove; _DrawingCanvas.PreviewMouseLeftButtonUp += _PDFView_PreviewMouseLeftButtonUp; } void _PDFView_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { Debug.WriteLine("Done dragging"); // Now, translate these coordinates to page coordinates double downX = _DownPoint.X; double downY = _DownPoint.Y; // Convert from screen point to page point. Debug.WriteLine("before: {0}, {1}", downX, downY); _PDFView.ConvScreenPtToPagePt(ref downX, ref downY, _PageNumber); Debug.WriteLine("after: {0}, {1}", downX, downY); // Do the same with MovePoint _Dragging = false; _DrawingCanvas.Children.Remove(_Rect); Close(); } void _PDFView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { if (_Dragging) { Debug.WriteLine("Dragging"); // check page bounds etc... _MovePoint = e.GetPosition(_PDFView); _Rect.Width = Math.Abs(_MovePoint.X - _DownPoint.X); _Rect.Height = Math.Abs(_MovePoint.Y - _DownPoint.Y); } } void _PDFView_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { Debug.WriteLine("mouse down"); _Dragging = true; _DownPoint = e.GetPosition(_PDFView); // Which page are we on _PageNumber = _PDFView.GetPageNumberFromScreenPt(_DownPoint.X, _DownPoint.Y); // You will want to build a crop box here pdftron.PDF.Page page = _PDFView.GetDoc().GetPage(_PageNumber); pdftron.PDF.Rect rect = page.GetCropBox(); // convert this to screen points using _PDFView.ConvPagePtToScreenPt _Rect = new System.Windows.Shapes.Rectangle(); _Rect.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(100, 255, 0, 0)); _DrawingCanvas.Children.Add(_Rect); _Rect.SetValue(Canvas.LeftProperty, _DownPoint.X + _PDFView.GetHScrollPos()); _Rect.SetValue(Canvas.TopProperty, _DownPoint.Y + _PDFView.GetVScrollPos()); } private void Close() { // unsubscribe events here // Restore the toolMode to pan _PDFView.SetToolMode(PDFViewWPF.ToolMode.e_pan); } } }