PDFViewControl.xaml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.Tool;
  3. using ComPDFKitViewer;
  4. using Nager.Country.Currencies;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. namespace Compdfkit_Tools.PDFControl
  13. {
  14. public partial class PDFViewControl : UserControl
  15. {
  16. public CPDFToolManager PDFToolManager;
  17. #region Properties
  18. public CPDFViewerTool PDFViewTool { get; set; }
  19. public bool CustomSignHandle { get; set; }
  20. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  21. #endregion
  22. public PDFViewControl()
  23. {
  24. InitializeComponent();
  25. PDFViewTool = new CPDFViewerTool();
  26. Content = PDFViewTool;
  27. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler;
  28. PDFToolManager = new CPDFToolManager(PDFViewTool);
  29. //PDFToolManager.SetToolType(CPDFToolManager.ToolType.Viewer);
  30. }
  31. public void InitDocument(string Path)
  32. {
  33. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
  34. if (pdfDoc != null)
  35. {
  36. PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);
  37. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
  38. }
  39. }
  40. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  41. {
  42. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  43. {
  44. e.IsZoom = true;
  45. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  46. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  47. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  48. PDFViewTool.GetCPDFViewer().UpDataRenderFrame();
  49. }
  50. }
  51. #region Private Command Methods
  52. private double CheckZoomLevel(double zoom, bool IsGrowth)
  53. {
  54. zoom += (IsGrowth ? 0.01 : -0.01);
  55. double standardZoom = 100;
  56. if (zoom <= 0.01)
  57. {
  58. return 0.01;
  59. }
  60. if (zoom >= 10)
  61. {
  62. return 10;
  63. }
  64. zoom *= 100;
  65. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  66. {
  67. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  68. {
  69. standardZoom = zoomLevelList[i + 1];
  70. break;
  71. }
  72. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  73. {
  74. standardZoom = zoomLevelList[i];
  75. break;
  76. }
  77. }
  78. return standardZoom / 100;
  79. }
  80. #endregion
  81. }
  82. }