remove digital signature from document with itextsharp library - vb.net

good night,
I am working with itextsharp and want to remove the digital signatures from the document. I want to remove them not flatten them.
I found this code to flatten, what would be the variant to delete the signature.
I will appreciate any help.
Observation: I do not have the name of the signature field, it must be removed from any signed document.
Thanks a lot,
I'm working with visual studio 2019 and
itextsharp 5.5.13.2
Greetings.
Protected Sub ManipulatePdf(ByVal dest As String)
Dim pdf As PdfReader = New PdfReader(SRC)
Dim stamper As PdfStamper = New PdfStamper(pdf, New FileStream(dest, FileMode.Create, FileAccess.Write, FileShare.None))
stamper.FormFlattening = True
stamper.Close()
End Sub
Public Shared ReadOnly DEST As String = "c:\temp1\2.pdf"
Public Shared ReadOnly SRC As String = "c:\temp1\1.pdf"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim file As System.IO.FileInfo = New System.IO.FileInfo(DEST)
file.Directory.Create()
ManipulatePdf(DEST)
End Sub
Code update to find and remove signatures, not working yet.
Protected Sub ManipulatePdf(ByVal dest As String)
Dim pdf As PdfReader = New PdfReader(SRC)
Dim acro As AcroFields = pdf.AcroFields
Dim stamper As PdfStamper = New PdfStamper(pdf, New FileStream(dest, FileMode.Create, FileAccess.Write, FileShare.None))
Dim nombrfirma As List(Of String) = acro.GetSignatureNames()
For Each firma As String In nombrfirma
Dim signature As AcroFields.Item = stamper.AcroFields.GetFieldItem(firma)
For i = 0 To i < signature.Size
signature.GetWidget(i).Clear()
signature.GetMerged(i).Clear()
signature.GetValue(i).Clear()
i += 1
Next
Next
'stamper.FormFlattening = True
stamper.Close()
pdf.Close()
End Sub
Public Shared ReadOnly DEST As String = "c:\temp1\2.pdf"
Public Shared ReadOnly SRC As String = "c:\temp1\1.pdf"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim file As System.IO.FileInfo = New System.IO.FileInfo(DEST)
file.Directory.Create()
ManipulatePdf(DEST)
End Sub

Related

Exception for add document variable(I cant figure out)

I cant figure out where is my error is.Below is my code and the exception:
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
Public Class Form1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Create a word file
Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create("C:\Users\local\Desktop\vb testing purpose\test.docx", WordprocessingDocumentType.Document)
' Add a main document part.
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
' Create the document structure and add some text.
mainPart.Document = New Document()
Dim body As Body = mainPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim run As Run = para.AppendChild(New Run())
run.AppendChild(New Text(""))
End Using
Dim strDoc As String = "C:\Users\local\Desktop\vb testing purpose\test.docx"
Dim varname As String = "Hi"
Dim varvalue As String = "Bryan"
AddDocVar(strDoc, varname, varvalue)
End Sub
Public Sub AddDocVar(ByVal sender As Object, ByVal varName As String, ByVal varValue As String)
Dim mainPart As MainDocumentPart = sender.MainDocumentPart
Dim settingsPart As DocumentSettingsPart = mainPart.DocumentSettingsPart
settingsPart.Settings = New Settings(New DocumentVariables(New DocumentVariable() With {.Name = varName, .Val = varValue}))
End Sub
End Class
Comes out a
exception(System.MissingMemberException: 'Public member 'MainDocumentPart' on type 'String' not found.') in
Dim mainPart As MainDocumentPart = sender.MainDocumentPart

Im cant get a doc from my documents to display

Imports System.IO
Public Class Form1
Private Sub MenuItem1_Click(sender As Object, e As EventArgs) Handles MenuItem1.Click
Me.txtFileContent.Text = Nothing
End Sub
Private Sub MenuItem2_Click(sender As Object, e As EventArgs) Handles MenuItem2.Click
Me.txtFileContent.Text = Nothing
End Sub
Private Sub MenuItem3_Click(sender As Object, e As EventArgs) Handles MenuItem3.Click
Me.savSaveFile.ShowDialog()
Dim strFileName As String = Me.savSaveFile.FileName
Dim fs As New FileStream(strFileName, FileMode.Create, FileAccess.Write)
Dim TextFile As New StreamWriter(fs)
TextFile.Write(Me.txtFileContent.Text)
TextFile.Close()
fs.Close()
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MenuItem5.Click
Me.savSaveFile.ShowDialog()
Dim strFileName As String = Me.savSaveFile.FileName
Dim fs As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Dim TextFile As New StreamWriter(fs)
TextFile.Close()
fs.Close()
End Sub
End Class
What im trying to do is display a "open" document dialog box so the text inside the box can be displayed into the text box. I did the same with the "save" document dialog box and it worked fine just confused on the Open part. Thanks a million everyone.
There are two different kind of dialog box for selecting files.
One for save operations and one for opening files.
Your code seems to use the same one and from the name I think it is a SaveFileDialog.
To open a file you need a OpenFileDialog and use a StreamReader not a StreamWriter
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MenuItem5.Click
' Create an OpenFileDialog here, for more precise property settings see the link above'
Dim openDlg = New OpenFileDialog()
' If user presses OK'
if openDlg.ShowDialog() = DialogResult.OK Then
' Still the filename selected could be empty, need to check'
Dim strFileName As String = openDlg.FileName
if strFileName.Trim().Length > 0 then
' Open the file using a reader, not a writer'
Using fs = New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Using TextFile = New StreamReader(fs)
' Read everything (caution this should be used only for small files)'
Dim fileContent = TextFile.ReadToEnd
' Pass everything into a TextBox control for display'
Me.txtFileContent.Text = fileContent
End Using
End Using
End If
End If
End Sub

