PDFViewControl.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. static CPDFViewerTool ViewerTool = new CPDFViewerTool();
  17. public CPDFToolManager PDFToolManager;
  18. #region Properties
  19. public CPDFViewerTool PDFViewTool { get; set; }
  20. public bool CustomSignHandle { get; set; }
  21. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  22. #endregion
  23. public PDFViewControl()
  24. {
  25. InitializeComponent();
  26. if (ViewerTool==null)
  27. {
  28. PDFViewTool = ViewerTool= new CPDFViewerTool();
  29. }
  30. else
  31. {
  32. PDFViewTool = ViewerTool;
  33. }
  34. Content = PDFViewTool;
  35. PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler;
  36. PDFViewTool.SizeChanged += PDFViewTool_SizeChanged;
  37. PDFToolManager = new CPDFToolManager(PDFViewTool);
  38. //PDFToolManager.SetToolType(CPDFToolManager.ToolType.Viewer);
  39. }
  40. private void PDFViewTool_SizeChanged(object sender, SizeChangedEventArgs e)
  41. {
  42. PDFViewTool.GetCPDFViewer().UpDataRenderFrame();
  43. }
  44. public void InitDocument(string Path)
  45. {
  46. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path);
  47. if (pdfDoc != null)
  48. {
  49. PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc);
  50. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight);
  51. }
  52. }
  53. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  54. {
  55. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  56. {
  57. e.IsZoom = true;
  58. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  59. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  60. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  61. PDFViewTool.GetCPDFViewer().UpDataRenderFrame();
  62. }
  63. }
  64. #region Private Command Methods
  65. private double CheckZoomLevel(double zoom, bool IsGrowth)
  66. {
  67. zoom += (IsGrowth ? 0.01 : -0.01);
  68. double standardZoom = 100;
  69. if (zoom <= 0.01)
  70. {
  71. return 0.01;
  72. }
  73. if (zoom >= 10)
  74. {
  75. return 10;
  76. }
  77. zoom *= 100;
  78. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  79. {
  80. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  81. {
  82. standardZoom = zoomLevelList[i + 1];
  83. break;
  84. }
  85. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  86. {
  87. standardZoom = zoomLevelList[i];
  88. break;
  89. }
  90. }
  91. return standardZoom / 100;
  92. }
  93. #endregion
  94. }
  95. }