CPDFViewerTool.PDFCompareEffect.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = maxId;
  36. PDFViewer.DrawChanged -= DrawChangedNotify;
  37. PDFViewer.DrawChanged += DrawChangedNotify;
  38. return customizeLayer;
  39. }
  40. private void DrawPDFCompare()
  41. {
  42. CustomizeLayer drawLayer = GetPDFCompareDrawView();
  43. if (drawLayer == null)
  44. {
  45. return;
  46. }
  47. if (PDFViewer.CurrentRenderFrame == null)
  48. {
  49. return;
  50. }
  51. RenderFrame frame = PDFViewer.CurrentRenderFrame;
  52. List<RenderData> drawDataList = frame.GetRenderDatas();
  53. if (drawDataList == null || drawDataList.Count == 0)
  54. {
  55. return;
  56. }
  57. RenderData drawData = null;
  58. foreach (RenderData data in drawDataList)
  59. {
  60. if (data.PageIndex == PDFComparePageIndex)
  61. {
  62. drawData = data;
  63. break;
  64. }
  65. }
  66. DrawingContext drawDC= drawLayer.RenderOpen();
  67. if (drawData != null && PDFCompareRectList != null)
  68. {
  69. foreach(Rect drawRect in PDFCompareRectList)
  70. {
  71. Rect standRect = DpiHelper.PDFRectToStandardRect(drawRect);
  72. Rect paintRect = new Rect(
  73. standRect.Left * frame.ZoomFactor,
  74. standRect.Top * frame.ZoomFactor,
  75. standRect.Width * frame.ZoomFactor,
  76. standRect.Height * frame.ZoomFactor);
  77. Rect offsetRect = new Rect(
  78. paintRect.Left + drawData.PageBound.Left,
  79. paintRect.Top + drawData.PageBound.Top,
  80. paintRect.Width,
  81. paintRect.Height);
  82. drawDC.DrawRectangle(PDFCompareBrush,PDFComparePen,offsetRect);
  83. }
  84. }
  85. drawDC.Close();
  86. }
  87. private void DrawChangedNotify(object sender, EventArgs e)
  88. {
  89. DrawPDFCompare();
  90. }
  91. public void SetPDFCompareView(SolidColorBrush FillBrush,Pen BorderPen,int PageIndex,List<Rect> PDFRectList)
  92. {
  93. PDFCompareRectList=PDFRectList;
  94. PDFComparePageIndex = PageIndex;
  95. PDFCompareBrush = FillBrush;
  96. PDFComparePen = BorderPen;
  97. DrawPDFCompare();
  98. }
  99. public void ClearPDFCompareView()
  100. {
  101. CustomizeLayer drawLayer = GetPDFCompareDrawView();
  102. if (drawLayer != null && PDFViewer != null)
  103. {
  104. PDFViewer.RemoveView(drawLayer);
  105. PDFCompareViewID = -1;
  106. }
  107. }
  108. }
  109. }