CPDFViewerTool.PDFCompareEffect.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using ComPDFKit.Tool.Help;
  2. using ComPDFKit.Viewer.Layer;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.Helper;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Media;
  13. namespace ComPDFKit.Tool
  14. {
  15. public partial class CPDFViewerTool
  16. {
  17. private int PDFCompareViewID { get; set; } = -1;
  18. private int PDFComparePageIndex { get; set; } = -1;
  19. private List<Rect> PDFCompareRectList { get; set; }
  20. private SolidColorBrush PDFCompareBrush { get; set; }
  21. private Pen PDFComparePen { get; set; }
  22. private CustomizeLayer GetPDFCompareDrawView()
  23. {
  24. if(PDFViewer==null)
  25. {
  26. return null;
  27. }
  28. if (PDFCompareViewID != -1)
  29. {
  30. return PDFViewer.GetViewForTag(PDFCompareViewID) as CustomizeLayer;
  31. }
  32. int maxId = PDFViewer.GetMaxViewIndex();
  33. CustomizeLayer customizeLayer = new CustomizeLayer();
  34. PDFViewer.InsertView(maxId, customizeLayer);
  35. PDFCompareViewID = customizeLayer.GetResTag();
  36. PDFViewer.DrawChanged -= DrawChangedNotify;
  37. PDFViewer.DrawChanged += DrawChangedNotify;
  38. return customizeLayer;
  39. }
  40. private void DrawPDFCompare()
  41. {
  42. if (PDFCompareViewID==-1)
  43. {
  44. return;
  45. }
  46. CustomizeLayer drawLayer = GetPDFCompareDrawView();
  47. if (drawLayer == null)
  48. {
  49. return;
  50. }
  51. if (PDFViewer.CurrentRenderFrame == null)
  52. {
  53. return;
  54. }
  55. RenderFrame frame = PDFViewer.CurrentRenderFrame;
  56. List<RenderData> drawDataList = frame.GetRenderDatas();
  57. if (drawDataList == null || drawDataList.Count == 0)
  58. {
  59. return;
  60. }
  61. RenderData drawData = null;
  62. foreach (RenderData data in drawDataList)
  63. {
  64. if (data.PageIndex == PDFComparePageIndex)
  65. {
  66. drawData = data;
  67. break;
  68. }
  69. }
  70. DrawingContext drawDC= drawLayer.RenderOpen();
  71. if (drawData != null && PDFCompareRectList != null)
  72. {
  73. foreach(Rect drawRect in PDFCompareRectList)
  74. {
  75. Rect standRect = DpiHelper.PDFRectToStandardRect(drawRect);
  76. Rect paintRect = new Rect(
  77. standRect.Left * frame.ZoomFactor,
  78. standRect.Top * frame.ZoomFactor,
  79. standRect.Width * frame.ZoomFactor,
  80. standRect.Height * frame.ZoomFactor);
  81. Rect offsetRect = new Rect(
  82. paintRect.Left + drawData.PageBound.Left,
  83. paintRect.Top + drawData.PageBound.Top,
  84. paintRect.Width,
  85. paintRect.Height);
  86. drawDC.DrawRectangle(PDFCompareBrush,PDFComparePen,offsetRect);
  87. }
  88. }
  89. drawDC.Close();
  90. }
  91. private void DrawChangedNotify(object sender, EventArgs e)
  92. {
  93. DrawPDFCompare();
  94. }
  95. public void SetPDFCompareView(SolidColorBrush FillBrush,Pen BorderPen,int PageIndex,List<Rect> PDFRectList)
  96. {
  97. PDFCompareRectList=PDFRectList;
  98. PDFComparePageIndex = PageIndex;
  99. PDFCompareBrush = FillBrush;
  100. PDFComparePen = BorderPen;
  101. if(PDFCompareViewID==-1)
  102. {
  103. GetPDFCompareDrawView();
  104. }
  105. DrawPDFCompare();
  106. }
  107. public void ClearPDFCompareView()
  108. {
  109. CustomizeLayer drawLayer = GetPDFCompareDrawView();
  110. if (drawLayer != null && PDFViewer != null)
  111. {
  112. PDFViewer.RemoveView(drawLayer);
  113. PDFCompareViewID = -1;
  114. }
  115. }
  116. }
  117. }