Browse Source

compdf(win) - 保存文件默认名称

liuaoran 1 year ago
parent
commit
3d820a26e0

+ 3 - 2
Demo/Examples/Compdfkit_Tools/Common/Helper/CommonHelper.cs

@@ -109,12 +109,13 @@ namespace Compdfkit_Tools.Helper
             return selectedFilePath;
         }
           
-        public static string GetGeneratePathOrEmpty(string filter)
+        public static string GetGeneratePathOrEmpty(string filter, string defaultFileName = "")
         {
             string selectedFilePath = string.Empty;
             SaveFileDialog saveFileDialog = new SaveFileDialog
             {
-                Filter = filter
+                Filter = filter,
+                FileName = defaultFileName
             };
             if (saveFileDialog.ShowDialog() == true)
             {

+ 0 - 1
Demo/Examples/Compdfkit_Tools/Compdfkit_Tools.csproj

@@ -696,7 +696,6 @@
       <SubType>Designer</SubType>
       <Generator>MSBuild:Compile</Generator>
     </Page>
-    <Page Include="DigitalSignature\AddCertificationControl\SaveCustomCertificationControl.xaml" />
     <Page Include="DigitalSignature\CPDFSignatureListControl\CPDFSignatureListControl.xaml" />
     <Page Include="DigitalSignature\DigitalSignatureControl\DigitalSignatureControl.xaml">
       <SubType>Designer</SubType>

+ 5 - 3
Demo/Examples/Compdfkit_Tools/DigitalSignature/AddCertificationControl/AddCertificationDialog.xaml.cs

@@ -49,8 +49,8 @@ namespace Compdfkit_Tools.PDFControl
                 this.Width = addExistedCertificationControl.Width + 20;
                  
                 addExistedCertificationControl.CancelEvent += CancelEvent;
-                addExistedCertificationControl.FillSignatureEvent -= AddExistedCertificationControl_FillSignatureEvent;
-                addExistedCertificationControl.FillSignatureEvent += AddExistedCertificationControl_FillSignatureEvent;
+                addExistedCertificationControl.FillSignatureEvent -= FillSignature;
+                addExistedCertificationControl.FillSignatureEvent += FillSignature;
 
 
             }
@@ -74,10 +74,12 @@ namespace Compdfkit_Tools.PDFControl
                 BodyBd.Child = saveCerficateControl;
                 this.Height = saveCerficateControl.Height + 20;
                 this.Width = saveCerficateControl.Width + 20;
+                saveCerficateControl.FillSignatureEvent -= FillSignature;
+                saveCerficateControl.FillSignatureEvent += FillSignature;
             }
         }
 
-        private void AddExistedCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
+        private void FillSignature(object sender, CertificateAccess e)
         {
             Close();
             FillSignatureEvent?.Invoke(sender, e);

+ 0 - 1
Demo/Examples/Compdfkit_Tools/DigitalSignature/AddCertificationControl/AddExistedCertificationControl.xaml.cs

@@ -38,7 +38,6 @@ namespace Compdfkit_Tools.PDFControl
             else
             {
                 FillSignatureEvent?.Invoke(sender, new CertificateAccess { filePath = FileNameTxt.Text, password = PasswordBoxTxt.Password });
-                
             }
         }
 

+ 12 - 4
Demo/Examples/Compdfkit_Tools/DigitalSignature/AddCertificationControl/SaveCerficateControl.xaml.cs

@@ -18,6 +18,8 @@ namespace Compdfkit_Tools.PDFControl
 
         public event PropertyChangedEventHandler PropertyChanged;
 
+        public event EventHandler<CertificateAccess> FillSignatureEvent;
+
         private string _filePath;
         public string FilePath
         {
@@ -68,10 +70,16 @@ namespace Compdfkit_Tools.PDFControl
             }
             certificateInfo += "/CN=" + CertificateInfo.GrantorName;
             bool is_2048 = CertificateInfo.AlgorithmType == AlgorithmType.RSA2048bit;
