private void OnKeyDown(object sender, KeyEventArgs k) { if (PDFPageImageList != null && PDFPageImageList.Count > 0) { var ic = (ItemsControl)ScrollPDF.Content; var itemWidth = ((ContentPresenter)ic.ItemContainerGenerator. ContainerFromItem(ic.Items[0])).ActualWidth; var numItemsShown = Math.Round(ActualWidth / itemWidth); var maxScroll = (PDFPageImageList.Count - numItemsShown) * itemWidth; // align offset to page var offset = Math.Round(ScrollPDF.HorizontalOffset / itemWidth) * itemWidth; if (k.Key == Key.Space || k.Key == Key.Right || k.Key == Key.Down) { ScrollPDF.ScrollToHorizontalOffset( Math.Min(maxScroll, offset + itemWidth)); } else if (k.Key == Key.Left || k.Key == Key.Up) { ScrollPDF.ScrollToHorizontalOffset(offset - itemWidth); } else if (k.Key == Key.Home) { ScrollPDF.ScrollToLeftEnd(); } else if (k.Key == Key.End) { ScrollPDF.ScrollToHorizontalOffset(maxScroll); //ScrollPDF.ScrollToRightEnd(); } else if ((k.Key >= Key.D0 && k.Key <= Key.D9) || (k.Key >= Key.NumPad0 && k.Key <= Key.NumPad9)) { int num; if (int.TryParse(k.Key.ToString().First( char.IsDigit).ToString(CultureInfo.InvariantCulture), out num)) { var time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; // 2 key within 200ms = double key press if (time - lastKeyPressTime < 200) { num += lastKeyPressNumber * 10; } if (num != 0) { ScrollPDF.ScrollToHorizontalOffset((num - 1) * itemWidth); } lastKeyPressTime = time; lastKeyPressNumber = num; } } } }
private void UpdateScaling() { if (PDFPageImageList == null || PagesToFit == 0) { ItemsMargin = new Thickness(0); return; } var image = PDFPageImageList[0]; var screenWidth = ActualWidth - 2 * SystemParameters.ResizeFrameVerticalBorderWidth; var screenHeight = ActualHeight - (SystemParameters.WindowCaptionHeight + SystemParameters.ResizeFrameHorizontalBorderHeight + SystemParameters.HorizontalScrollBarHeight); //var screenWidth = PDFContentArea.ActualWidth; //var screenHeight = PDFContentArea.ActualHeight; var screenAspect = screenWidth / screenHeight; var pageWidth = image.PixelWidth; var pageHeight = image.PixelHeight; var pageAspect = ((double)PagesToFit * pageWidth) / pageHeight; var newWidth = screenWidth; var newHeight = screenHeight; if (screenAspect > pageAspect) // need to reduce width { newWidth = screenHeight * pageAspect; } else // need to reduce height { newHeight = screenWidth / pageAspect; } var horiz = (screenWidth - newWidth) / 2.0; var vert = (screenHeight - newHeight) / 2.0; ItemsMargin = new Thickness(horiz, vert, horiz, vert); var ic = (ItemsControl)ScrollPDF.Content; var itemWidth = ((ContentPresenter)ic.ItemContainerGenerator. ContainerFromItem(ic.Items[0])).ActualWidth; if (itemWidth > double.Epsilon) { var offset = Math.Round(ScrollPDF.HorizontalOffset / itemWidth) * itemWidth; ScrollPDF.ScrollToHorizontalOffset(offset); } }