CPDFAnnotationListControl.xaml.cs 15 KB

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