Browse Source

compdfkit(win) - 文本复制鉴权

weixiangjie 1 year ago
parent
commit
a26531b91a

+ 1 - 7
Demo/Examples/Compdfkit_Tools/Annotation/AnnotationControl/AnnotationControl.xaml

@@ -81,11 +81,8 @@
                                 5.30443 12.4079 3.40164 10.0607 3.40164H3.37132L5.7123 1.06066L4.65164 0L1.03033 3.62131Z
                         </Path.Data>
                     </Path>
-                    <Button.ToolTip>
-                        <ToolTip Content="Undo"></ToolTip>
-                    </Button.ToolTip>
                 </Button>
-                <Button Style="{StaticResource LightButtonStyle}" BorderThickness="0" Width="40" Height="40" Margin="10,0,0,0"  IsEnabled="{Binding CanRedo,Mode=OneWay}" Click="RedoButton_Click"
+                <Button x:Name="RedoBtn" Style="{StaticResource LightButtonStyle}" BorderThickness="0" Width="40" Height="40" Margin="10,0,0,0"  IsEnabled="{Binding CanRedo,Mode=OneWay}" Click="RedoButton_Click"
                                     Background="Transparent">
                     <Path IsEnabled="{Binding CanRedo,Mode=OneWay}">
                         <Path.Resources>
@@ -106,9 +103,6 @@
                                 7.65164C0.499887 5.30443 2.40268 3.40164 4.74989 3.40164H11.4392L9.09825 1.06066L10.1589 0L13.7802 3.62131Z
                         </Path.Data>
                     </Path>
-                    <Button.ToolTip>
-                        <ToolTip Content="Redo"></ToolTip>
-                    </Button.ToolTip>
                 </Button>
             </StackPanel>
         </Border>

+ 15 - 0
Demo/Examples/Compdfkit_Tools/Annotation/AnnotationControl/AnnotationControl.xaml.cs

@@ -19,6 +19,7 @@ using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
 using Compdfkit_Tools.Helper;
 using ComPDFKit.DigitalSign;
 using ComPDFKit.PDFAnnotation.Form;
+using PasswordBoxPlus.Helper;
 
 namespace Compdfkit_Tools.PDFControl
 {
@@ -98,6 +99,9 @@ namespace Compdfkit_Tools.PDFControl
             AnnotationBarControl.InitAnnotationBar(annotationProperties);
             panelState.PropertyChanged -= PanelState_PropertyChanged;
             panelState.PropertyChanged += PanelState_PropertyChanged;
+
+            UndoBtn.ToolTip = LanguageHelper.CommonManager.GetString("Tooltip_Undo");
+            RedoBtn.ToolTip = LanguageHelper.CommonManager.GetString("Tooltip_Redo");
         }
 
         #region Init PDFViewer
@@ -500,8 +504,19 @@ namespace Compdfkit_Tools.PDFControl
                     }
                     break;
                 case CommandType.Copy:
+                {
+                    if (PDFViewControl == null) return;
+                    if (!PDFViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
+                    {
+                        if(!PasswordHelper.UnlockWithOwnerPassword(PDFViewControl.PDFView.Document))
+                        {
+                            return;
+                        }
+                    }
                     e.DoCommand();
                     break;
+                }
+                    
                 case CommandType.Cut:
                 case CommandType.Paste:
                 case CommandType.Delete:

+ 15 - 0
Demo/Examples/Compdfkit_Tools/Common/Helper/PasswordHelper.cs

@@ -1,5 +1,8 @@
 using System.Windows;
 using System.Windows.Controls;
+using Compdfkit_Tools.Common;
+using Compdfkit_Tools.Helper;
+using ComPDFKit.PDFDocument;
 
 namespace PasswordBoxPlus.Helper
 {
@@ -52,5 +55,17 @@ namespace PasswordBoxPlus.Helper
             SetPassword(passwordBox, passwordBox.Password);
             isUpdating = false;
         }
+        
+        public static bool UnlockWithOwnerPassword(CPDFDocument document)
+        {
+            PasswordWindow window = new PasswordWindow();
+            window.InitWithDocument(document);
+            window.PasswordType = PasswordType.OwnerPassword;
+            if (Application.Current.MainWindow != null) 
+                window.Owner = Window.GetWindow(Application.Current.MainWindow);
+            window.PasswordDialog.SetShowText(document.FileName + " " + LanguageHelper.CommonManager.GetString("Tip_Permission"));
+            window.ShowDialog();
+            return document.GetPermissionsInfo().AllowsCopying || document.GetPermissionsInfo().AllowsPrinting;
+        }
     }
 }

+ 14 - 5
Demo/Examples/Compdfkit_Tools/Common/PasswordControl/PasswordWindow.xaml.cs

@@ -2,6 +2,7 @@
 using System;
 using System.Windows;
 using System.Windows.Input;
+using Compdfkit_Tools.Helper;
 using ComPDFKit.PDFDocument;
 
 namespace Compdfkit_Tools.Common
