PDFViewControl.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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().UpDateRenderFrame();
  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. PDFViewTool.GetCPDFViewer().SetViewMode(ViewModes.SingleContinuous);
  62. }
  63. }
  64. private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  65. {
  66. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  67. {
  68. e.IsZoom = true;
  69. PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom);
  70. double zoom = PDFViewTool.GetCPDFViewer().GetZoom();
  71. PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior)));
  72. PDFViewTool.GetCPDFViewer().UpDateRenderFrame();
  73. }
  74. }
  75. #region Private Command Methods
  76. private double CheckZoomLevel(double zoom, bool IsGrowth)
  77. {
  78. zoom += (IsGrowth ? 0.01 : -0.01);
  79. double standardZoom = 100;
  80. if (zoom <= 0.01)
  81. {
  82. return 0.01;
  83. }
  84. if (zoom >= 10)
  85. {
  86. return 10;
  87. }
  88. zoom *= 100;
  89. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  90. {
  91. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  92. {
  93. standardZoom = zoomLevelList[i + 1];
  94. break;
  95. }
  96. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  97. {
  98. standardZoom = zoomLevelList[i];
  99. break;
  100. }
  101. }
  102. return standardZoom / 100;
  103. }
  104. #endregion
  105. }
  106. }