CropPageUndoManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PDF_Office.Helper
  10. {
  11. public enum CropPageEnum
  12. {
  13. CropCurrentPageWM,
  14. CropAllPagesWM
  15. }
  16. public class CropPageUndoManager : IHistory
  17. {
  18. public List<int> cropPageList = new List<int>();
  19. private List<CropPageEnum> cropPageEnumList = new List<CropPageEnum>();
  20. private int cropPageEnumIndex = -1;
  21. private int cropPageListInterval = 0;
  22. private CPDFViewer pdfviewer = null;
  23. public CropPageUndoManager()
  24. {
  25. }
  26. public void setPDFViewer(CPDFViewer pDFViewer)
  27. {
  28. pdfviewer = pDFViewer;
  29. }
  30. public void ADDCropCurrentPageWM()
  31. {
  32. cropPageEnumIndex++;
  33. cropPageEnumList.Insert(cropPageEnumIndex, CropPageEnum.CropCurrentPageWM);
  34. if (cropPageEnumIndex != cropPageEnumList.Count - 1)
  35. {
  36. cropPageEnumList.RemoveRange(cropPageEnumIndex + 1, cropPageEnumList.Count - cropPageEnumIndex-1);
  37. cropPageList.RemoveRange(cropPageList.Count - cropPageListInterval-1, cropPageListInterval);
  38. cropPageListInterval = 0;
  39. }
  40. }
  41. public void ADDCropAllPagesWM()
  42. {
  43. cropPageEnumIndex++;
  44. cropPageEnumList.Insert(cropPageEnumIndex, CropPageEnum.CropAllPagesWM);
  45. if (cropPageEnumIndex != cropPageEnumList.Count - 1)
  46. {
  47. cropPageEnumList.RemoveRange(cropPageEnumIndex+1, cropPageEnumList.Count - cropPageEnumIndex-1);
  48. cropPageList.RemoveRange(cropPageList.Count - cropPageListInterval-37, cropPageListInterval);
  49. cropPageListInterval = 0;
  50. }
  51. }
  52. public void setPageList(List<int> CropPageList)
  53. {
  54. cropPageList = CropPageList;
  55. }
  56. public bool Redo()
  57. {
  58. cropPageEnumIndex++;
  59. if (cropPageEnumIndex < cropPageEnumList.Count )
  60. {
  61. if (cropPageEnumList[cropPageEnumIndex] == CropPageEnum.CropAllPagesWM) { cropPageListInterval -= pdfviewer.Document.PageCount; } else { cropPageListInterval--; }
  62. if (cropPageList.Count - cropPageListInterval == 0) { pdfviewer.SetCropMode(false); } else { pdfviewer.SetCropMode(true, cropPageList.GetRange(0, cropPageList.Count - cropPageListInterval)); }
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. public bool Undo()
  71. {
  72. if (cropPageEnumIndex > -1)
  73. {
  74. if (cropPageEnumList[cropPageEnumIndex] == CropPageEnum.CropAllPagesWM)
  75. {
  76. cropPageListInterval += pdfviewer.Document.PageCount;
  77. }
  78. else
  79. {
  80. cropPageListInterval++;
  81. }
  82. if (cropPageList.Count - cropPageListInterval == 0) { pdfviewer.SetCropMode(false); } else { pdfviewer.SetCropMode(true, cropPageList.GetRange(0, cropPageList.Count - cropPageListInterval)); }
  83. cropPageEnumIndex--;
  84. return true;
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. }
  92. }