소스 검색

标记密文-完善标记密文逻辑

ZhouJieSheng 2 년 전
부모
커밋
2f9a1ad838

+ 1 - 5
PDF Office/Styles/ContextMenuStyle.xaml

@@ -159,11 +159,7 @@
         </MenuItem>
     </ContextMenu>
     <ContextMenu x:Key="RedactionContextMenu">
-        <MenuItem
-            Name="MenuDelete"
-            Command="{x:Static ApplicationCommands.Delete}"
-            CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"
-            Header="Delete" />
+        <MenuItem Name="MenuDelete" Header="Delete" />
         <Separator Style="{StaticResource HorizontalSeparatorStyle}" />
         <MenuItem Name="MenuProperties" Header="Properties..." />
         <MenuItem Name="MenuSetDeufalt" Header="Use Current Properties as New Default" />

+ 96 - 18
PDF Office/ViewModels/Dialog/Redaction/MarkSettingDialogViewModel.cs

@@ -138,6 +138,23 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
             set
             {
                 SetProperty(ref fontFamilySelectedIndex, value);
+                if(value>0)
+                {
+                    switch (value)
+                    {
+                        case 0:
+                            fontFamily = "Courier";
+                            break;
+                        case 1:
+                            fontFamily = "Helvetica";
+                            break;
+                        case 2:
+                            fontFamily = "Times Roman";
+                            break;
+                        default:
+                            break;
+                    }
+                }
             }
         }
 
@@ -151,11 +168,33 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
             set
             {
                 SetProperty(ref fontWeightSelectedIndex, value);
+                if (value > 0)
+                {
+                    switch (value)
+                    {
+                        case 0:
+                            fontWeight = FontWeights.Regular;
+                            break;
+                        case 1:
+                            fontWeight = FontWeights.Bold;
+                            break;
+                        case 2:
+                            fontWeight = FontWeights.SemiBold;
+                            break;
+                        case 3:
+                            fontWeight = FontWeights.UltraBold;
+                            break;
+                        default:
+                            break;
+                    }
+                }
             }
         }
 
         private FontWeight fontWeight = FontWeights.Regular;
 
+        private int fontSize = 12;
+
         private int fontSizeSelectedIndex;
 
         public int FontSizeSelectedIndex
@@ -173,6 +212,8 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
 
         public List<string> FontSizes { get; set; }
 
+        public TextAlignment textAlignment { get; set; } = TextAlignment.Left;
+
 
 
         public MarkSettingDialogViewModel()
