123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using Microsoft.Win32;
- using System.Windows.Controls.Primitives;
- using ComPDFKitViewer.PdfViewer;
- using ComPDFKitViewer.AnnotEvent;
- namespace ComPDFKitDemo.BOTA
- {
- public partial class AnnotationsControl : UserControl
- {
- public CPDFViewer pdfViewer;
- private bool selectChangeEnable = true;
- public AnnotationsControl()
- {
- InitializeComponent();
- ContextMenu popMenu = new ContextMenu();
- MenuItem copyItem = new MenuItem();
- copyItem.Header = "Copy";
- copyItem.Click += CopyItem_Click;
- popMenu.Items.Add(copyItem);
- MenuItem delItem = new MenuItem();
- delItem.Header = "Delete";
- delItem.Click += DelItem_Click;
- popMenu.Items.Add(delItem);
- this.ContextMenu = popMenu;
- }
- private void DelItem_Click(object sender, RoutedEventArgs e)
- {
- if (AnnotListBox.SelectedItem != null && pdfViewer != null)
- {
- AnnotCommentControl AnnotControl = (AnnotCommentControl)AnnotListBox.SelectedItem;
- AnnotHandlerEventArgs comment = AnnotControl.GetBindData();
- if (comment != null)
- {
- pdfViewer.RemovePageAnnot(comment.PageIndex, comment.AnnotIndex);
- }
- AnnotListBox.Items.Remove(AnnotControl);
- }
- }
- private void CopyItem_Click(object sender, RoutedEventArgs e)
- {
- if (AnnotListBox.SelectedItem != null && pdfViewer != null)
- {
- if(!pdfViewer.ExecCopyCommand())
- {
- Debug.WriteLine("Copy Faild");
- }
- }
- }
- private void ExportAnnots_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton exportBtn = sender as ToggleButton;
- if (exportBtn != null)
- {
- exportBtn.IsEnabled = false;
- SaveFileDialog dlg = new SaveFileDialog();
- dlg.Filter = "PDF|*.xfdf";
- dlg.DefaultExt = ".xfdf";
- dlg.FileName = pdfViewer.Document.FileName;
- if (dlg.ShowDialog() == true)
- {
- string fileName = dlg.FileName;
- }
- exportBtn.IsEnabled = true;
- }
- }
- private void ImportAnnots_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton importBtn = sender as ToggleButton;
- if (importBtn != null)
- {
- importBtn.IsEnabled = false;
- OpenFileDialog dlg = new OpenFileDialog();
- dlg.Filter = "PDF|*.xfdf";
- dlg.DefaultExt = ".xfdf";
- dlg.FileName = pdfViewer.Document.FileName;
- if (dlg.ShowDialog() == true)
- {
- }
- importBtn.IsEnabled = true;
- }
- }
- public void LoadAnnots()
- {
- AnnotListBox.Items.Clear();
- if (pdfViewer != null && pdfViewer.Document != null)
- {
- int pageCount = pdfViewer.Document.PageCount;
- Task job = new Task(() =>
- {
- for (int i = 0; i < pageCount; i++)
- {
- List<AnnotHandlerEventArgs> commentsList = pdfViewer.GetAnnotCommentList(i, pdfViewer.Document);
- if (commentsList != null)
- {
- Dispatcher.Invoke(() =>
- {
- foreach (AnnotHandlerEventArgs comment in commentsList)
- {
- AnnotCommentControl AnnotControl = new AnnotCommentControl();
- AnnotControl.BindData(comment);
- AnnotListBox.Items.Add(AnnotControl);
- }
- });
- }
- }
- });
- job.Start();
- }
- }
- private void AnnotListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if(selectChangeEnable)
- {
- if (AnnotListBox.SelectedItems.Count > 0 && pdfViewer != null)
- {
- Dictionary<int, List<int>> selectAnnotsDict = new Dictionary<int, List<int>>();
- foreach (AnnotCommentControl AnnotControl in AnnotListBox.SelectedItems)
- {
- AnnotHandlerEventArgs comment = AnnotControl.GetBindData();
- if (comment != null)
- {
- if (selectAnnotsDict.ContainsKey(comment.PageIndex) == false)
- {
- selectAnnotsDict[comment.PageIndex] = new List<int>();
- }
- selectAnnotsDict[comment.PageIndex].Add(comment.AnnotIndex);
- }
- }
- pdfViewer.SelectAnnotation(selectAnnotsDict);
- }
- }
-
- AnnotListBox.ScrollIntoView(AnnotListBox.SelectedItem);
- }
- public void SetSelectAnnot(Dictionary<int, List<int>> selectAnnotDicts)
- {
- int selectIndex = -1;
- selectChangeEnable = false;
- AnnotListBox.SelectedItems.Clear();
- foreach (AnnotCommentControl Item in AnnotListBox.Items)
- {
- AnnotHandlerEventArgs commentData = Item.GetBindData();
- selectIndex++;
- if(selectAnnotDicts.ContainsKey(commentData.PageIndex) && selectAnnotDicts[commentData.PageIndex].Contains(commentData.AnnotIndex))
- {
- AnnotListBox.SelectedItems.Add(Item);
- }
- }
- selectChangeEnable = true;
- }
- }
- }
|