CPDFAnnotationListControl.xaml.cs 14 KB

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