using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Word; using Microsoft.Win32; using PDF_Office.CustomControl; using PDF_Office.EventAggregators; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Properties; using PDF_Office.ViewModels.BOTA; using PDF_Office.ViewModels.PropertyPanel; using PDF_Office.ViewModels.PropertyPanel.AnnotPanel; using PDF_Office.Views.BOTA; using PDF_Office.Views.PropertyPanel.AnnotPanel; using PDFSettings; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using ContextMenu = System.Windows.Controls.ContextMenu; using MenuItem = System.Windows.Controls.MenuItem; namespace PDF_Office.ViewModels.Tools { public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware { #region 初始化 public AnnotToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService) { region = regionManager; events = eventAggregator; dialogs = dialogService; InitCommand(); BindingEvent(); InitDefaultValue(); InitToolDict(); } private void InitCommand() { MyToolsCommand = new DelegateCommand(BtnMyTools_Click); SetAddAnnotationCommand = new DelegateCommand(AddAnnotation_Click); PropertyRegionName = Guid.NewGuid().ToString(); } #endregion 初始化 #region Command实现 //点击注释工具 private void BtnMyTools_Click(CustomIconToggleBtn annotBtn) { //不创建注释,属于注释模板 bool isTemplateAnnot = false; bool isSnapshotEdit = false; AnnotHandlerEventArgs annotArgs = null; if (annotBtn.IsChecked == true) { var tag = annotBtn.Tag.ToString(); FindAnnotTypeKey(tag, ref annotArgs); if (tag == "SnapshotEdit") { isSnapshotEdit = true; } else if (tag == "Signature" || tag == "Stamp") { isTemplateAnnot = true; } if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); isRightMenuAddAnnot = false; } } //当不是注释模板,且无创建注释时,属性面板显示为空内容 if (isTemplateAnnot == false && annotArgs == null) { PDFViewer.SetMouseMode(MouseModes.PanTool); viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } else { if (isSnapshotEdit == true) { ShowPropertyPanel(false); isSnapshotEdit = false; } else { ShowPropertyPanel((bool)annotBtn.IsChecked); } } } /// /// 右键菜单-添加注释 /// /// private void AddAnnotation_Click(object sender) { if (sender is MenuItem menuItem) { AnnotHandlerEventArgs annotHandler = null; string str = menuItem.Tag.ToString(); if (str == AddAnnotType.AnnotFreehand.ToString()) { annotHandler = GetFreehand(); } if (str == AddAnnotType.AnnotFreeText.ToString()) { annotHandler = GetFreetext(); } if (str == AddAnnotType.AnnotSticky.ToString()) { annotHandler = GetStickyNote(); } if (str == AddAnnotType.AnnotSquare.ToString()) { annotHandler = GetRect(); } if (str == AddAnnotType.AnnotCircle.ToString()) { annotHandler = GetCircle(); } if (str == AddAnnotType.AnnotArrow.ToString()) { annotHandler = GetArrowLine("Arrow"); } if (str == AddAnnotType.AnnotLine.ToString()) { annotHandler = GetArrowLine("Line"); } if (str == AddAnnotType.AnnotLink.ToString()) { annotHandler = GetLink(); } if (str == AddAnnotType.AnnotStamp.ToString()) { annotHandler = GetStamp(); } if (str == AddAnnotType.AnnotAutograph.ToString()) { annotHandler = GetSignature(); } if (annotHandler != null) { annotHandler.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotHandler); isRightMenuAddAnnot = true; ShowPropertyPanel(false); } } } #endregion Command实现 #region BOTA private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel) { AnnotationContentViewModel viewModel = null; if (bOTAContentViewModel != null) { if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName)) { var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views; foreach (var item in views) { if (item is AnnotationContent annotation) { viewModel = annotation.DataContext as AnnotationContentViewModel; break; } } } } return viewModel; } /// /// 判断Bota的TabItem是否已显示 /// /// /// /// /// private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText) { bool isTabItem = false; bOTAContentViewModel = null; bOTAContent = null; if (region.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName)) { var views = region.Regions[viewContentViewModel.BOTARegionName].Views; var model = views.FirstOrDefault(q => q is BOTAContent); if (model is BOTAContent bOTAContent1) { bOTAContent = bOTAContent1; bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel; if (bOTAContentViewModel.CurrentBar == tabItemText) { isTabItem = true; } } //foreach (var item in views) //{ // if (item is BOTAContent bOTAContent1) // { // bOTAContent = bOTAContent1; // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel; // if (bOTAContentViewModel.CurrentBar == tabItemText) // { // isTabItem = true; // break; // } // } //} } return isTabItem; } private void Select_Click(object sender, RoutedEventArgs e) { if (viewContentViewModel.OpenBOTA == false) { viewContentViewModel.OpenBOTA = true; } bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch"); if (isBook == false) { bOTAContent.TabItemSearch.IsSelected = true; } } #endregion BOTA #region 阅读页右键菜单 private void PageDisplay(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { menuItem.Click -= PageDisplay_Click; menuItem.Click += PageDisplay_Click; } } } private void PageDisplay_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { switch (menuItem.Tag.ToString()) { case "SingleView": PDFViewer.ChangeViewMode(ViewMode.Single); break; case "SingleContinueView": PDFViewer.ChangeViewMode(ViewMode.SingleContinuous); break; case "DoubleView": PDFViewer.ChangeViewMode(ViewMode.Double); break; case "DoubleContinueView": PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous); break; case "BookMode": PDFViewer.ChangeViewMode(ViewMode.Book); break; } } } private void ViewZoom(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { menuItem.Click -= PageDisplay_Click; menuItem.Click += ViewZoom_Click; } } } private void ViewZoom_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { switch (menuItem.Tag.ToString()) { case "FitWidth": PDFViewer.ChangeFitMode(FitMode.FitWidth); break; case "FitHeight": PDFViewer.ChangeFitMode(FitMode.FitHeight); break; case "FitSize": PDFViewer.ChangeFitMode(FitMode.FitSize); break; case "enlarge": double newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + 0.01), true); PDFViewer.Zoom(newZoom); break; case "narrow": newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + -0.01), false); PDFViewer.Zoom(newZoom); break; } } } private void UnReadModel_Click(object sender, RoutedEventArgs e) { viewContentViewModel.UnReadModel(); } private void ReadModel_Click(object sender, RoutedEventArgs e) { viewContentViewModel.RbtnReadMode(); } private void SetToolMode(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { menuItem.Click -= PageDisplay_Click; menuItem.Click += ToolMode_Click; } } } private void ToolMode_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { switch (menuItem.Tag.ToString()) { case "default": PDFViewer.SetMouseMode(MouseModes.None); //viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); BtnHandIsChecked = true; ShowPropertyPanel(false); break; case "roll": PDFViewer.SetMouseMode(MouseModes.PanTool); //viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); BtnHandIsChecked = true; ShowPropertyPanel(false); break; case "content": AnnotHandlerEventArgs annotHandler = GetSnapshotEdit(); BtnSelecttoolIsChecked = true; if (annotHandler != null) { annotHandler.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotHandler); ShowPropertyPanel(false); } break; default: break; } } } private void BookMark(MenuItem addBookMark, MenuItem delBookMark) { int index = PDFViewer.CurrentIndex; //检测是否已存在相同数据 CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index); if (list != null) { isAddBookMark = false; } else { isAddBookMark = true; } if (isAddBookMark) { addBookMark.Visibility = Visibility.Visible; delBookMark.Visibility = Visibility.Collapsed; addBookMark.Click += AddBookMark_Click; } else { delBookMark.Visibility = Visibility.Visible; addBookMark.Visibility = Visibility.Collapsed; delBookMark.Click += DelBookMark_Click; } } private void DelBookMark_Click(object sender, RoutedEventArgs e) { if (viewContentViewModel.OpenBOTA == false) { viewContentViewModel.OpenBOTA = true; } bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); if (isBook == false) { bOTAContent.TabItemBookMark.IsSelected = true; } BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentViewModel, out BookmarkContent bookmark); int index = PDFViewer.CurrentIndex; //检测是否已存在相同数据 CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index); if (bookmarkContentViewModel != null && bookmark != null && bOTAContent != null && list != null) { bookmarkContentViewModel.DeleteCommand.Execute(list); isAddBookMark = bookmarkContentViewModel.isAddBookMark; } // bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); //if (isBook == false || viewContentViewModel.OpenBOTA == false) //{ // viewContentViewModel.OpenBOTA = true; // bOTAContent.TabItemBookMark.IsSelected = true; //} } private void AddBookMark_Click(object sender, RoutedEventArgs e) { if (viewContentViewModel.OpenBOTA == false) { viewContentViewModel.OpenBOTA = true; } bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); if (isBook == false) { bOTAContent.TabItemBookMark.IsSelected = true; } BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentViewModel, out BookmarkContent bookmark); if (bookmarkContentViewModel != null && bookmark != null && bOTAContent != null) { bookmarkContentViewModel.AddBookmarkCommand.Execute(bookmark.BookMarkListView); isAddBookMark = bookmarkContentViewModel.isAddBookMark; return; } } private BookmarkContentViewModel GetBookmarkContentViewModel(BOTAContentViewModel bOTAContentViewModel, out BookmarkContent bookmark) { BookmarkContentViewModel viewModel = null; bookmark = null; if (bOTAContentViewModel != null) { if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName)) { var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views; foreach (var item in views) { if (item is BookmarkContent bookmark1) { bookmark = bookmark1; viewModel = bookmark1.DataContext as BookmarkContentViewModel; break; } } } } return viewModel; } private OutLineControlViewModel GetOutLineControlViewModel(BOTAContentViewModel bOTAContentViewModel, out OutLineControl outLineControl) { OutLineControlViewModel viewModel = null; outLineControl = null; if (bOTAContentViewModel != null) { if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName)) { var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views; foreach (var item in views) { if (item is OutLineControl outLineControl1) { outLineControl = outLineControl1; viewModel = outLineControl1.DataContext as OutLineControlViewModel; break; } } } } return viewModel; } private void DisplayAnnot_Click(object sender, RoutedEventArgs e) { PDFViewer.SetDrawAnnot(true); isHiddenAnnot = true; } private void HiddenAnnot_Click(object sender, RoutedEventArgs e) { PDFViewer.SetDrawAnnot(false); isHiddenAnnot = false; } private void SetAddAnnotation(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { menuItem.CommandParameter = item; menuItem.Command = SetAddAnnotationCommand; } } } private void AddLink_Click(object sender, RoutedEventArgs e) { var args = PDFViewer.ToolManager.CurrentAnnotArgs; BtnLinkIsChecked = true; } #endregion 阅读页右键菜单 #region Navigation public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { UnBindingPDFViewerHandler(); UnBindingEvent(); } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); BindingPDFViewerHandler(); } #endregion Navigation } }