PDFViewControl.xaml.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. double standardZoom = 100;
  55. if (zoom <= 0.01)
  56. {
  57. return 0.01;
  58. }
  59. if (zoom >= 10)
  60. {
  61. return 10;
  62. }
  63. zoom *= 100;
  64. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  65. {
  66. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  67. {
  68. standardZoom = zoomLevelList[i + 1];
  69. break;
  70. }
  71. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  72. {
  73. standardZoom = zoomLevelList[i];
  74. break;
  75. }
  76. }
  77. return standardZoom / 100;
  78. }
  79. #endregion
  80. }
  81. }