@@ -60,6 +61,7 @@ namespace Compdfkit_Tools.Common
 
         private void PasswordDialog_Confirmed(object sender, string e)
         {
+            string errorMessage = LanguageHelper.CommonManager.GetString("Tip_WrongPassword");
             if (document != null)
             {
                 if(PasswordType == PasswordType.UserPassword)
@@ -72,19 +74,26 @@ namespace Compdfkit_Tools.Common
                     }
                     else
                     {
-                        PasswordDialog.SetShowError("Wrong Password", Visibility.Visible);
+                        PasswordDialog.SetShowError(errorMessage, Visibility.Visible);
                     }
                 }
                 else
                 {
-                    if (document.CheckOwnerPassword(e))
+                    if (document.UnlockWithPassword(e))
                     {
-                        PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
-                        CloseWindow(passwordEventArgs);
+                        if(document.CheckOwnerPassword(e))
+                        {
+                            PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
+                            CloseWindow(passwordEventArgs);
+                        }
+                        else
+                        {
+                            PasswordDialog.SetShowError(errorMessage, Visibility.Visible);
+                        }
                     }
                     else
                     {
-                        PasswordDialog.SetShowError("Wrong Password", Visibility.Visible);
+                        PasswordDialog.SetShowError(errorMessage, Visibility.Visible);
                     }
                 }
             }

+ 16 - 1
Demo/Examples/Compdfkit_Tools/DigitalSignature/DigitalSignatureControl/DigitalSignatureControl.xaml.cs

@@ -16,6 +16,7 @@ using Compdfkit_Tools.Helper;
 using ComPDFKit.DigitalSign;
 using ComPDFKit.PDFAnnotation.Form;
 using ComPDFKitViewer;
+using PasswordBoxPlus.Helper;
 
 namespace Compdfkit_Tools.PDFControl
 {
@@ -515,6 +516,12 @@ namespace Compdfkit_Tools.PDFControl
                             { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
                     }
                 }
+                else if (e.PressOnSelectedText)
+                {
+                    e.Handle = true;
+                    e.PopupMenu = new ContextMenu();
+                    e.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
+                }
                 else
                 {
                     e.Handle = true;
@@ -637,8 +644,16 @@ namespace Compdfkit_Tools.PDFControl
                     e.PopupMenu.Items.Add(resetFormMenu);
                 }
             }
-            else
+            if (e != null && e.CommandType == CommandType.Copy)
             {
+                if (PDFViewControl?.PDFView == null) return;
+                if (!PDFViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
+                {
+                    if(!PasswordHelper.UnlockWithOwnerPassword(PDFViewControl.PDFView.Document))
+                    {
+                        return;
+                    }
+                }
                 e.DoCommand();
             }
         }

+ 2 - 1
Demo/Examples/Compdfkit_Tools/Edit/ContentEditCOntrol/ContentEditControl.xaml.cs

@@ -605,7 +605,8 @@ namespace Compdfkit_Tools.PDFControl
             editCommand.PopupMenu = new ContextMenu();
             if (lastPDFEditEvent != null || lastPDFEditMultiEvents!=null)
             {
-                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
+                if(PdfViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
+                    editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
                 editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
                 editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
                 editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });

+ 8 - 2
Demo/Examples/Compdfkit_Tools/PDFView/RegularViewerControl.xaml.cs

@@ -11,6 +11,7 @@ using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Media.Imaging;
 using Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
+using Compdfkit_Tools.Common;
 using Compdfkit_Tools.Helper;
 using Compdfkit_Tools.PDFControl;
 using ComPDFKit.DigitalSign;
@@ -18,6 +19,7 @@ using ComPDFKit.PDFAnnotation.Form;
 using ComPDFKitViewer;
 using ComPDFKitViewer.AnnotEvent;
 using ComPDFKitViewer.PdfViewer;
