Afegir Funcionalitat martenir signatures i desar a mateixa carpeta que l'original

This commit is contained in:
2021-01-05 14:24:59 +01:00
parent 670fe977c2
commit 3a06df0921
2 changed files with 57 additions and 13 deletions

View File

@@ -97,7 +97,13 @@
<TextBox x:Name="OutSignFile" Margin="20,0,0,0" Height="23" Width="400"/>
<Button x:Name="SelectFile6" Margin="0,0,0,0" Width="30" Click="SelectSaveFile_Button_Click">...</Button>
</DockPanel>
<Button Margin="130,30,130,0" Height="30" Click="RemoveSign_Button_Click">Treure Signatura</Button>
<CheckBox x:Name="CheckSameFolder" Content="Desar en el mateix lloc que l'original" Margin="20,20,0,0" HorizontalAlignment="Left" Checked="CheckBox_Checked" Unchecked="CheckBox_UnChecked" IsChecked="True"/>
<TextBlock Margin="50,10,0,0">- S'afegirà '-sense-signatura' al nom del fitxer i es desarà a la mateixa carpeta que</TextBlock>
<TextBlock Margin="60,5,0,0"> l'original.</TextBlock>
<CheckBox x:Name="CheckKeepSignature" Content="Mantenir signatures originals" Margin="20,20,0,0" HorizontalAlignment="Left"/>
<TextBlock Margin="50,10,0,0">- Si s'activa, seditaran les signatures però Adobe Reader donarà error de signatures</TextBlock>
<TextBlock Margin="60,5,0,0">degut a que l'arxiu s'ha modificat desprès dhaver-lo signat.</TextBlock>
<Button Margin="0,20,0,0" Height="30" Width="180" HorizontalAlignment="Center" Click="RemoveSign_Button_Click">Treure Signatura</Button>
</StackPanel>
</Grid>
</TabItem>

View File

@@ -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