CropPageUndoManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.PdfViewer;
  5. using Microsoft.Office.Core;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. namespace PDF_Master.Helper
  13. {
  14. public enum CropPageEnum
  15. {
  16. CropCurrentPageWM,
  17. CropAllPagesWM
  18. }
  19. public class AreaCropPageUndoManager : IHistory
  20. {
  21. private CPDFViewer pdfviewer = null;
  22. private List<Tuple<List<int>, Rect,Size>> history = new List<Tuple<List<int>, Rect, Size>>();
  23. private int historyIndex = -1;
  24. public AreaCropPageUndoManager(CPDFViewer pDFViewer) {
  25. pdfviewer = pDFViewer;
  26. }
  27. public void ADDAreaCropPage(List<int> pageindex, Rect rect,Size pagesize)
  28. {
  29. historyIndex++;
  30. history.Add(new Tuple<List<int>, Rect, Size>(pageindex, rect, pagesize));
  31. }
  32. public bool Redo()
  33. {
  34. historyIndex++;
  35. if (historyIndex < history.Count)
  36. {
  37. pdfviewer?.CropPage(CPDFDisplayBox.CropBox, history[historyIndex].Item2, history[historyIndex].Item1);
  38. pdfviewer.UndoManager.CanSave = true;
  39. return true;
  40. }
  41. else
  42. {
  43. return false;
  44. }
  45. }
  46. public bool Undo()
  47. {
  48. if (historyIndex > -1)
  49. {
  50. Rect newRect =new Rect( -history[historyIndex].Item2.Left, -history[historyIndex].Item2.Top, history[historyIndex].Item3.Width+ history[historyIndex].Item2.Left, history[historyIndex].Item3.Height + history[historyIndex].Item2.Top);
  51. pdfviewer?.CropPage(CPDFDisplayBox.CropBox, newRect, history[historyIndex].Item1);
  52. historyIndex--;
  53. pdfviewer.UndoManager.CanSave = true;
  54. return true;
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61. }
  62. public class CropPageUndoManager : IHistory
  63. {
  64. public List<int> cropPageList = new List<int>();
  65. private List<CropPageEnum> cropPageEnumList = new List<CropPageEnum>();
  66. private int cropPageEnumIndex = -1;
  67. private int cropPageListInterval = 0;
  68. private CPDFViewer pdfviewer = null;
  69. public CropPageUndoManager()
  70. {
  71. }
  72. public void setPDFViewer(CPDFViewer pDFViewer)
  73. {
  74. pdfviewer = pDFViewer;
  75. }
  76. public void ADDCropCurrentPageWM()
  77. {
  78. cropPageEnumIndex++;
  79. cropPageEnumList.Insert(cropPageEnumIndex, CropPageEnum.CropCurrentPageWM);
  80. if (cropPageEnumIndex != cropPageEnumList.Count - 1)
  81. {
  82. cropPageEnumList.RemoveRange(cropPageEnumIndex + 1, cropPageEnumList.Count - cropPageEnumIndex - 1);
  83. cropPageList.RemoveRange(cropPageList.Count - cropPageListInterval - 1, cropPageListInterval);
  84. cropPageListInterval = 0;
  85. }
  86. }
  87. public void ADDCropAllPagesWM()
  88. {
  89. cropPageEnumIndex++;
  90. cropPageEnumList.Insert(cropPageEnumIndex, CropPageEnum.CropAllPagesWM);
  91. if (cropPageEnumIndex != cropPageEnumList.Count - 1)
  92. {
  93. cropPageEnumList.RemoveRange(cropPageEnumIndex + 1, cropPageEnumList.Count - cropPageEnumIndex - 1);
  94. cropPageList.RemoveRange(cropPageList.Count - cropPageListInterval - pdfviewer.Document.PageCount, cropPageListInterval);
  95. cropPageListInterval = 0;
  96. }
  97. }
  98. public void setPageList(List<int> CropPageList)
  99. {
  100. cropPageList = CropPageList;
  101. }
  102. public bool Redo()
  103. {
  104. cropPageEnumIndex++;
  105. if (cropPageEnumIndex < cropPageEnumList.Count)
  106. {
  107. if (cropPageEnumList[cropPageEnumIndex] == CropPageEnum.CropAllPagesWM) { cropPageListInterval -= pdfviewer.Document.PageCount; } else { cropPageListInterval--; }
  108. if (cropPageList.Count - cropPageListInterval == 0) { pdfviewer.SetCropMode(false); } else { pdfviewer.SetCropMode(true, cropPageList.GetRange(0, cropPageList.Count - cropPageListInterval)); }
  109. pdfviewer.UndoManager.CanSave = true;
  110. return true;
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
  117. public bool Undo()
  118. {
  119. if (cropPageEnumIndex > -1)
  120. {
  121. if (cropPageEnumList[cropPageEnumIndex] == CropPageEnum.CropAllPagesWM)
  122. {
  123. cropPageListInterval += pdfviewer.Document.PageCount;
  124. }
  125. else
  126. {
  127. cropPageListInterval++;
  128. }
  129. if (cropPageList.Count - cropPageListInterval == 0) { pdfviewer.SetCropMode(false); } else { pdfviewer.SetCropMode(true, cropPageList.GetRange(0, cropPageList.Count - cropPageListInterval)); }
  130. cropPageEnumIndex--;
  131. pdfviewer.UndoManager.CanSave = true;
  132. return true;
  133. }
  134. else
  135. {
  136. return false;
  137. }
  138. }
  139. }
  140. }