@@ -188,6 +229,7 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
 
         private void InitFontFamily()
         {
+            FontFamilys = new List<string>();
             FontFamilys.Add("Courier New");
             FontFamilys.Add("Arial");
             FontFamilys.Add("Times New Roman");
@@ -195,6 +237,7 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
 
         private void InitFontWeight()
         {
+            FontWeight = new List<string>();
             FontWeight.Add("Regular");
             FontWeight.Add("Bold");
             FontWeight.Add("Italic");
@@ -203,6 +246,7 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
 
         private void InitFontSize()
         {
+            FontSizes = new List<string>();
             FontSizes.Add("Auto");
         }
 
@@ -213,26 +257,44 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
 
         private void ok()
         {
-            //annotArgs.UpdateAttrib(AnnotAttrib.FillColor, frm.SaveredactionArgs.BgColor);
-            //annotArgs.UpdateAttrib(AnnotAttrib.NoteText, frm.SaveredactionArgs.Content);
-            //annotArgs.UpdateAttrib(AnnotAttrib.FontColor, frm.SaveredactionArgs.FontColor);
+            AnnotAttribEvent eventargs = AnnotAttribEvent.GetAnnotAttribEvent(annotArgs, annotArgs.GetAnnotAttrib());
+            eventargs.UpdateAttrib(AnnotAttrib.FillColor,BgColor);
+            eventargs.UpdateAttrib(AnnotAttrib.NoteText, OvertText);
+            eventargs.UpdateAttrib(AnnotAttrib.FontColor,FontColor);
 
-            //annotArgs.UpdateAttrib(AnnotAttrib.Color, frm.SaveredactionArgs.LineColor);
+            eventargs.UpdateAttrib(AnnotAttrib.Color, LineColor);
 
-            //annotArgs.UpdateAttrib(AnnotAttrib.FontSize, frm.SaveredactionArgs.FontSize);
+            eventargs.UpdateAttrib(AnnotAttrib.FontSize,fontSize);
 
-            //annotArgs.UpdateAttrib(AnnotAttrib.TextAlign, frm.SaveredactionArgs.Align);
-            //if (IsUseText)
-            //{
-            //    annotArgs.UpdateAttrib(AnnotAttrib.NoteText, string.Empty);
-            //}
-            //annotArgs.UpdateAnnot();
+            eventargs.UpdateAttrib(AnnotAttrib.TextAlign, textAlignment);
+            eventargs.UpdateAttrib(AnnotAttrib.FontWeight, fontWeight);
+            if (IsUseText)
+            {
+                eventargs.UpdateAttrib(AnnotAttrib.NoteText, string.Empty);
+            }
+            eventargs.UpdateAnnot();
             RequestClose.Invoke(new DialogResult(ButtonResult.OK));
         }
 
         private void align(string tag)
         {
-
+            switch (tag)
+            {
+                case "Left":
+                    textAlignment = TextAlignment.Left;
+                    break;
+                case "Center":
+                    textAlignment = TextAlignment.Center;
+                    break;
+                case "Right":
+                    textAlignment = TextAlignment.Right;
+                    break;
+                case "Strech":
+                    textAlignment = TextAlignment.Justify;
+                    break;
+                default:
+                    break;
+            }
         }
 
         public bool CanCloseDialog()
@@ -256,20 +318,36 @@ namespace PDF_Office.ViewModels.Dialog.Redaction
             OvertText = annotArgs.Content;
             switch (annotArgs.FontFamily.ToString())
             {
-                case "":
-                    FontFamilySelectedIndex = 0;
-                    break;
-                case "1":
+
+                case "Helvetica":
                     FontFamilySelectedIndex = 1;
                     break;
-                case "2":
+                case "Times Roman":
                     FontFamilySelectedIndex = 2;
                     break;
                 default:
+                case "Courier":
+                    FontFamilySelectedIndex = 0;
                     break;
             }
 
-
+            switch (annotArgs.Align)
+            {
+                case TextAlignment.Left:
+                    LeftChecked = true;
+                    break;
+                case TextAlignment.Right:
+                    RightChecked = true;
+                    break;
+                case TextAlignment.Center:
+                    CenterChecked = true;
+                    break;
+                case TextAlignment.Justify:
+                    StrechChecked = true;
+                    break;
+                default:
+                    break;
+            }
         }
     }
 }

+ 56 - 3
PDF Office/ViewModels/EditTools/Redaction/RedactionContentViewModel.cs

@@ -85,6 +85,21 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
             }
         }
 
+        private bool isEnable = false; 
+
+        /// <summary>
+        /// 按钮是否启用
+        /// </summary>
+        public bool IsEnable
+        {
+            get { return isEnable; }
+            set
+            {
+                SetProperty(ref isEnable, value);
+            }
+        }
+
+
 
         public RedactionContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService)
         {
@@ -95,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);
-            EraseCommand = new DelegateCommand(erase);
+            ApplyCommmand = new DelegateCommand(apply,CanExcute).ObservesProperty(() => IsEnable);
+            EraseCommand = new DelegateCommand(erase, CanExcute).ObservesProperty(() => IsEnable);
             PageRedactionCommand = new DelegateCommand(pageMark);
 
             eventAggregator.GetEvent<RedactionCommandEvent>().Subscribe(SetContextMenu, e => e.UniCode == Unicode);
