Browse Source

标记密文-调整标记密文擦除逻辑

ZhouJieSheng 2 years ago
parent
commit
36ad34a8be

+ 109 - 12
PDF Office/ViewModels/EditTools/Redaction/RedactionContentViewModel.cs

@@ -46,11 +46,11 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
 
         public DelegateCommand CloseEditToolCommand { get; set; }
 
-        public DelegateCommand ApplyCommmand { get; set; }
+        public DelegateCommand<string> ApplyCommmand { get; set; }
 
         public DelegateCommand PageRedactionCommand { get; set; }
 
-        public DelegateCommand EraseCommand { get; set; }
+        public DelegateCommand<string> EraseCommand { get; set; }
 
         public RedactionCommandEventArgs TempArgs { get; set; }
 
@@ -110,8 +110,8 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
             RedactionDocumentRegionName = Guid.NewGuid().ToString();
             Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
             CloseEditToolCommand = new DelegateCommand(CloseEditTool);
-            ApplyCommmand = new DelegateCommand(apply,CanExcute).ObservesProperty(() => IsEnable);
-            EraseCommand = new DelegateCommand(erase, CanExcute).ObservesProperty(() => IsEnable);
+            ApplyCommmand = new DelegateCommand<string>(apply,CanExcute).ObservesProperty(() => IsEnable);
+            EraseCommand = new DelegateCommand<string>(erase, CanExcute).ObservesProperty(() => IsEnable);
             PageRedactionCommand = new DelegateCommand(pageMark);
 
             eventAggregator.GetEvent<RedactionCommandEvent>().Subscribe(SetContextMenu, e => e.UniCode == Unicode);
