CPDFAnnotationListControl.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.Help;
  6. using ComPDFKitViewer;
  7. using ComPDFKitViewer.BaseObject;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.IO;
  11. using System.Windows;
  12. using System.Windows.Annotations;
  13. using System.Windows.Controls;
  14. using ComPDFKit.Controls.Helper;
  15. using static ComPDFKit.Controls.PDFControlUI.CPDFAnnotationListUI;
  16. using ComPDFKit.Controls.PDFControlUI;
  17. using System.Windows.Input;
  18. using System;
  19. namespace ComPDFKit.Controls.PDFControl
  20. {
  21. public partial class CPDFAnnotationListControl : UserControl
  22. {
  23. private List<C_ANNOTATION_TYPE> OmitList = new List<C_ANNOTATION_TYPE>
  24. {
  25. C_ANNOTATION_TYPE.C_ANNOTATION_UNKOWN,
  26. C_ANNOTATION_TYPE.C_ANNOTATION_LINK,
  27. C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET,
  28. C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE,
  29. C_ANNOTATION_TYPE.C_ANNOTATION_SOUND,
  30. C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA
  31. };
  32. /// <summary>
  33. /// PDFViewer
  34. /// </summary>
  35. private PDFViewControl pdfViewer;
  36. public CPDFAnnotationListControl()
  37. {
  38. InitializeComponent();
  39. Loaded += CPDFAnnotationListControl_Loaded;
  40. }
  41. private void CPDFAnnotationListControl_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. AnnotationList.DeleteItemHandler -= AnnotationList_DeleteItemHandler;
  44. AnnotationList.DeleteItemHandler += AnnotationList_DeleteItemHandler;
  45. AnnotationList.ReplyStatusChanged -= AnnotationList_ReplyStatusChanged;
  46. AnnotationList.ReplyStatusChanged += AnnotationList_ReplyStatusChanged;
  47. AnnotationReplyListControl.ReplyListChanged -= AnnotationReplyListControl_ReplyListChanged;
  48. AnnotationReplyListControl.ReplyListChanged += AnnotationReplyListControl_ReplyListChanged;
  49. }
  50. private void AnnotationList_ReplyStatusChanged(object sender, CPDFAnnotationState e)
  51. {
  52. if (sender is ReplyStatusControl replyStatusControl)
  53. {
  54. if (replyStatusControl.DataContext is AnnotationBindData data)
  55. {
  56. if (pdfViewer != null)
  57. {
  58. CPDFAnnotation annot = data.BindProperty.Annotation;
  59. if (annot != null)
  60. {
  61. annot.SetState(e);
  62. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  63. pdfViewer.PDFViewTool.IsDocumentModified = true;
  64. }
  65. }
  66. }
  67. }
  68. else if (sender is CheckBox checkBox)
  69. {
  70. if (checkBox.DataContext is AnnotationBindData data)
  71. {
  72. if (pdfViewer != null)
  73. {
  74. CPDFAnnotation annot = data.BindProperty.Annotation;
  75. if (annot != null)
  76. {
  77. annot.SetMarkedAnnotState(checkBox.IsChecked == true ? CPDFAnnotationState.C_ANNOTATION_MARKED : CPDFAnnotationState.C_ANNOTATION_UNMARKED, "");
  78. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  79. pdfViewer.PDFViewTool.IsDocumentModified = true;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. private void AnnotationReplyListControl_ReplyListChanged(object sender, System.EventArgs e)
  86. {
  87. pdfViewer.PDFViewTool.IsDocumentModified = true;
  88. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  89. }
  90. private void AnnotationList_DeleteItemHandler(object sender, Dictionary<int, List<int>> e)
  91. {
  92. if (pdfViewer != null)
  93. {
  94. pdfViewer.PDFToolManager.ClearSelect();
  95. ParamConverter.RemovePageAnnot(e, pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  96. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  97. LoadAnnotationList();
  98. }
  99. }
  100. public void InitWithPDFViewer(PDFViewControl newPDFView)
  101. {
  102. pdfViewer = newPDFView;
  103. pdfViewer.PDFToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  104. pdfViewer.PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  105. }
  106. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  107. {
  108. if (OmitList.Contains(e.annotType))
  109. {
  110. AnnotationList.SelectAnnotationChanged(-1);
  111. }
  112. else
  113. {
  114. BaseAnnot baseAnnot = pdfViewer.PDFToolManager.GetCacheHitTestAnnot();
  115. AnnotData annotData = baseAnnot?.GetAnnotData();
  116. if (annotData != null)
  117. {
  118. AnnotationList.SelectAnnotationChanged(annotData.PageIndex, annotData.AnnotIndex);
  119. }
  120. }
  121. }
  122. public void LoadAnnotationList()
  123. {
  124. if (pdfViewer == null || pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument() == null)
  125. {
  126. return;
  127. }
  128. if (pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().IsLocked)
  129. {
  130. return;
  131. }
  132. int pageCount = pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().PageCount;
  133. List<BindAnnotationResult> bindAnnotationResults = new List<BindAnnotationResult>();
  134. pdfViewer.UpdateAnnotFrame();
  135. for (int i = 0; i < pageCount; i++)
  136. {
  137. List<AnnotParam> annotList = GetAnnotCommentList(i, pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  138. if (annotList != null && annotList.Count > 0)
  139. {
  140. Dispatcher.Invoke(() =>
  141. {
  142. foreach (AnnotParam annot in annotList)
  143. {
  144. CPDFAnnotation annotCore = pdfViewer?.GetCPDFViewer()?.GetAnnotForIndex(i, annot.AnnotIndex)?.GetAnnotData()?.Annot;
  145. if (annotCore == null || annotCore.IsReplyAnnot())
  146. {
  147. continue;
  148. }
  149. var bindResult = new BindAnnotationResult
  150. {
  151. PageIndex = i,
  152. annotationData = annot,
  153. pdfViewer = pdfViewer,
  154. ReplyState = annotCore.GetState(),
  155. IsMarkState = annotCore.IsMarkedStateAnnot()
  156. };
  157. List<CPDFAnnotation> replyAnnotations = annotCore?.GetReplies();
  158. if (replyAnnotations != null && replyAnnotations.Count > 0)
  159. {
  160. foreach (CPDFAnnotation replyAnnot in replyAnnotations)
  161. {
  162. if (replyAnnot == null)
  163. {
  164. continue;
  165. }
  166. bindResult.ReplyList.Add(new ReplyData
  167. {
  168. ReplyAnnotation = replyAnnot,
  169. });
  170. }
  171. }
  172. bindAnnotationResults.Add(bindResult);
  173. }
  174. });
  175. }
  176. }
  177. AnnotationList.SetAnnotationList(bindAnnotationResults);
  178. }
  179. public List<AnnotParam> GetAnnotCommentList(int pageIndex, CPDFDocument currentDoc)
  180. {
  181. List<AnnotParam> annotList = new List<AnnotParam>();
  182. if (pageIndex < 0 || pdfViewer == null || currentDoc == null)
  183. {
  184. return annotList;
  185. }
  186. CPDFPage docPage = currentDoc.PageAtIndex(pageIndex, false);
  187. if (docPage == null)
  188. {
  189. return annotList;
  190. }
  191. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  192. if (docAnnots != null && docAnnots.Count > 0)
  193. {
  194. foreach (CPDFAnnotation annotation in docAnnots)
  195. {
  196. if (annotation == null || OmitList.Contains(annotation.Type))
  197. {
  198. continue;
  199. }
  200. AnnotParam annotParam = ParamConverter.CPDFDataConverterToAnnotParam(currentDoc, pageIndex, annotation);
  201. if (annotParam != null)
  202. {
  203. annotList.Add(annotParam);
  204. }
  205. }
  206. }
  207. return annotList;
  208. }
  209. private void AnnotationList_Loaded(object sender, RoutedEventArgs e)
  210. {
  211. AnnotationList.AnnotationSelectionChanged -= AnnotationList_AnnotationSelectionChanged;
  212. AnnotationList.AnnotationSelectionChanged += AnnotationList_AnnotationSelectionChanged;
  213. }
  214. private void AnnotationList_Unloaded(object sender, RoutedEventArgs e)
  215. {
  216. AnnotationList.AnnotationSelectionChanged -= AnnotationList_AnnotationSelectionChanged;
  217. }
  218. private void AnnotationList_AnnotationSelectionChanged(object sender, object e)
  219. {
  220. var bindAnnotationResult = e as BindAnnotationResult;
  221. pdfViewer.PDFViewTool.GetCPDFViewer().GoToPage(bindAnnotationResult.PageIndex, new Point(bindAnnotationResult.annotationData.ClientRect.left, bindAnnotationResult.annotationData.ClientRect.top));
  222. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateRenderFrame();
  223. pdfViewer.PDFViewTool.SelectedAnnotForIndex(bindAnnotationResult.PageIndex, bindAnnotationResult.AnnotIndex);
  224. }
  225. private void ImportBtn_Click(object sender, RoutedEventArgs e)
  226. {
  227. var selectedPath = Helper.CommonHelper.GetExistedPathOrEmpty("XFDF Files (*.xfdf)|*.xfdf");
  228. if (string.IsNullOrEmpty(selectedPath)) return;
  229. var tempPath = Path.Combine(Path.GetDirectoryName(selectedPath), "temp");
  230. if (!Directory.Exists(tempPath))
  231. {
  232. Directory.CreateDirectory(tempPath);
  233. }
  234. pdfViewer.PDFToolManager.GetDocument().ImportAnnotationFromXFDFPath(selectedPath, tempPath);
  235. LoadAnnotationList();
  236. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateVirtualNodes();
  237. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateRenderFrame();
  238. //pdfViewer.UndoManager.CanSave = true;
  239. }
  240. private void ExportBtn_Click(object sender, RoutedEventArgs e)
  241. {
  242. var selectedPath = Helper.CommonHelper.GetGeneratePathOrEmpty("XFDF Files (*.xfdf)|*.xfdf", pdfViewer.PDFToolManager.GetDocument().FileName);
  243. if (string.IsNullOrEmpty(selectedPath)) return;
  244. var tempPath = Path.Combine(Path.GetDirectoryName(selectedPath), "temp");
  245. if (!Directory.Exists(tempPath))
  246. {
  247. Directory.CreateDirectory(tempPath);
  248. }
  249. if (pdfViewer.PDFToolManager.GetDocument().ExportAnnotationToXFDFPath(selectedPath, tempPath))
  250. {
  251. System.Diagnostics.Process.Start("explorer.exe", "/select," + selectedPath);
  252. }
  253. }
  254. }
  255. public class ExpandAllReplyCommand : ICommand
  256. {
  257. public event EventHandler CanExecuteChanged;
  258. public bool CanExecute(object parameter)
  259. {
  260. return true;
  261. }
  262. public void Execute(object parameter)
  263. {
  264. if (parameter is CPDFAnnotationListControl annotationListControl)
  265. {
  266. annotationListControl.AnnotationList.ExpandAllReply(true);
  267. }
  268. }
  269. }
  270. public class FoldAllReplyCommand : ICommand
  271. {
  272. public event EventHandler CanExecuteChanged;
  273. public bool CanExecute(object parameter)
  274. {
  275. return true;
  276. }
  277. public void Execute(object parameter)
  278. {
  279. if (parameter is CPDFAnnotationListControl annotationListControl)
  280. {
  281. annotationListControl.AnnotationList.ExpandAllReply(false);
  282. }
  283. }
  284. }
  285. public class DeleteAllAnnotCommand : ICommand
  286. {
  287. public event EventHandler CanExecuteChanged;
  288. public bool CanExecute(object parameter)
  289. {
  290. return true;
  291. }
  292. public void Execute(object parameter)
  293. {
  294. if (parameter is CPDFAnnotationListControl annotationListControl)
  295. {
  296. annotationListControl.AnnotationList.DeleteAllReply();
  297. annotationListControl.LoadAnnotationList();
  298. annotationListControl.AnnotationList.DeleteAllAnnot();
  299. }
  300. }
  301. }
  302. public class DeleteAllReplyCommand : ICommand
  303. {
  304. public event EventHandler CanExecuteChanged;
  305. public bool CanExecute(object parameter)
  306. {
  307. return true;
  308. }
  309. public void Execute(object parameter)
  310. {
  311. if (parameter is CPDFAnnotationListControl annotationListControl)
  312. {
  313. annotationListControl.AnnotationList.DeleteAllReply();
  314. annotationListControl.LoadAnnotationList();
  315. }
  316. }
  317. }
  318. }