@@ -113,6 +128,10 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
                     var item = contextmenu.Items[i] as MenuItem;
                     switch (item.Name)
                     {
+                        case "MenuDelete":
+                            item.Command = ApplicationCommands.Delete;
+                            item.CommandParameter = (UIElement)item;
+                            break;
                         case "MenuSetDeufalt":
                             item.Command = new DelegateCommand<RedactionCommandEventArgs>(SetDefualtProperty);
                             item.CommandParameter = obj;
@@ -149,7 +168,7 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
             DialogParameters keyValues = new DialogParameters();
             keyValues.Add(ParameterNames.DataModel,args.args.AnnotEventArgsList[0] as RedactionAnnotArgs);
 
-            Settings.Default.RedactionsSettings.SetDefualtValue(annoteargs.Content, annoteargs.LineColor,annoteargs.BgColor,annoteargs.FontColor, (int)annoteargs.FontSize,string.IsNullOrEmpty(annoteargs.Content) ? false : true, annoteargs.Align, annoteargs.FontFamily.ToString(),FontWeights.Regular);
+            Settings.Default.RedactionsSettings.SetDefualtValue(annoteargs.Content, annoteargs.LineColor,annoteargs.BgColor,annoteargs.FontColor, (int)annoteargs.FontSize,string.IsNullOrEmpty(annoteargs.Content) ? false : true, annoteargs.Align, annoteargs.FontFamily.ToString(),annoteargs.FontWeight);
         }
 
         /// <summary>
@@ -198,7 +217,19 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
         /// </summary>
         private void erase()
         {
+            AlertsMessage alertsMessage = new AlertsMessage();
+            alertsMessage.ShowDialog("","Replace selected sensitive content with color blocks."+
+
+"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);
+            }
+        }
 
+        private bool CanExcute()
+        {
+            return IsEnable;
         }
 
         private void pageMark()
@@ -354,7 +385,10 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
             App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
             navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
             navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
+            PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
             PDFViewer.InfoChanged += PDFViewer_InfoChanged;
+            PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
+            PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
             CurrentPage = PDFViewer.CurrentIndex + 1;
             PageCount = PDFViewer.Document.PageCount;
             if (!redactionRegion.Regions[RedactionDocumentRegionName].Views.Contains(PDFViewer))
@@ -384,6 +418,25 @@ namespace PDF_Office.ViewModels.EditTools.Redaction
   
         }
 
+        private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
+        {
+            //添加注释后检测是否有未应用的标记密文注释
+            IsEnable = false;
+            for(int i=0;i<PDFViewer.Document.PageCount;i++)
+            {
+                var annotes = PDFViewer.GetAnnotCommentList(i,PDFViewer.Document);
+                for(int j=0;j<annotes.Count;j++)
+                {
+                    if(annotes[j].EventType == AnnotArgsType.AnnotRedaction)
+                    {
+                        IsEnable = true;
+                        return;
+                    }
+                }
+            }
+   
+        }
+
         private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
         {
             isFromSelf = true;

+ 29 - 2
PDF Office/ViewModels/ViewContentViewModel.cs

@@ -1004,7 +1004,7 @@ namespace PDF_Office.ViewModels
         /// <summary>
         /// 另存为或新文档保存逻辑
         /// </summary>
-        public bool saveAsFile(bool isFromRedaction = false)
+        public bool saveAsFile(bool isApplyRedaction = false,bool isEraseRedaction = false)
         {
             var dlg = new SaveFileDialog();
             dlg.Filter = Properties.Resources.OpenDialogFilter;
@@ -1012,7 +1012,8 @@ namespace PDF_Office.ViewModels
             if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
             {
                 bool result = false;
-                if (isFromRedaction)
+                //标记密文后的保存
+                if (isApplyRedaction)
                 {
                     //应用标记密文并另存为保存
                     try
@@ -1023,6 +1024,11 @@ namespace PDF_Office.ViewModels
                     }
                     catch { }
                 }
+                else if(isEraseRedaction)
+                {
+                    EraseReadction();
+                }
+
                 if (App.OpenedFileList.Contains(dlg.FileName))
                 {
                     //提示文件已经被打开
@@ -1047,6 +1053,27 @@ namespace PDF_Office.ViewModels
                 return false;
         }
 
+        private void EraseReadction()
+        {
+            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>
         /// 另存为后进行的操作
         /// 重新打开新文档