CPDFAnnotationListControl.xaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.CPDFAnnoationListUI;
  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. }
  43. private void AnnotationList_DeleteItemHandler(object sender, Dictionary<int, List<int>> e)
  44. {
  45. if (pdfViewer != null)
  46. {
  47. pdfViewer.PDFToolManager.ClearSelect();
  48. ParamConverter.RemovePageAnnot(e, pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  49. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateAnnotFrame();
  50. LoadAnnotationList();
  51. }
  52. }
  53. public void InitWithPDFViewer(PDFViewControl newPDFView)
  54. {
  55. pdfViewer = newPDFView;
  56. pdfViewer.PDFToolManager.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler;
  57. pdfViewer.PDFToolManager.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler;
  58. }
  59. private void PDFToolManager_MouseLeftButtonDownHandler(object sender, ComPDFKit.Tool.MouseEventObject e)
  60. {
  61. if (OmitList.Contains(e.annotType))
  62. {
  63. AnnotationList.SelectAnnotationChanged(-1);
  64. }
  65. else
  66. {
  67. BaseAnnot baseAnnot= pdfViewer.PDFToolManager.GetCacheHitTestAnnot();
  68. AnnotData annotData = baseAnnot?.GetAnnotData();
  69. if(annotData != null)
  70. {
  71. AnnotationList.SelectAnnotationChanged(annotData.PageIndex, annotData.AnnotIndex);
  72. }
  73. }
  74. }
  75. public void LoadAnnotationList()
  76. {
  77. if (pdfViewer == null || pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument() == null)
  78. {
  79. return;
  80. }
  81. if (pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().IsLocked)
  82. {
  83. return;
  84. }
  85. int pageCount = pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument().PageCount;
  86. List<BindAnnotationResult> bindAnnotationResults = new List<BindAnnotationResult>();
  87. for (int i = 0; i < pageCount; i++)
  88. {
  89. List<AnnotParam> annotList = GetAnnotCommentList(i, pdfViewer.PDFViewTool.GetCPDFViewer().GetDocument());
  90. if (annotList != null&& annotList.Count>0)
  91. {
  92. Dispatcher.Invoke(() =>
  93. {
  94. foreach (AnnotParam annot in annotList)
  95. {
  96. bindAnnotationResults.Add(new BindAnnotationResult
  97. {
  98. PageIndex = i,
  99. annotationData = annot
  100. });
  101. }
  102. });
  103. }
  104. }
  105. AnnotationList.SetAnnotationList(bindAnnotationResults);
  106. }
  107. public List<AnnotParam> GetAnnotCommentList(int pageIndex, CPDFDocument currentDoc)
  108. {
  109. List<AnnotParam> annotList = new List<AnnotParam>();
  110. if (pageIndex < 0 || pdfViewer == null || currentDoc == null)
  111. {
  112. return annotList;
  113. }
  114. CPDFPage docPage = currentDoc.PageAtIndex(pageIndex, false);
  115. if (docPage == null)
  116. {
  117. return annotList;
  118. }
  119. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  120. if (docAnnots != null && docAnnots.Count > 0)
  121. {
  122. foreach (CPDFAnnotation annotation in docAnnots)
  123. {
  124. if (annotation==null|| OmitList.Contains(annotation.Type))
  125. {
  126. continue;
  127. }
  128. AnnotParam annotParam= ParamConverter.CPDFDataConverterToAnnotParam(currentDoc,pageIndex,annotation);
  129. if (annotParam!=null)
  130. {
  131. annotList.Add(annotParam);
  132. }
  133. }
  134. }
  135. return annotList;
  136. }
  137. private void AnnotationList_Loaded(object sender, RoutedEventArgs e)
  138. {
  139. AnnotationList.AnnotationSelectionChanged -= AnnotationList_AnnotationSelectionChanged;
  140. AnnotationList.AnnotationSelectionChanged += AnnotationList_AnnotationSelectionChanged;
  141. }
  142. private void AnnotationList_Unloaded(object sender, RoutedEventArgs e)
  143. {
  144. AnnotationList.AnnotationSelectionChanged -= AnnotationList_AnnotationSelectionChanged;
  145. }
  146. private void AnnotationList_AnnotationSelectionChanged(object sender, object e)
  147. {
  148. var bindAnnotationResult = e as BindAnnotationResult;
  149. pdfViewer.PDFViewTool.GetCPDFViewer().GoToPage(bindAnnotationResult.PageIndex, new Point(bindAnnotationResult.annotationData.ClientRect.left, bindAnnotationResult.annotationData.ClientRect.top));
  150. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateRenderFrame();
  151. pdfViewer.PDFViewTool.SelectedAnnotForIndex(bindAnnotationResult.PageIndex, bindAnnotationResult.AnnotIndex);
  152. }
  153. private void ImportBtn_Click(object sender, RoutedEventArgs e)
  154. {
  155. var selectedPath = Helper.CommonHelper.GetExistedPathOrEmpty("XFDF Files (*.xfdf)|*.xfdf");
  156. if (string.IsNullOrEmpty(selectedPath)) return;
  157. var tempPath = Path.Combine(Path.GetDirectoryName(selectedPath), "temp");
  158. if (!Directory.Exists(tempPath))
  159. {
  160. Directory.CreateDirectory(tempPath);
  161. }
  162. pdfViewer.PDFToolManager.GetDocument().ImportAnnotationFromXFDFPath(selectedPath,tempPath);
  163. LoadAnnotationList();
  164. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateVirtualNodes();
  165. pdfViewer.PDFViewTool.GetCPDFViewer().UpdateRenderFrame();
  166. //pdfViewer.UndoManager.CanSave = true;
  167. }
  168. private void ExportBtn_Click(object sender, RoutedEventArgs e)
  169. {
  170. var selectedPath = Helper.CommonHelper.GetGeneratePathOrEmpty("XFDF Files (*.xfdf)|*.xfdf", pdfViewer.PDFToolManager.GetDocument().FileName);
  171. if (string.IsNullOrEmpty(selectedPath)) return;
  172. var tempPath = Path.Combine(Path.GetDirectoryName(selectedPath), "temp");
  173. if (!Directory.Exists(tempPath))
  174. {
  175. Directory.CreateDirectory(tempPath);
  176. }
  177. if (pdfViewer.PDFToolManager.GetDocument().ExportAnnotationToXFDFPath(selectedPath, tempPath))
  178. {
  179. System.Diagnostics.Process.Start("explorer.exe", "/select," + selectedPath);
  180. }
  181. }
  182. }
  183. }