Bind a PictureBox to DataColum in VB.NET

I have a data repeater that contains a PictureBox and the picture needs to be fetched from a webserver. I already have a function that downloads the image, but the binding does not seem to be working. I am quite new to using windows forms and I am not too sure what I am doing wrong.
I have tried using a gridview, and I can confirm that that does show the images.
Public Function DlImg(ByVal _URL As String) As Byte()
Dim _tmpImage As Image = Nothing
Dim BytesOut As Byte()
Try
Dim _HttpWebRequest As System.Net.HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(_URL), System.Net.HttpWebRequest)
_HttpWebRequest.AllowWriteStreamBuffering = True
Dim _WebResponse As System.Net.WebResponse = _HttpWebRequest.GetResponse():
Dim _WebStream As System.IO.Stream = _WebResponse.GetResponseStream()
_tmpImage = Image.FromStream(_WebStream)
_WebResponse.Close()
Using picture As Image = _tmpImage
Using stream As New IO.MemoryStream
picture.Save(stream, Imaging.ImageFormat.Jpeg)
BytesOut = stream.GetBuffer()
End Using
End Using
Catch _Exception As Exception
Return Nothing
End Try
Return BytesOut
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListData.Columns.Add("Image", Type.GetType("System.Byte[]"))
For Each row As DataRow In ListData.Rows
row("Image") = DlImg("http://rental.joshblease.co.uk/propertyimages/p184js94kv1qco1e1e1jt71ouv11lm6.jpg")
Next row
ImgListItem.DataBindings.Add("Image", ListData, "Image", True)
DataGridView1.DataSource = ListData
DataRepeater1.DataSource = ListData
End Sub

Display Metadata-thumbnail of Jpeg in picturebox

I need to display a Image's thumbnail, that is saved in its Metadata in a picturebox. I'm using VB.NET
http://msdn.microsoft.com/en-us/library/windows/desktop/ee719904%28v=vs.85%29.aspx#_jpeg_metadata
So far i came up with this. Adding a breakpoint displays that GETQUERY returns empty even if i know that the file does indeed have a thumbnail
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim imagepath = "C:\xampp\htdocs\Downloads\IMG_1322.JPG" ' path to file
Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)
Dim ms As New System.IO.MemoryStream
Dim bm As Bitmap
Dim arData() As Byte
arData = metadata.GetQuery("/app0/{ushort=6}") '<--- Breakpoint here: Query returns nothing!
ms.Write(arData, 78, arData.Length - 78)
bm = New Bitmap(ms)
PictureBox1.Image = bm
stream.Close()
End Sub
You can try something like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim imagepath = "C:\xampp\htdocs\Downloads\IMG_1322.JPG" ' path to file
Dim stream = New FileStream(imagepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim decoder = New JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None)
Dim metadata = TryCast(decoder.Frames(0).Metadata, BitmapMetadata)
Dim thumb As BitmapMetadataBlob
thumb = metadata.GetQuery("/app1/thumb/")
If Not (thumb Is Nothing) Then
Dim src As New BitmapImage
Dim ms As MemoryStream = New MemoryStream(thumb.GetBlobValue())
src.BeginInit()
src.StreamSource = ms
src.EndInit()
PictureBox1.Source = src
End If
stream.Close()
End Sub

VB.net Multiple StreamReader and FileStream in a single button

I want to create multiple of these readers but my program only reads the first filestream is there a way for it to read them all? or do i have to place them in different buttons? here is my current code, :
Public aRecp As String()
Public listRecp As New List(Of String)
Public aEmail As String()
Public listEmail As New List(Of String)
Public aName As String()
Public listName As New List(Of String)
Public sArray As String()
Public sList As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
sList.Add(sReader.ReadLine)
Loop
sArray = sList.ToArray
fStream.Close()
sReader.Close()
Dim StreamName As New System.IO.FileStream("sendername.txt", IO.FileMode.Open)
Dim ReaderName As New System.IO.StreamReader(StreamName)
Dim IndexName As Integer = 0
Do While ReaderName.Peek >= 0
listName.Add(sReader.ReadLine)
Loop
aName = listName.ToArray
StreamName.Close()
ReaderName.Close()
Dim StreamEmail As New System.IO.FileStream("senderemail.txt", IO.FileMode.Open)
Dim ReaderEmail As New System.IO.StreamReader(StreamEmail)
Dim IndexEmail As Integer = 0
Do While ReaderEmail.Peek >= 0
listEmail.Add(sReader.ReadLine)
Loop
aEmail = listEmail.ToArray
StreamEmail.Close()
ReaderEmail.Close()
Dim StreamRecp As New System.IO.FileStream("recpname.txt", IO.FileMode.Open)
Dim ReaderRecp As New System.IO.StreamReader(StreamRecp)
Dim IndexRecp As Integer = 0
Do While ReaderRecp.Peek >= 0
listRecp.Add(ReaderRecp.ReadLine)
Loop
aRecp = listRecp.ToArray
StreamRecp.Close()
ReaderRecp.Close()
End Sub
Not a direct answer to your question (and there's nothing obvious to me in your posted code as to why it's only executing the first reader), but since you're reading text files it'd be a lot less code to use File.ReadAllLines(fileName), like this:
Public aRecp As String()
Public aEmail As String()
Public aName As String()
Public sArray As String()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
sArray = File.ReadAllLines("messages.txt")
aName = File.ReadAllLines("sendername.txt")
aEmail = File.ReadAllLines("senderemail.txt")
aRecp = File.ReadAllLines("recpname.txt")
End Sub
File.ReadAllLines(fileName) returns an array that has each line of the text file as an element. Much simpler than creating the stream, peeking your way through and reading each line into a list and then converting it to an array.