From 3a06df09215f992d5a1d62ba8ec4ef5830dc84d8 Mon Sep 17 00:00:00 2001 From: vicente Date: Tue, 5 Jan 2021 14:24:59 +0100 Subject: [PATCH] Afegir Funcionalitat martenir signatures i desar a mateixa carpeta que l'original --- UtilitatPdf/MainWindow.xaml | 8 ++++- UtilitatPdf/MainWindow.xaml.cs | 62 +++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/UtilitatPdf/MainWindow.xaml b/UtilitatPdf/MainWindow.xaml index a81b760..faa5ac4 100644 --- a/UtilitatPdf/MainWindow.xaml +++ b/UtilitatPdf/MainWindow.xaml @@ -97,7 +97,13 @@ - + + - S'afegirà '-sense-signatura' al nom del fitxer i es desarà a la mateixa carpeta que + l'original. + + - Si s'activa, s’editaran les signatures però Adobe Reader donarà error de signatures + degut a que l'arxiu s'ha modificat desprès d’haver-lo signat. + diff --git a/UtilitatPdf/MainWindow.xaml.cs b/UtilitatPdf/MainWindow.xaml.cs index f82892b..8b0a7ff 100644 --- a/UtilitatPdf/MainWindow.xaml.cs +++ b/UtilitatPdf/MainWindow.xaml.cs @@ -18,6 +18,8 @@ using Rectangle = iText.Kernel.Geom.Rectangle; using System.Windows.Controls; using Canvas = iText.Layout.Canvas; using System.Collections.ObjectModel; +using System.Collections.Generic; +using System.Linq; namespace UtilitatPdf { @@ -143,19 +145,44 @@ namespace UtilitatPdf } private void RemoveSign_Button_Click(object sender, RoutedEventArgs e) { - if (string.IsNullOrEmpty(InSignFile.Text) || string.IsNullOrEmpty(OutSignFile.Text)) + string OutputFilename = string.Empty; + + if (CheckSameFolder.IsChecked ?? false) { - MessageBox.Show("Has de seleccioner l'arxiu d'origen i destí.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + string filePath = Path.GetDirectoryName(InSignFile.Text); + string fileName = Path.GetFileNameWithoutExtension(InSignFile.Text); + string fileExt = Path.GetExtension(InSignFile.Text); + OutputFilename = filePath + @"\" + fileName + "-sense-signatura" + fileExt; + } + + if (string.IsNullOrEmpty(InSignFile.Text)) + { + MessageBox.Show("Has de seleccioner un arxiu d'origen.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { + if (string.IsNullOrEmpty(OutSignFile.Text)) + { + if (!CheckSameFolder.IsChecked ?? false) + { + MessageBox.Show("Has de seleccioner un arxiu de destí.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + else + { + if (!CheckSameFolder.IsChecked ?? false) + { + if (Path.GetExtension(OutSignFile.Text) != ".pdf") + { + OutSignFile.Text += ".pdf"; + } + OutputFilename = OutSignFile.Text; + } + } + if (File.Exists(InSignFile.Text)) { - if(Path.GetExtension(OutSignFile.Text) != ".pdf") - { - OutSignFile.Text += ".pdf"; - } - if (ManipulatePdf(InSignFile.Text, OutSignFile.Text)) + if (ManipulatePdf(InSignFile.Text, OutputFilename, CheckKeepSignature.IsChecked ?? false)) { InSignFile.Text = string.Empty; OutSignFile.Text = string.Empty; @@ -164,7 +191,7 @@ namespace UtilitatPdf } else { - MessageBox.Show("L'arxiu: "+ InSignFile.Text + " No Existiex.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show("L'arxiu: " + InSignFile.Text + " No Existiex.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } @@ -210,7 +237,7 @@ namespace UtilitatPdf } #endregion #region RemoveSignature - private bool ManipulatePdf(string resource, string result) + private bool ManipulatePdf(string resource, string result, bool KeepSignatures=false) { string patronCif = @"([a-zA-Z]-?[0-9]{8})"; string patronNif = @"([0-9]{8}-?[a-zA-Z])"; @@ -259,10 +286,10 @@ namespace UtilitatPdf pdfWidgetAnnotation.SetNormalAppearance(form.GetPdfObject()); } - acroForm.PartialFormFlattening(name); + if(!KeepSignatures) acroForm.PartialFormFlattening(name); } } - acroForm.FlattenFields(); + if (!KeepSignatures) acroForm.FlattenFields(); doneOk = true; } } @@ -296,7 +323,18 @@ namespace UtilitatPdf return string.Empty; } #endregion + + private void CheckBox_Checked(object sender, RoutedEventArgs e) + { + OutSignFile.IsEnabled = false; + SelectFile6.IsEnabled = false; + } + private void CheckBox_UnChecked(object sender, RoutedEventArgs e) + { + OutSignFile.IsEnabled = true; + SelectFile6.IsEnabled = true; + } + } // END Window Class - } // END Namespace