-            CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048);
-            FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog();
-            fillDigitalSignatureDialog.FilePath = FilePath;
-            fillDigitalSignatureDialog.ShowDialog();
+            if(CPDFPKCS12CertHelper.GeneratePKCS12Cert(certificateInfo, CertificateInfo.Password, FilePath, CertificateInfo.PurposeType, is_2048))
+            {
+                FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog();
+                CertificateAccess certificateAccess = new CertificateAccess()
+                {
+                    filePath = FilePath,
+                    password = CertificateInfo.Password
+                };
+                FillSignatureEvent?.Invoke(sender, certificateAccess);
+            } 
         }
 
         protected void UpdateProper<T>(ref T properValue,

+ 0 - 64
Demo/Examples/Compdfkit_Tools/DigitalSignature/AddCertificationControl/SaveCustomCertificationControl.xaml

@@ -1,64 +0,0 @@
-<UserControl x:Class="Compdfkit_Tools.DigitalSignature.AddCertificationControl.SaveCustomCertificationControl"
-             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:local="clr-namespace:Compdfkit_Tools.DigitalSignature.AddCertificationControl"
-             xmlns:common="clr-namespace:Compdfkit_Tools.Common"
-             mc:Ignorable="d"
-             d:DesignHeight="277" d:DesignWidth="532">
-    <UserControl.Resources>
-        <common:TextLengthToVisibilityConverter x:Key="TextLengthToVisibilityConverter"></common:TextLengthToVisibilityConverter>
-        <common:BoolToCollapsedConverter x:Key="BoolToCollapsedConverter"></common:BoolToCollapsedConverter>
-    </UserControl.Resources>
-    <Grid Margin="10">
-        <Grid.RowDefinitions>
-            <RowDefinition Height="Auto"></RowDefinition>
-            <RowDefinition Height="Auto"></RowDefinition>
-            <RowDefinition></RowDefinition>
-        </Grid.RowDefinitions>
-        
-        <TextBlock Text="Digitally signed ID cards can be saved locally for secondary use" 
-                   HorizontalAlignment="Left" Width="422" Height="20" FontSize="14" FontFamily="Segoe UI"></TextBlock>
-        
-        <Grid Grid.Row="1" Margin="0,10,0,10">
-            <Grid.RowDefinitions>
-                <RowDefinition></RowDefinition>
-                <RowDefinition></RowDefinition>
-                <RowDefinition></RowDefinition>
-                <RowDefinition></RowDefinition>
-            </Grid.RowDefinitions>
-            <Grid.ColumnDefinitions>
-                <ColumnDefinition Width="120"></ColumnDefinition>
-                <ColumnDefinition Width="258"></ColumnDefinition>
-                <ColumnDefinition></ColumnDefinition>
-            </Grid.ColumnDefinitions>
-            
-            <TextBlock Text="File location" FontSize="14" FontFamily="Segoe UI" VerticalAlignment="Center"></TextBlock>
-            <Grid Grid.Column="1">
-                <TextBox Height="28" x:Name="FileLocation" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
-                <TextBlock Text="Select a file" VerticalAlignment="Center"  Margin="10" Visibility="{Binding ElementName=FileLocation, Path=Text, Converter={StaticResource TextLengthToVisibilityConverter}}" IsHitTestVisible="False"  Foreground="#BBB"></TextBlock>
-            </Grid>
-            <Button Grid.Column="2" Content="Select File" Height="28" Margin="10,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Width="80"></Button>
-            
-            <TextBlock Grid.Row="1" Text="Set Password" FontSize="14" FontFamily="Segoe UI" VerticalAlignment="Center"></TextBlock>
-            <Grid Grid.Row="1" Grid.Column="1">
-                <PasswordBox Height="28" x:Name="Password" Password="{Binding SetPassword,Mode=OneWayToSource}" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
-                <TextBlock Text="Please enter the password" VerticalAlignment="Center"  Margin="10" Visibility="{Binding ElementName=Password, Path=Password.Length, Converter={StaticResource TextLengthToVisibilityConverter}}" IsHitTestVisible="False"  Foreground="#BBB"></TextBlock>
-            </Grid>
-            
-            <TextBlock Grid.Row="2" Text="Confirm Password" FontSize="14" FontFamily="Segoe UI" VerticalAlignment="Center"></TextBlock>
-            <Grid Grid.Row="2" Grid.Column="1">
-                <PasswordBox Height="28" x:Name="ConfirmPassword" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
-                <TextBlock Text="Please enter the password again" VerticalAlignment="Center"  Margin="10" Visibility="{Binding ElementName=ConfirmPassword, Path=Password.Length, Converter={StaticResource TextLengthToVisibilityConverter}}" IsHitTestVisible="False"  Foreground="#BBB"></TextBlock>
-            </Grid>
-            
-            <TextBlock Grid.Row="3" Grid.Column="1" Foreground="Red" Text="Confirmation password is not the same as the set password, please re-enter it"
-                       TextWrapping="Wrap" FontSize="12" FontFamily="Segoe UI" VerticalAlignment="Center"></TextBlock>
-        </Grid>
-        <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
-            <Button Width="112" Height="32" Content="Cancel"></Button>
-            <Button Width="112" Height="32" Content="Done" Margin="10,0,0,0"></Button>
-        </StackPanel>
-    </Grid>
-</UserControl>

+ 8 - 10
Demo/Examples/Compdfkit_Tools/DigitalSignature/FillDigitalSignatureControl/FillDigitalSignatureControl.xaml

@@ -82,7 +82,7 @@
                     <ColumnDefinition Width="auto"></ColumnDefinition>
                 </Grid.ColumnDefinitions>
                 <TextBlock FontSize="14" Margin="0,3,0,0">Contains Text</TextBlock>
-                <StackPanel Grid.Column="1" Orientation="Horizontal">
+                <StackPanel Margin="00,0,20,0" Grid.Column="1" Orientation="Horizontal">
                     <TextBlock FontSize="14" VerticalAlignment="Center" Margin="0,0,8,0">Text Direction</TextBlock>
                     <ToggleButton Name="TextAlignLeftBtn" Style="{StaticResource ToggleButtonStyle}" BorderThickness="0" Width="73" Height="30" 
                             IsChecked="True" Click="TextAlignBtn_Click">
@@ -133,17 +133,18 @@
                     <CheckBox x:Name="PositionChk" FontSize="14" Grid.Column="2" Grid.Row="2" Click="PositionChk_Click">Position</CheckBox>
                     <CheckBox x:Name="TabChk" FontSize="14" Grid.Column="2" Grid.Row="3" Click="TabChk_Click">Tab</CheckBox>
 
-                    <StackPanel x:Name="Reasonstp" Visibility="Collapsed" Orientation="Horizontal" Grid.Column="4" Margin="50,0,0,0" Grid.RowSpan="2" Height="30" Width="280">
-                        <TextBlock Text="Reason: " VerticalAlignment="Center"></TextBlock>
-                        <ComboBox x:Name="ReasonCmb"  SelectionChanged="ReasonCmb_SelectionChanged" VerticalContentAlignment="Center">
+                    <StackPanel x:Name="Reasonstp" Visibility="Visible" Orientation="Horizontal" Grid.Column="4" Margin="30,0,0,0" Grid.RowSpan="2" Height="30" Width="280">
+                        <TextBlock Text="Reason: " VerticalAlignment="Center" Width="50"></TextBlock>
+                        <ComboBox x:Name="ReasonCmb"  SelectionChanged="ReasonCmb_SelectionChanged" Width="220" VerticalContentAlignment="Center">
+                            <ComboBoxItem Content="&lt; None &gt;"></ComboBoxItem>
                             <ComboBoxItem Content="I am the owner of the document." IsSelected="True"></ComboBoxItem>
                             <ComboBoxItem Content="I am approving the document."></ComboBoxItem>
                             <ComboBoxItem Content="I have reviewed this document"></ComboBoxItem>
                         </ComboBox>
                     </StackPanel>
-                    <StackPanel x:Name="PositionStp" Visibility="Collapsed" Orientation="Horizontal" Grid.Column="4" Grid.Row="1" Margin="50,10,0,0"  Grid.RowSpan="2" Height="30" Width="280">
-                        <TextBlock Text="Position: " VerticalAlignment="Center"></TextBlock>
-                        <TextBox Width="180" x:Name="PositionTbx" TextChanged="PositionTbx_TextChanged"></TextBox>
+                    <StackPanel x:Name="PositionStp" Visibility="Visible" Orientation="Horizontal" Grid.Column="4" Grid.Row="1" Margin="30,10,0,0"  Grid.RowSpan="2" Height="30" Width="280">
+                        <TextBlock Text="Position: " VerticalAlignment="Center" Width="50"></TextBlock>
+                        <TextBox Width="220" x:Name="PositionTbx" TextChanged="PositionTbx_TextChanged"></TextBox>
                     </StackPanel>
                 </Grid>
 
@@ -163,9 +164,6 @@
                     <ColumnDefinition Width="auto"></ColumnDefinition>
                 </Grid.ColumnDefinitions>
 
-                <TextBlock Foreground="#43474D" FontSize="14" VerticalAlignment="Center">Present name</TextBlock>
-                <TextBox Grid.Column="1" Width="250" Height="30" Margin="10,0,0,0" VerticalContentAlignment="Center"></TextBox>
-
                 <Button x:Name="CancelBtn" Grid.Column="3" Height="32" Width="82" Style="{StaticResource LightButtonStyle}"
                         Background="#DFE9FF" BorderThickness="0" Click="CancelBtn_Click">Cancel</Button>
                 <Button x:Name="ContinueBtn" Grid.Column="4" Margin="10,0,25,0" Height="32" Width="97" Style="{StaticResource ButtonStyle}" Click="ContinueBtn_Click">Continue</Button>

+ 45 - 33
Demo/Examples/Compdfkit_Tools/DigitalSignature/FillDigitalSignatureControl/FillDigitalSignatureControl.xaml.cs

@@ -9,6 +9,7 @@ using System;
 using System.Collections.Generic;
 using System.Drawing;
 using System.Reflection;
+using System.Runtime.Remoting.Messaging;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
@@ -40,13 +41,13 @@ namespace Compdfkit_Tools.PDFControl
 
         private string reason = string.Empty;
 
-        private string _filePath = string.Empty;
-        public string signaturePath
-        {
-            get => _filePath;
+        private string _signaturePath = string.Empty;
+        public string SignaturePath
+        { 
+            get => _signaturePath;
             set
             {
-                _filePath = value;
+                _signaturePath = value;
             }
         }
 
@@ -57,7 +58,7 @@ namespace Compdfkit_Tools.PDFControl
             set
             {
                 _password = value;
-                signatureCertificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path(signaturePath, Password);
+                signatureCertificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path(SignaturePath, Password);
             }
         }
 
@@ -352,19 +353,15 @@ namespace Compdfkit_Tools.PDFControl
         private void BrowseTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
             string pngPath = CommonHelper.GetExistedPathOrEmpty("PNG files (*.png)|*.png");
-            if (pngPath != string.Empty)
+            if (!string.IsNullOrEmpty(pngPath))
             {
                 imagePath = CommonHelper.GetExistedPathOrEmpty(pngPath);
             }
-            else
-            {
-
-            }
         }
 
         private void ClearTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