@@ -121,6 +121,7 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
         {
             var contextmenu = App.Current.FindResource("RedactionContextMenu") as ContextMenu;
             obj.args.PopupMenu = contextmenu;
+            TempArgs = obj;
             for(int i=0;i<contextmenu.Items.Count;i++)
             {
                 if(contextmenu.Items[i] is MenuItem) 
@@ -146,9 +147,11 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
                             break;
                         case "MenuApply":
                             item.Command = this.ApplyCommmand;
+                            item.CommandParameter = "Single";
                             break;
                         case "MenuErase":
                             item.Command = this.EraseCommand;
+                            item.CommandParameter = "Single";
                             break;
                         default:
                             break;
@@ -203,22 +206,31 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
         /// <summary>
         /// 应用
         /// </summary>
-        private void apply()
+        private void apply(string type)
         {
             AlertsMessage alertsMessage = new AlertsMessage();
-            alertsMessage.ShowDialog("Apply Redactions", "Use blank blocks to remove selected sensitive content."+
+            alertsMessage.ShowDialog("Apply Redactions", "Use blank blocks to remove selected sensitive content." +
 
-"This action will permanently delete the marked ciphertext information from this document and you will not be able to retrieve the marked ciphertext information.","Cancel","Apply & Save As");
-            if(alertsMessage.result== ContentResult.Ok)
+"This action will permanently delete the marked ciphertext information from this document and you will not be able to retrieve the marked ciphertext information.", "Cancel", "Apply & Save As");
+            if (alertsMessage.result == ContentResult.Ok)
             {
-                viewContentViewModel.saveAsFile(true);
+                if (!string.IsNullOrEmpty(type) && type == "All")
+                {
+                    viewContentViewModel.saveAsFile(applyAll);
+                }
+                else
+                {
+                    viewContentViewModel.saveAsFile(applySingle);
+                }
             }
         }
 
+
+
         /// <summary>
         /// 擦除
         /// </summary>
-        private void erase()
+        private void erase(string type)
         {
             AlertsMessage alertsMessage = new AlertsMessage();
             alertsMessage.ShowDialog("","Replace selected sensitive content with color blocks."+
@@ -226,11 +238,96 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
 "This action will permanently delete the marked ciphertext information from this document and you will not be able to retrieve the marked ciphertext information.", "Cancel", "Apply & Save As");
             if (alertsMessage.result == ContentResult.Ok)
             {
-                viewContentViewModel.saveAsFile(false,true);
+                if (!string.IsNullOrEmpty(type) && type == "All")
+                {
+                    viewContentViewModel.saveAsFile(eraseAll);
+                }
+                else
+                {
+                    viewContentViewModel.saveAsFile(eraseSingle);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 应用所有密文块
+        /// </summary>
+        private void applyAll()
+        {
+            PDFViewer.Document.ApplyRedaction();
+            PDFViewer.Document.ReleasePages();
+            PDFViewer.ReloadDocument();
+        }
+
+
+        /// <summary>
+        /// 擦除所有密文块
+        /// </summary>
+        private void eraseAll()
+        {
+            for (int i = 0; i < PDFViewer.Document.PageCount; i++)
+            {
+                //获取页面Page对象
+                var page = PDFViewer.Document.PageAtIndex(i);
+                var annots = PDFViewer.GetAnnotCommentList(i, PDFViewer.Document);
+                //循环擦除当前页的标记密文
+                for (int j = 0; j < annots.Count; j++)
+                {
+                    if (annots[j].EventType == AnnotArgsType.AnnotRedaction)
+                    {
+                        foreach (var rect in (annots[j] as RedactionAnnotArgs).QuardRects)
+                        {
+                            page.ErasureRedaction(rect);
+                        }
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// 应用选中的密文块
+        /// </summary>
+        private void applySingle()
+        {
+            AnnotCommandArgs annotCommand =TempArgs.args;
+            if (annotCommand != null)
+            {
+                List<RedactionAnnotArgs> redactionList = new List<RedactionAnnotArgs>();
+                foreach (AnnotHandlerEventArgs annotArgs in annotCommand.AnnotEventArgsList)
+                {
+                    if (annotArgs.EventType == AnnotArgsType.AnnotRedaction)
+                    {
+                        redactionList.Add((RedactionAnnotArgs)annotArgs);
+                    }
+                }
+                PDFViewer.ApplyRedaction(redactionList);
+            }
+        }
+
+        /// <summary>
+        /// 擦除选中的密文块
+        /// </summary>
+        private void eraseSingle()
+        {
+            AnnotCommandArgs annotCommand = TempArgs.args;
+            if (annotCommand != null)
+            {
+                List<RedactionAnnotArgs> redactionList = new List<RedactionAnnotArgs>();
+                foreach (AnnotHandlerEventArgs annotArgs in annotCommand.AnnotEventArgsList)
+                {
+                    if (annotArgs.EventType == AnnotArgsType.AnnotRedaction)
+                    {
+                        foreach (var rect in (annotArgs as RedactionAnnotArgs).QuardRects)
+                        {
+                            var page = PDFViewer.Document.PageAtIndex((annotArgs as RedactionAnnotArgs).PageIndex);
+                            page.ErasureRedaction(rect);
+                        }
+                    }
+                }
             }
         }
 
-        private bool CanExcute()
+        private bool CanExcute(string type)
         {
             return IsEnable;
         }

+ 25 - 6
PDF Office/ViewModels/MainWindowViewModel.cs

@@ -222,17 +222,24 @@ namespace PDF_Office.ViewModels
 
         public void CloseTabItem(object item)
         {
-            if (pdfViewer != null && pdfViewer.UndoManager.CanSave)
+            //获取x号所在的maincontentviewmodel对象
+            var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
+            //如果已经有打开文档,先切换到目标页签
+            if (maincontentViewModel.PDFViewer != null)
             {
-                ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
+                SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
+            }
+            if (maincontentViewModel.PDFViewer != null &&maincontentViewModel.PDFViewer.UndoManager.CanSave)
+            {
+                ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
                 if (result == ContentResult.Cancel)
                 {
                     return;
                 }
-                App.OpenedFileList.Remove(pdfViewer.Document.FilePath);
-                if (pdfViewer.Document != null)
+                App.OpenedFileList.Remove(maincontentViewModel.PDFViewer.Document.FilePath);
+                if (maincontentViewModel.PDFViewer.Document != null)
                 {
-                    pdfViewer.CloseDocument();
+                    maincontentViewModel.PDFViewer.CloseDocument();
                 }
 
             }
@@ -243,7 +250,19 @@ namespace PDF_Office.ViewModels
             }
             else
             {
-                App.Current.MainWindow.Close();
+           
+                if (maincontentViewModel.PDFViewer != null)
+                {
+                    //如果是打开文档的页签 则回到首页
+                    region.Regions[RegionNames.MainRegion].Remove(item);
+                    region.RequestNavigate(RegionNames.MainRegion, "MainContent");
+                }
+                else
+                {
+                    //如果是关闭首页则关闭软件
+                    region.Regions[RegionNames.MainRegion].Remove(item);
+                    App.Current.MainWindow.Close();
+                }
             }
             return;
 

+ 2 - 0
PDF Office/Views/EditTools/Redaction/RedactionContent.xaml

@@ -81,6 +81,7 @@
                     Margin="8,0"
                     Padding="8,0"
                     Command="{Binding ApplyCommmand}"
+                    CommandParameter="All"
                     Content="应用密文"
                     Style="{StaticResource Btn.cta}" />
                 <Button
@@ -88,6 +89,7 @@
                     Margin="8,0"
                     Padding="8,0"
                     Command="{Binding EraseCommand}"
+                    CommandParameter="All"
                     Content="擦除密文"
                     Style="{StaticResource Btn.cta}" />
                 <Button

+ 1 - 1
PDF Office/Views/MainWindow.xaml

@@ -140,7 +140,7 @@
                                             CommandParameter="{Binding}"
                                             Header="Close Tab" />
                                         <MenuItem
-                                            Command="{Binding DataContext.mainWindowViewModel.CloseAllCommand}"
+                                            Command="{Binding DataContext.mainWindowViewModel.CloseAllTabCommand}"
                                             Header="Close All Tab"
                                             IsEnabled="{Binding DataContext.mainWindowViewModel.IsCloseAllEnable}" />
                                         <MenuItem Header="Open In New Window" Visibility="Collapsed" />