PDFViewControl.xaml.cs 3.8 KB

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