Browse Source

compdfkit(win) - 优化密码窗口,加密文件再次加密的逻辑

weixiangjie 1 year ago
parent
commit
6e2ef7d9f8

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

@@ -1,6 +1,7 @@
 using ComPDFKitViewer.PdfViewer;
 using System;
 using System.Windows;
+using ComPDFKit.PDFDocument;
 
 namespace Compdfkit_Tools.Common
 {
@@ -9,7 +10,7 @@ namespace Compdfkit_Tools.Common
     /// </summary>
     public partial class PasswordWindow : Window
     {
-        private CPDFViewer pdfViewer;
+        private CPDFDocument document;
 
         public delegate void DialogCloseEventHandler(object sender, PasswordEventArgs e);
         public event DialogCloseEventHandler DialogClosed;
@@ -28,7 +29,12 @@ namespace Compdfkit_Tools.Common
 
         public void InitWithPDFViewer(CPDFViewer pdfViewer)
         {
-            this.pdfViewer = pdfViewer;
+            document = pdfViewer.Document;
+        }
+        
+        public void InitWithDocument(CPDFDocument document)
+        {
+            this.document = document;
         }
 
         private void PasswordDialog_Unloaded(object sender, RoutedEventArgs e)
@@ -46,10 +52,10 @@ namespace Compdfkit_Tools.Common
 
         private void PasswordDialog_Confirmed(object sender, string e)
         {
-            if (pdfViewer != null)
+            if (document != null)
             {
-                pdfViewer.Document.UnlockWithPassword(e);
-                if (pdfViewer.Document.IsLocked == false)
+                document.UnlockWithPassword(e);
+                if (document.IsLocked == false)
                 {
                     PasswordEventArgs passwordEventArgs = new PasswordEventArgs(e);
                     CloseWindow(passwordEventArgs);

+ 23 - 18
Demo/Examples/Compdfkit_Tools/Encryption/EncryptionDialog.xaml

@@ -4,29 +4,34 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:Compdfkit_Tools.PDFControl"
+        xmlns:common="clr-namespace:Compdfkit_Tools.Common"
         mc:Ignorable="d"
         ResizeMode="NoResize"
         Title="Batch Encryption" Height="504" Width="1052">
     <Grid>
-        <Grid.RowDefinitions>
-            <RowDefinition Height="Auto"></RowDefinition>
-            <RowDefinition Height="Auto"></RowDefinition>
-        </Grid.RowDefinitions>
-        <Grid.ColumnDefinitions>
-            <ColumnDefinition Width="7*"></ColumnDefinition>
-            <ColumnDefinition Width="4*"/>
-        </Grid.ColumnDefinitions>
+        <Grid>
+            <Grid.RowDefinitions>
+                <RowDefinition Height="Auto"></RowDefinition>
+                <RowDefinition Height="Auto"></RowDefinition>
+            </Grid.RowDefinitions>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="7*"></ColumnDefinition>
+                <ColumnDefinition Width="4*"/>
+            </Grid.ColumnDefinitions>
 
-        <local:FileGridListControl x:Name="FileListControl"/>
-        <local:SetEncryptionControl Grid.Row="0" Grid.Column="1" Margin="0,50,0,0" x:Name="SetEncryptionControl"/>
-        
-        <StackPanel Grid.Row="1" Orientation="Horizontal">
+            <local:FileGridListControl x:Name="FileListControl"/>
+            <local:SetEncryptionControl Grid.Row="0" Grid.Column="1" Margin="0,50,0,0" x:Name="SetEncryptionControl"/>
             
-        </StackPanel>
-        
-        <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,20,0">
-            <Button Height="32" Width="112" Content="Encrypt" IsEnabled="{Binding CanEncrypt}" Click="ButtonEncrypt_Click"></Button>
-            <Button Height="32" Width="112" Content="Cancel" Margin="10,0,0,0" Click="ButtonCancel_Click"></Button>
-        </StackPanel>
+            <StackPanel Grid.Row="1" Orientation="Horizontal">
+                
+            </StackPanel>
+            
+            <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,20,0">
+                <Button Height="32" Width="112" Content="Encrypt" IsEnabled="{Binding CanEncrypt}" Click="ButtonEncrypt_Click"></Button>
+                <Button Height="32" Width="112" Content="Cancel" Margin="10,0,0,0" Click="ButtonCancel_Click"></Button>
+            </StackPanel>
+        </Grid>
+        <Border Name="PopupBorder" Background="#A0000000" Visibility="Collapsed">
+        </Border>
     </Grid>
 </Window>

+ 27 - 14
Demo/Examples/Compdfkit_Tools/Encryption/EncryptionDialog.xaml.cs

@@ -8,6 +8,7 @@ using System.Runtime.CompilerServices;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Forms;
+using Compdfkit_Tools.Common;
 using ComPDFKit.PDFDocument;
 
 namespace Compdfkit_Tools.PDFControl
@@ -42,27 +43,39 @@ namespace Compdfkit_Tools.PDFControl
             var dialog = new FolderBrowserDialog();
             dialog.ShowDialog();
             var savePath = dialog.SelectedPath;
+            if (string.IsNullOrEmpty(savePath)) return;
+            
             var permissionsInfo = new CPDFPermissionsInfo();
+            string userPassword = string.Empty;
+            string ownerPassword = string.Empty;
 
-            if (!SetEncryptionControl.IsOwnerPasswordEnabled || string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
-            {
-                foreach (var fileInfoData in FileListControl.FileInfoDataList)
-                {
-                    document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
-                    document.Encrypt(SetEncryptionControl.UserPassword, null, permissionsInfo);
-                    document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf");
-                }
-            }
-            else
+            if (SetEncryptionControl.IsOwnerPasswordEnabled &&  !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword))
             {
                 permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint;
                 permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy;
-                foreach (var fileInfoData in FileListControl.FileInfoDataList)
+                ownerPassword = SetEncryptionControl.OwnerPassword;
+            }
+            if(SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword))
+            {
+                userPassword = SetEncryptionControl.UserPassword;
+            }
+            foreach (var fileInfoData in FileListControl.FileInfoDataList)
+            {
+                document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
+                if (document.IsLocked)
                 {
-                    document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
-                    document.Encrypt(SetEncryptionControl.UserPassword, SetEncryptionControl.OwnerPassword, permissionsInfo);
-                    document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf");
+                    PasswordWindow window = new PasswordWindow();
+                    window.InitWithDocument(document);
+                    window.Owner = this;
+                    window.ShowDialog();
+                    if (document.IsLocked)
+                    {
+                        continue;
+                    }
                 }
+                document.Encrypt(userPassword, ownerPassword, permissionsInfo);
+                document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf");
+                document.Release();
             }
         }