163 lines
7.1 KiB
VB.net
163 lines
7.1 KiB
VB.net
Imports System
|
|
Imports System.IO
|
|
Imports System.Security.Cryptography
|
|
Imports System.Security.Cryptography.X509Certificates
|
|
Imports TSpdfUtils
|
|
Imports tsPDFUtilsCore
|
|
Imports TSpdf
|
|
|
|
|
|
Public Class Form1
|
|
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
|
'FirmaPDFPFX("prueba", "prueba", "prueba", "c:\tmp\pruebafirmada.pdf", True, True)
|
|
|
|
'Dim ms = New MemoryStream(System.IO.File.ReadAllBytes("c:\tmp\pruebasolicitudcertificado.pdf"))
|
|
'TSpdfUtils.PdfSignatureTools.MostrarRevisiones(ms)
|
|
|
|
Dim resultado As VerificadorPDFFirmado.Resultado
|
|
|
|
Using pdfOriginal As FileStream =
|
|
File.OpenRead("C:\tmp\o1.pdf")
|
|
|
|
Using pdfFirmado As FileStream =
|
|
File.OpenRead("C:\tmp\f1.pdf")
|
|
|
|
resultado =
|
|
VerificadorPDFFirmado.Verificar(
|
|
PdfOriginal:=pdfOriginal,
|
|
PdfFirmado:=pdfFirmado,
|
|
NombreCampoFirma1:="FirmaCliente",
|
|
NombreCampoFirma2:="",
|
|
ComprobarSegundaFirma:=False
|
|
)
|
|
|
|
End Using
|
|
End Using
|
|
|
|
If resultado.DocumentoValido Then
|
|
Console.WriteLine("PDF CORRECTO")
|
|
Else
|
|
Console.WriteLine(resultado.Mensaje)
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
Public Shared Sub FirmaPDFPFX(Razon As String, Contacto As String, Localizacion As String, FicheroPdfDef As String, EliminaFirmasAnteriores As Boolean, Optional EliminaFicheroDestino As Boolean = False)
|
|
Dim p As String = "prueba1234."
|
|
Try
|
|
Dim bpfx = System.IO.File.ReadAllBytes("C:\tmp\mmm.pfx")
|
|
Dim bpdf = System.IO.File.ReadAllBytes("c:\tmp\E_2026-00322_sellado.pdf")
|
|
Dim pk As New X509Certificate2(bpfx, p, X509KeyStorageFlags.Exportable)
|
|
CambiaKeyContainerName(pk)
|
|
If pk.NotAfter < Now Then Throw New Exception("El certificado está caducado")
|
|
' If Not pk.Subject.Contains("CN=" & DocumentoIdentidad) Then Throw New Exception("El certificado no pertenece al validador")
|
|
Dim ms As New MemoryStream
|
|
'tsUtilsPDF.pdf.FirmaPDF(pk, "", PdfStream, ms, Razon, Contacto, Localizacion, RutaTmp, EliminaFirmasAnteriores)
|
|
TSpdfUtils.Firmas.FirmaPDF(bpfx, p, "", New MemoryStream(bpdf), ms, Razon, Contacto, Localizacion, EliminaFirmasAnteriores)
|
|
If EliminaFicheroDestino AndAlso System.IO.File.Exists(FicheroPdfDef) Then System.IO.File.Delete(FicheroPdfDef)
|
|
System.IO.File.WriteAllBytes("c:\tmp\pruebafirmada.pdf", ms.ToArray)
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Sub
|
|
Public Shared Sub CambiaKeyContainerName(ByRef Certificado As System.Security.Cryptography.X509Certificates.X509Certificate2)
|
|
Dim rsa As RSACryptoServiceProvider = CType(Certificado.PrivateKey, RSACryptoServiceProvider)
|
|
Dim csparams As New CspParameters
|
|
csparams.KeyContainerName = "TECNOSIS-" & Guid.NewGuid.ToString
|
|
Dim rsa2 As New RSACryptoServiceProvider(csparams)
|
|
rsa2.ImportParameters(rsa.ExportParameters(True))
|
|
Certificado.PrivateKey = rsa2
|
|
End Sub
|
|
|
|
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
|
|
|
|
End Sub
|
|
Private Shared Sub FirmaDocumentoCFO(PdfOrigen As String, PdfDestino As String)
|
|
Dim br As Brush
|
|
Dim Imagen As Byte() = IO.File.ReadAllBytes("c:\tmp\OPENCOAS_LOGO.png")
|
|
'Dim TextoArt As String = ""
|
|
'If Doc.tiposdocumentosCOAS.Descripcion = "C.F.O. VISADO UNICO" Then
|
|
' TextoArt = "Conforme al Art. 5.1 del R.D. 1000/2010"
|
|
'End If
|
|
'If ds.NumeroExpedienteDocumentoOriginal.ToString <> "" Then
|
|
' ' probablemente haya que reducir la fuente en este caso
|
|
' TextoArt = "Sustituye al visado el " & ds.FechaVisadoOriginal.ToString("dd-MM-yyyy") & ". " & ds.NumeroExpedienteDocumentoOriginal.Split(" ")(1)
|
|
'Else
|
|
' Dim releds As relacionesentredocumentos = Doc.RelacionesComoHijo.FirstOrDefault(Function(x) x.Tipo = TipoRelacionEntreDocumentosEnum.SUSTITUCION)
|
|
' If releds IsNot Nothing Then
|
|
' Dim sFecVis As String
|
|
' If releds.DocumentoPadre.NumeroExpedienteDocumento.StartsWith("24/003566") OrElse releds.DocumentoPadre.NumeroExpedienteDocumento.StartsWith("24/003565") Then
|
|
' sFecVis = "22-12-2023"
|
|
' Else
|
|
' If releds.DocumentoPadre.FechaVisado.HasValue = False Then Throw New Exception("El documento al que sustituye no tiene fecha de visado")
|
|
' sFecVis = releds.DocumentoPadre.FechaVisado.Value.ToString("dd-MM-yyyy")
|
|
' End If
|
|
' TextoArt = "Sustituye al visado " & releds.DocumentoPadre.Documento & " el " & sFecVis
|
|
' End If
|
|
'End If
|
|
|
|
'If ds.TipoSello.StartsWith("2") Or ds.TipoSello.StartsWith("3") Then
|
|
' br = Brushes.Green
|
|
' Imagen = My.Resources.CFO_Verde
|
|
'Else
|
|
' br = Brushes.DarkBlue
|
|
' Imagen = My.Resources.CFO_Azul
|
|
'End If
|
|
Dim listadoTextos As List(Of DatosTextos) = New List(Of DatosTextos)
|
|
Dim listadoImagenes As List(Of DatosImagenFirma) = New List(Of DatosImagenFirma)
|
|
|
|
Dim Texto1 As New DatosTextos
|
|
Texto1.texto = "Solicitud Certificado Digital del COAS"
|
|
Texto1.size = 7.25
|
|
Texto1.NombreFuente = "Montserrat-Regular"
|
|
Texto1.PosicionEsquina = EsquinaEnum.INFERIOR_IZQUIERDA
|
|
Texto1.anguloRotacion = 270
|
|
Texto1.distanciaX = 7
|
|
Texto1.distanciaY = 715
|
|
Texto1.colorTexto = br
|
|
Texto1.estiloTexto = FontStyle.Regular
|
|
Texto1.AlineacionTexto = AlineamientoEnum.IZQUIERDA
|
|
|
|
listadoTextos.Add(Texto1)
|
|
|
|
Dim imagenFondo As New DatosImagenFirma
|
|
imagenFondo.PosicionEsquina = EsquinaEnum.INFERIOR_IZQUIERDA
|
|
imagenFondo.Alto = 100
|
|
imagenFondo.Ancho = 100
|
|
imagenFondo.PosicionX = 0
|
|
imagenFondo.PosicionY = 0
|
|
imagenFondo.AnguloRotacion = 270
|
|
imagenFondo.Imagen = Imagen
|
|
|
|
listadoImagenes.Add(imagenFondo)
|
|
|
|
Dim listadoFuentes As New List(Of DatosFuente)
|
|
|
|
Dim dfr As New DatosFuente
|
|
With dfr
|
|
.NombreFuente = "Montserrat-Regular"
|
|
.memoryFont = IO.File.ReadAllBytes("C:\tecnosis.tfs\Clientes\COAS\GestionWebCOAS\Content\Fuentes\Montserrat_Regular.ttf")
|
|
End With
|
|
listadoFuentes.Add(dfr)
|
|
Dim dfb As New DatosFuente
|
|
With dfb
|
|
.NombreFuente = "Montserrat-Bold"
|
|
.memoryFont = IO.File.ReadAllBytes("C:\tecnosis.tfs\Clientes\COAS\GestionWebCOAS\Content\Fuentes\Montserrat_Bold.ttf")
|
|
End With
|
|
listadoFuentes.Add(dfb)
|
|
|
|
Dim medidas As Kernel.Geom.Rectangle
|
|
'medidas = New Kernel.Geom.Rectangle(0, 0, 48.1889648, 396.8502)
|
|
medidas = New Kernel.Geom.Rectangle(0, 0, 48.1889648, 312.735)
|
|
|
|
TSpdfUtils.FirmaCanvas.FirmaPDFVisible(bPfx, ClavePFX, ClavePDF, PdfOrigen, PdfDestino, Razon, Contacto, Localizacion, bimagen, False, False, NombreCampoFirma, dcf, BloquearDocumento)
|
|
End Sub
|
|
End Class
|