using ComPDFKit.Import; using ComPDFKit.PDFPage; using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace PDF_Master.Helper { public enum CropPageEnum { CropCurrentPageWM, CropAllPagesWM } public class AreaCropPageUndoManager : IHistory { private CPDFViewer pdfviewer = null; private List, Rect,Size>> history = new List, Rect, Size>>(); private int historyIndex = -1; public AreaCropPageUndoManager(CPDFViewer pDFViewer) { pdfviewer = pDFViewer; } public void ADDAreaCropPage(List pageindex, Rect rect,Size pagesize) { historyIndex++; history.Add(new Tuple, Rect, Size>(pageindex, rect, pagesize)); } public bool Redo() { historyIndex++; if (historyIndex < history.Count) { pdfviewer?.CropPage(CPDFDisplayBox.CropBox, history[historyIndex].Item2, history[historyIndex].Item1); pdfviewer.UndoManager.CanSave = true; return true; } else { return false; } } public bool Undo() { if (historyIndex > -1) { 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); pdfviewer?.CropPage(CPDFDisplayBox.CropBox, newRect, history[historyIndex].Item1); historyIndex--; pdfviewer.UndoManager.CanSave = true; return true; } else { return false; } } } public class CropPageUndoManager : IHistory { public List cropPageList = new List(); private List cropPageEnumList = new List(); private int cropPageEnumIndex = -1; private int cropPageListInterval = 0; private CPDFViewer pdfviewer = null; public CropPageUndoManager() { } public void setPDFViewer(CPDFViewer pDFViewer) { pdfviewer = pDFViewer; } public void ADDCropCurrentPageWM() { cropPageEnumIndex++; cropPageEnumList.Insert(cropPageEnumIndex, CropPageEnum.CropCurrentPageWM); if (cropPageEnumIndex != cropPageEnumList.Count - 1) { cropPageEnumList.RemoveRange(cropPageEnumIndex + 1, cropPageEnumList.Count - cropPageEnumIndex - 1); cropPageList.RemoveRange(cropPageList.Count - cropPageListInterval - 1, cropPageListInterval); cropPageListInterval = 0; } } public void ADDCropAllPagesWM() { cropPageEnumIndex++; cropPageEnumList.Insert(cropPageEnumIndex, CropPageEnum.CropAllPagesWM); if (cropPageEnumIndex != cropPageEnumList.Count - 1) { cropPageEnumList.RemoveRange(cropPageEnumIndex + 1, cropPageEnumList.Count - cropPageEnumIndex - 1); cropPageList.RemoveRange(cropPageList.Count - cropPageListInterval - pdfviewer.Document.PageCount, cropPageListInterval); cropPageListInterval = 0; } } public void setPageList(List CropPageList) { cropPageList = CropPageList; } public bool Redo() { cropPageEnumIndex++; if (cropPageEnumIndex < cropPageEnumList.Count) { if (cropPageEnumList[cropPageEnumIndex] == CropPageEnum.CropAllPagesWM) { cropPageListInterval -= pdfviewer.Document.PageCount; } else { cropPageListInterval--; } if (cropPageList.Count - cropPageListInterval == 0) { pdfviewer.SetCropMode(false); } else { pdfviewer.SetCropMode(true, cropPageList.GetRange(0, cropPageList.Count - cropPageListInterval)); } pdfviewer.UndoManager.CanSave = true; return true; } else { return false; } } public bool Undo() { if (cropPageEnumIndex > -1) { if (cropPageEnumList[cropPageEnumIndex] == CropPageEnum.CropAllPagesWM) { cropPageListInterval += pdfviewer.Document.PageCount; } else { cropPageListInterval++; } if (cropPageList.Count - cropPageListInterval == 0) { pdfviewer.SetCropMode(false); } else { pdfviewer.SetCropMode(true, cropPageList.GetRange(0, cropPageList.Count - cropPageListInterval)); } cropPageEnumIndex--; pdfviewer.UndoManager.CanSave = true; return true; } else { return false; } } } }