+using PasswordBoxPlus.Helper;
 
 namespace Compdfkit_Tools.PDFView
 {
@@ -239,7 +241,7 @@ namespace Compdfkit_Tools.PDFView
                 }
                 else if (e.CommandTarget == TargetType.ImageSelection)
                 {
-                    if (PdfViewControl != null && PdfViewControl.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
+                    if (PdfViewControl?.PDFView != null && PdfViewControl.PDFView.GetSelectImageCount() > 0)
                     {
                         e.Handle = true;
                         e.PopupMenu = new ContextMenu();
@@ -381,9 +383,13 @@ namespace Compdfkit_Tools.PDFView
 
             if (e != null && e.CommandType == CommandType.Copy)
             {
+                if (PdfViewControl?.PDFView == null) return;
                 if (!PdfViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying)
                 {
-                    // Unlock
+                    if(!PasswordHelper.UnlockWithOwnerPassword(PdfViewControl.PDFView.Document))
+                    {
+                        return;
+                    }
                 }
                 e.DoCommand();
             }

+ 2 - 2
Demo/Examples/Compdfkit_Tools/Security/Encryption/FileGridListControl.xaml

@@ -50,7 +50,7 @@
                                IsReadOnly="True" AutoGenerateColumns="False" FontSize="14" SelectionMode="Extended"
                                ScrollViewer.CanContentScroll="False" HorizontalScrollBarVisibility="Auto" MouseLeftButtonDown="FileDataGrid_OnMouseLeftButtonDown" SelectionChanged="FileDataGrid_SelectionChanged">
                         <DataGrid.Columns>
-                            <DataGridTextColumn Width="*" Binding="{Binding Name}" >
+                            <DataGridTextColumn Width="*" Binding="{Binding FileName}" >
                                 <DataGridTextColumn.Header>
                                     <TextBlock Text="{Binding Converter={StaticResource SecurityResourceConverter},ConverterParameter=Table_FileName}"/>
                                 </DataGridTextColumn.Header>
@@ -60,7 +60,7 @@
                                     <TextBlock Text="{Binding Converter={StaticResource SecurityResourceConverter},ConverterParameter=Table_FileSize}"/>
                                 </DataGridTextColumn.Header>
                             </DataGridTextColumn>
-                            <DataGridTextColumn Width="*" Binding="{Binding Path}">
+                            <DataGridTextColumn Width="*" Binding="{Binding Location}">
                                 <DataGridTextColumn.Header>
                                     <TextBlock Text="{Binding Converter={StaticResource SecurityResourceConverter},ConverterParameter=Table_Path}"/>
                                 </DataGridTextColumn.Header>

+ 4 - 7
Demo/Examples/Compdfkit_Tools/Security/Encryption/FileGridListControl.xaml.cs

@@ -14,6 +14,7 @@ using Compdfkit_Tools.Common;
 using Compdfkit_Tools.Helper;
 using ComPDFKit.PDFDocument;
 using Microsoft.Win32;
+using PasswordBoxPlus.Helper;
 
 namespace Compdfkit_Tools.PDFControl
 {
@@ -106,20 +107,16 @@ namespace Compdfkit_Tools.PDFControl
                 {
                     continue;
                 }
+                
                 if (document.IsLocked)
                 {
-                    PasswordWindow window = new PasswordWindow();
-                    window.InitWithDocument(document);
-                    window.Owner = Window.GetWindow(this);
-                    window.PasswordDialog.SetShowText(fileInfoData.FileName + " is encrypted.");
-                    window.ShowDialog();
-                    if (document.IsLocked)
+                    if(!PasswordHelper.UnlockWithOwnerPassword(document))
                     {
                         document.Release();
                         continue;
                     }
                 }
-
+                
                 fileInfoData.Document = document;
                 FileInfoDataList.Add(fileInfoData);
 

+ 9 - 0
Demo/Examples/Compdfkit_Tools/Strings/Common.Designer.cs

@@ -815,6 +815,15 @@ namespace Compdfkit_Tools.Strings.Common {
             }
         }
         
+        /// <summary>
+        ///   Looks up a localized string similar to is with permission restrictions..
+        /// </summary>
+        internal static string Tip_Permission {
+            get {
+                return ResourceManager.GetString("Tip_Permission", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   Looks up a localized string similar to Wrong Password.
         /// </summary>

+ 3 - 0
Demo/Examples/Compdfkit_Tools/Strings/Common.resx

@@ -486,4 +486,7 @@
   <data name="Warn_NotSave" xml:space="preserve">
     <value>is changed. Do you want to save the changes?</value>
   </data>
+  <data name="Tip_Permission" xml:space="preserve">
+    <value>is with permission restrictions.</value>
+  </data>
 </root>

+ 5 - 2
Demo/Examples/Compdfkit_Tools/Strings/Common.zh.resx

@@ -325,7 +325,7 @@
     <value>文件已加密!</value>
   </data>
   <data name="Tip_Password" xml:space="preserve">
-    <value>请输入密码</value>
+    <value>密码</value>
   </data>
   <data name="Tip_WrongPassword" xml:space="preserve">
     <value>密码错误</value>
@@ -334,7 +334,7 @@
     <value>新建</value>
   </data>
   <data name="Title_Password" xml:space="preserve">
-    <value>输入密码以查看文档</value>
+    <value>输入密码</value>
   </data>
   <data name="Title_QuickStart" xml:space="preserve">
     <value>点击打开 &amp; 处理文档</value>
@@ -486,4 +486,7 @@
   <data name="Warn_NotSave" xml:space="preserve">
     <value>已修改,是否保存更改?</value>
   </data>
+  <data name="Tip_Permission" xml:space="preserve">
+    <value>具有权限限制</value>
+  </data>
 </root>

+ 2 - 0
Demo/Examples/PDFViewer/SettingsDialog.xaml.cs

@@ -3,6 +3,7 @@ using System.ComponentModel;
 using System.Diagnostics;
 using System.Windows;
 using System.Windows.Controls;
+using Compdfkit_Tools.Helper;
 
 namespace PDFViewer
 {
@@ -16,6 +17,7 @@ namespace PDFViewer
         {
             InitializeComponent();
             Closing += MainWindow_Closing;
+            Title = App.MainResourceManager.GetString("Title_Settings");
         }
 
         private void MainWindow_Closing(object sender, CancelEventArgs e)