123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.Tool;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Input;
- using ComPDFKit.Controls.Helper;
- using System.Collections.Specialized;
- using ComPDFKit.Controls.PDFControl;
- using static ComPDFKit.Controls.PDFControlUI.CPDFAnnotationListUI;
- using ComPDFKit.Controls.Data;
- namespace ComPDFKit.Controls.PDFControlUI
- {
- public class ShowReplyListCommand : ICommand
- {
- public event EventHandler CanExecuteChanged;
- public bool CanExecute(object parameter)
- {
- return true;
- }
- public void Execute(object parameter)
- {
- if (parameter is AnnotationBindData data)
- {
- data.IsReplyListVisible = !data.IsReplyListVisible;
- }
- }
- }
- public class ShowReplyInputCommand : ICommand
- {
- public event EventHandler CanExecuteChanged;
- public bool CanExecute(object parameter)
- {
- return true;
- }
- public void Execute(object parameter)
- {
- if (parameter is AnnotationBindData data)
- {
- data.IsReplyInputVisible = !data.IsReplyInputVisible;
- if(data.IsReplyInputVisible)
- {
- data.IsReplyListVisible = true;
- }
- }
- }
- }
- internal class AnnotationBindData : INotifyPropertyChanged
- {
- public PDFViewControl pdfViewer { get; set; }
- public AnnotParam annotationData { get; set; }
- public string ReplyCount
- {
- get => ReplyList.Count.ToString();
- }
- private bool _isReplyListVisible = true;
- public bool IsReplyListVisible
- {
- get { return _isReplyListVisible; }
- set
- {
- _isReplyListVisible = value;
- OnPropertyChanged(nameof(IsReplyListVisible));
- }
- }
- private bool _isReplyInputVisible;
- public bool IsReplyInputVisible
- {
- get { return _isReplyInputVisible; }
- set
- {
- _isReplyInputVisible = value;
- OnPropertyChanged(nameof(IsReplyInputVisible));
- }
- }
- public int PageIndex { get; set; }
- public int AnnotIndex { get => annotationData.AnnotIndex; }
- public string Author
- {
- get => annotationData.Author;
- }
- public string Note
- {
- get => annotationData.Content;
- }
- private CPDFAnnotation _annotation;
- public CPDFAnnotation Annotation
- {
- get
- {
- List<CPDFAnnotation> annotCoreList = pdfViewer?.GetCPDFViewer()?.GetDocument()?.PageAtIndex(annotationData.PageIndex, false)?.GetAnnotations();
- return annotCoreList[annotationData.AnnotIndex];
- }
- }
- private ObservableCollection<ReplyData> _replyList = new ObservableCollection<ReplyData>();
- public ObservableCollection<ReplyData> ReplyList
- {
- get => _replyList;
- set
- {
- if (_replyList != value)
- {
- if (_replyList != null)
- {
- // Unsubscribe from the previous collection
- _replyList.CollectionChanged -= ReplyList_CollectionChanged;
- }
- _replyList = value;
- if (_replyList != null)
- {
- // Subscribe to the new collection
- _replyList.CollectionChanged += ReplyList_CollectionChanged;
- }
- OnPropertyChanged(nameof(ReplyList));
- }
- }
- }
- private ObservableCollection<AnnotationBindData> annotationList = new ObservableCollection<AnnotationBindData>();
- private void ReplyList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- // Notify that the ReplyCount has changed when the collection changes
- OnPropertyChanged(nameof(ReplyCount));
- }
- public BindAnnotationResult BindProperty { get; set; }
- public AnnotationBindData()
- {
- BindProperty = new BindAnnotationResult();
- }
- public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public partial class CPDFAnnotationListUI : UserControl
- {
- public event EventHandler<CPDFAnnotationState> ReplyStatusChanged;
- public class ReplyData
- {
- public CPDFAnnotation ReplyAnnotation { get; set; }
- public string Author
- {
- get => ReplyAnnotation.GetAuthor();
- set => ReplyAnnotation.SetAuthor(value);
- }
- public string Date
- {
- get
- {
- try
- {
- if (Regex.IsMatch(ReplyAnnotation.GetCreationDate(), "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
- {
- string dateStr = Regex.Match(ReplyAnnotation.GetCreationDate(), "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
- return (dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + ", " + dateStr.Substring(8, 2) + ":" +
- dateStr.Substring(10, 2));
- }
- else
- {
- return string.Empty;
- }
- }
- catch (Exception)
- {
- return string.Empty;
- }
- }
- set => ReplyAnnotation.SetCreationDate(value);
- }
- public string Content
- {
- get => ReplyAnnotation.GetContent();
- set => ReplyAnnotation.SetContent(value);
- }
- }
-
- public class BindAnnotationResult : INotifyPropertyChanged
- {
- private ObservableCollection<ReplyData> _replyList = new ObservableCollection<ReplyData>();
- public ObservableCollection<ReplyData> ReplyList
- {
- get => _replyList;
- set
- {
- if (_replyList != value)
- {
- if (_replyList != null)
- {
- // Unsubscribe from the previous collection
- _replyList.CollectionChanged -= ReplyList_CollectionChanged;
- }
- _replyList = value;
- if (_replyList != null)
- {
- // Subscribe to the new collection
- _replyList.CollectionChanged += ReplyList_CollectionChanged; ;
- }
- OnPropertyChanged(nameof(ReplyList));
- }
- }
- }
- private void ReplyList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- OnPropertyChanged(nameof(ReplyCount));
- }
- private Visibility _isAnnotListVisible = Visibility.Visible;
- public Visibility IsAnnotListVisible
- {
- get { return _isAnnotListVisible; }
- set
- {
- _isAnnotListVisible = value;
- OnPropertyChanged(nameof(IsAnnotListVisible));
- }
- }
- public int PageIndex { get; set; }
- public int AnnotIndex { get => annotationData.AnnotIndex; }
- public string Author
- {
- get => annotationData.Author;
- }
- public string ReplyCount
- {
- get => ReplyList.Count.ToString();
- }
- public string CreateDate
- {
- get
- {
- if (Regex.IsMatch(annotationData.CreateTime, "(?<=D\\:)[0-9]+(?=[\\+\\-])"))
- {
- string dateStr = Regex.Match(annotationData.CreateTime, "(?<=D\\:)[0-9]+(?=[\\+\\-])").Value;
- return (dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6, 2) + ", " + dateStr.Substring(8, 2) + ":" +
- dateStr.Substring(10, 2));
- }
- else
- {
- return string.Empty;
- }
- }
- }
- public string Note
- {
- get => annotationData.Content;
- }
- public CPDFAnnotationType CurrentAnnotationType
- {
- get
- {
- switch(Annotation.Type)
- {
- case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
- return CPDFAnnotationType.Circle;
- case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
- return CPDFAnnotationType.Square;
- case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
- {
- if(Annotation.IsMeasured())
- return CPDFAnnotationType.LineMeasure;
- else
- return CPDFAnnotationType.Line;
- }
- case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
- return CPDFAnnotationType.FreeText;
- case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
- return CPDFAnnotationType.Highlight;
- case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
- return CPDFAnnotationType.Squiggly;
- case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
- return CPDFAnnotationType.Strikeout;
- case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
- return CPDFAnnotationType.Underline;
- case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
- return CPDFAnnotationType.Freehand;
- case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
- return CPDFAnnotationType.Note;
- case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
- return CPDFAnnotationType.Stamp;
- case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
- {
- if (Annotation.IsMeasured())
- return CPDFAnnotationType.PolyLineMeasure;
- else
- return CPDFAnnotationType.PolyLine;
- }
- case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
- {
- if (Annotation.IsMeasured())
- return CPDFAnnotationType.PolygonMeasure;
- else
- return CPDFAnnotationType.Polygon;
- }
- default:
- return CPDFAnnotationType.Note;
- }
- }
- }
- public AnnotParam annotationData { get; set; }
- private CPDFAnnotation _annotation;
- public CPDFAnnotation Annotation
- {
- get
- {
- List<CPDFAnnotation> annotCoreList = pdfViewer?.GetCPDFViewer()?.GetDocument()?.PageAtIndex(annotationData.PageIndex, false)?.GetAnnotations();
- return annotCoreList[annotationData.AnnotIndex];
- }
- }
- public PDFViewControl pdfViewer { get; set; }
- public CPDFAnnotationState ReplyState { get; set; }
- public bool IsMarkState { get; set; }
- public BindAnnotationResult()
- {
- ReplyList.CollectionChanged += ReplyList_CollectionChanged;
- }
-
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- private ObservableCollection<AnnotationBindData> annotationList = new ObservableCollection<AnnotationBindData>();
- public event EventHandler<object> AnnotationSelectionChanged;
- public event EventHandler<Dictionary<int, List<int>>> DeleteItemHandler;
- private ContextMenu popContextMenu;
- private bool enableSelectEvent = true;
- public CPDFAnnotationListUI()
- {
- InitializeComponent();
- ICollectionView groupView = CollectionViewSource.GetDefaultView(annotationList);
- groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationBindData.ShowPageIndex)));
- AnnotationList.PreviewMouseRightButtonUp += ((sender, args) => { SetContextMenu(); });
- }
- private void SetContextMenu()
- {
- if (AnnotationList.SelectedIndex == -1)
- {
- return;
- }
- popContextMenu = new ContextMenu();
- AnnotationBindData data = annotationList[AnnotationList.SelectedIndex];
- MenuItem deleteMenu = new MenuItem();
- deleteMenu.Header = LanguageHelper.BotaManager.GetString("Menu_Delete");
- deleteMenu.Click -= DeleteMenu_Click;
- deleteMenu.Click += DeleteMenu_Click;
- popContextMenu.Items.Add(deleteMenu);
- MenuItem deleteAllMenu = new MenuItem();
- deleteAllMenu.Header = LanguageHelper.BotaManager.GetString("Menu_DeleteAll");
- deleteAllMenu.Click -= DeleteAllMenu_Click;
- deleteAllMenu.Click += DeleteAllMenu_Click;
- popContextMenu.Items.Add(deleteAllMenu);
- MenuItem replyMenu = new MenuItem();
- replyMenu.Header = LanguageHelper.BotaManager.GetString("Menu_AddReply");
- replyMenu.Click += (sender, e) =>
- {
- if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
- {
- data.IsReplyInputVisible = true;
- }
- };
- popContextMenu.Items.Add(replyMenu);
- MenuItem showReplyMenu = new MenuItem();
- if (data.IsReplyListVisible)
- {
- showReplyMenu.Header = LanguageHelper.BotaManager.GetString("Menu_FoldReply");
- }
- else
- {
- showReplyMenu.Header = LanguageHelper.BotaManager.GetString("Menu_ExpandReply");
- }
- showReplyMenu.Click += (sender, e) =>
- {
- if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
- {
- data.IsReplyListVisible = !data.IsReplyListVisible;
- }
- };
- popContextMenu.Items.Add(showReplyMenu);
- AnnotationList.ContextMenu = popContextMenu;
- }
- public void DeleteAllAnnot()
- {
- try
- {
- Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
- foreach (AnnotationBindData bindData in annotationList)
- {
- if (delDict.ContainsKey(bindData.BindProperty.PageIndex) == false)
- {
- delDict[bindData.BindProperty.PageIndex] = new List<int>();
- }
- delDict[bindData.BindProperty.PageIndex].Add(bindData.BindProperty.AnnotIndex);
- }
- if (delDict.Count > 0)
- {
- DeleteItemHandler?.Invoke(this, delDict);
- }
- }
- catch (Exception ex)
- {
- return;
- }
- }
- public void DeleteAllReply()
- {
- try
- {
- foreach (var data in annotationList)
- {
- foreach (var replyData in data.BindProperty.ReplyList)
- {
- replyData.ReplyAnnotation.RemoveAnnot();
- }
- }
- }
- catch (Exception ex)
- {
- return;
- }
- }
- public void ExpandAllReply(bool isExpand)
- {
- foreach (AnnotationBindData data in annotationList)
- {
- data.IsReplyListVisible = isExpand;
- }
- }
- public void ExpandAnnotList(bool isExpand)
- {
- foreach (AnnotationBindData data in annotationList)
- {
- if (isExpand)
- data.BindProperty.IsAnnotListVisible = Visibility.Visible;
- else
- data.BindProperty.IsAnnotListVisible = Visibility.Collapsed;
- }
- }
- private void DeleteAllMenu_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
- foreach (AnnotationBindData bindData in annotationList)
- {
- if (delDict.ContainsKey(bindData.BindProperty.PageIndex) == false)
- {
- delDict[bindData.BindProperty.PageIndex] = new List<int>();
- }
- delDict[bindData.BindProperty.PageIndex].Add(bindData.BindProperty.AnnotIndex);
- }
- if (delDict.Count > 0)
- {
- DeleteItemHandler?.Invoke(this, delDict);
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void DeleteMenu_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (AnnotationList != null && AnnotationList.SelectedIndex >= 0)
- {
- AnnotationBindData bindData = annotationList[AnnotationList.SelectedIndex];
- Dictionary<int, List<int>> delDict = new Dictionary<int, List<int>>();
- delDict[bindData.BindProperty.PageIndex] = new List<int>()
- {
- bindData.BindProperty.AnnotIndex
- };
- DeleteItemHandler?.Invoke(this, delDict);
- }
- }
- catch (Exception ex)
- {
- }
- }
- public void SetAnnotationList(List<BindAnnotationResult> results)
- {
- annotationList.Clear();
- AnnotationList.ContextMenu = null;
- if (results == null || results.Count == 0)
- {
- AnnotationList.ItemsSource = null;
- NoContentText.Visibility = Visibility.Visible;
- return;
- }
- foreach (BindAnnotationResult item in results)
- {
- annotationList.Add(new AnnotationBindData()
- {
- BindProperty = item
- });
- }
- AnnotationList.ItemsSource = annotationList;
- if (annotationList.Count > 0)
- {
- AnnotationList.ContextMenu = popContextMenu;
- }
- AnnotationList.Visibility = Visibility.Visible;
- NoContentText.Visibility = Visibility.Collapsed;
- }
- private void AnnotationListControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (enableSelectEvent)
- {
- try
- {
- if (e.AddedItems[0] is AnnotationBindData annotationBindData)
- {
- AnnotationSelectionChanged?.Invoke(this, (annotationBindData).BindProperty);
- }
- }
- catch { }
- }
- }
- public void CancelSelected()
- {
- AnnotationList.SelectedIndex = -1;
- }
- public void SelectAnnotationChanged(int annotationIndex = -1)
- {
- AnnotationList.SelectedIndex = annotationIndex;
- }
- public void SelectAnnotationChanged(int pageIIndex, int annotIndex)
- {
- if (annotationList != null && annotationList.Count > 0)
- {
- for (int i = 0; i < annotationList.Count; i++)
- {
- AnnotationBindData data = annotationList[i];
- if (data.BindProperty.PageIndex == pageIIndex && data.BindProperty.AnnotIndex == annotIndex)
- {
- enableSelectEvent = false;
- AnnotationList.SelectedIndex = i;
- enableSelectEvent = true;
- break;
- }
- }
- }
- }
- private void AnnotationList_ContextMenuOpening(object sender, ContextMenuEventArgs e)
- {
- try
- {
- if (popContextMenu.Items[0] is MenuItem checkMenu)
- {
- if (checkMenu != null)
- {
- checkMenu.IsEnabled = true;
- }
- if (AnnotationList != null && AnnotationList.SelectedIndex == -1)
- {
- checkMenu.IsEnabled = false;
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void AnnotationList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- CancelSelected();
- }
- private void ToggleButton_Checked(object sender, RoutedEventArgs e)
- {
- ReplyStatusChanged?.Invoke(sender, CPDFAnnotationState.C_ANNOTATION_MARKED);
- }
- private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
- {
- ReplyStatusChanged?.Invoke(sender, CPDFAnnotationState.C_ANNOTATION_UNMARKED);
- }
- private void StatusControl_ReplyStatusChanged(object sender, CPDFAnnotationState e)
- {
- ReplyStatusChanged?.Invoke(sender, e);
- }
- }
- }
|