-
+            imagePath = string.Empty;
         }
 
         private void NameChk_Click(object sender, RoutedEventArgs e)
@@ -452,33 +449,48 @@ namespace Compdfkit_Tools.PDFControl
         }
 
         private void ContinueBtn_Click(object sender, RoutedEventArgs e)
-        {
-            string filePath = CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf");
-            if (!string.IsNullOrEmpty(filePath))
+        { 
+            string filePath = CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf", Document.FileName+ "_Signed.pdf");
+
+            // 如果用户取消了文件保存对话框,则不执行后续操作
+            if (string.IsNullOrEmpty(filePath))
             {
-                if ((bool)ReasonChk.IsChecked)
-                {
-                    reason = (ReasonCmb?.SelectedItem as ComboBoxItem)?.Content?.ToString();
-                }
-                else
-                {
-                    reason = string.Empty;
-                }
+                return;
+            }
 
-                if ((bool)PositionChk.IsChecked)
-                {
-                    location = PositionTbx.Text; 
-                }
-                else
-                {
-                    location = string.Empty;
-                }
-                signatureWidget.UpdataApWithSignature(tempSignatureConfig);
-                Document.WriteSignatureToFilePath(signatureWidget, filePath, signaturePath, Password, location, reason, false);
+            // 在这里继续处理其他逻辑
+
+            if ((bool)ReasonChk.IsChecked)
+            {
+                reason = (ReasonCmb?.SelectedItem as ComboBoxItem)?.Content?.ToString();
+            }
+            else
+            {
+                reason = string.Empty;
+            }
+
+            if ((bool)PositionChk.IsChecked)
+            {
+                location = PositionTbx.Text;
             }
+            else
+            {
+                location = string.Empty;
+            }
+
+            signatureWidget.UpdataApWithSignature(tempSignatureConfig);
+
+            Document.WriteSignatureToFilePath(signatureWidget, filePath, SignaturePath, Password, location, reason, CPDFSignaturePermissions.CPDFSignaturePermissionsNone);
+            CloseWindow(sender);
         }
 
+
         private void CancelBtn_Click(object sender, RoutedEventArgs e)
+        {
+            CloseWindow(sender);
+        }
+
+        private void CloseWindow(object sender)
         {
             Window parentWindow = Window.GetWindow(sender as DependencyObject);
             parentWindow?.Close();

+ 1 - 1
Demo/Examples/Compdfkit_Tools/DigitalSignature/FillDigitalSignatureControl/FillDigitalSignatureDialog.xaml.cs

@@ -28,7 +28,7 @@ namespace Compdfkit_Tools.PDFControl
             set
             {
                 _filePath = value;
-                FillDigitalSignatureControl.signaturePath = value;
+                FillDigitalSignatureControl.SignaturePath = value;
             }
         }
 

BIN
Demo/Examples/packages/ComPDFKit.NetFramework.1.9.1/build/x64/ComPDFKit.dll