How to Add a signature form field visible on all pages using itext7 - pdf

as showed on another post, using iText7 I can add a signature field on a pdf document so that it is visible on first and last page.
So, I need to add an empty signature field visible on all pages.
Is there any way to do my goal?
I tried to ad the dame field multiple times (but N field will be created):
Using reader As New PdfReader(sourceFile)
Using writer As New PdfWriter(destFile)
Dim document As PdfDocument = New PdfDocument(reader, writer, New StampingProperties)
Dim r As New iText.Kernel.Geom.Rectangle(400, 380, 120, 60)
Dim field As PdfFormField = PdfFormField.CreateSignature(document, r)
field.SetFieldName(fieldName)
Dim form As PdfAcroForm = PdfAcroForm.GetAcroForm(document, True)
For i As Integer = document.GetNumberOfPages To 1 Step -1
form.AddField(field, document.GetPage(i))
Next
field.UpdateDefaultAppearance()
document.Close()
writer.Close()
End Using
End Using
I tried also to add widget to the same signature (but it seems not correct):
Using reader As New PdfReader(sourceFile)
Using writer As New PdfWriter(destFile)
Dim document As PdfDocument = New PdfDocument(reader, writer, New StampingProperties)
Dim r As New iText.Kernel.Geom.Rectangle(400, 380, 120, 60)
Dim field As PdfFormField = PdfFormField.CreateSignature(document, r)
field.SetFieldName(fieldName)
Dim form As PdfAcroForm = PdfAcroForm.GetAcroForm(document, True)
form.AddField(field, document.GetPage(1))
For i As Integer = 2 To document.GetNumberOfPages
Dim w As New PdfWidgetAnnotation(r)
w.MakeIndirect(document)
field.AddKid(w)
w.SetPage(document.GetPage(i))
field.SetPage(i)
Next
field.UpdateDefaultAppearance()
document.Close()
writer.Close()
End Using
End Using

Related

RDLC generate barcode using ZXing

I am pretty new to the RDLC report feature, I am looking to generate labels from Product data within a SQL database. When the user opens this Product/Part they are greeted with the information. When the user then clicks a button this will open the Report which will pass the parameters across to the Report in order to generate the label.
Dim myparam As ReportParameter
Dim testParameter As New List(Of ReportParameter)
myparam = New ReportParameter("PartID", "Test")
testParameter.Add(myparam)
myparam = New ReportParameter("MRPID", "Test MRP")
testParameter.Add(myparam)
myparam = New ReportParameter("PartName", "Test Name")
testParameter.Add(myparam)
ReportViewer1.LocalReport.SetParameters(testParameter)
Dim writer As New BarcodeWriter
writer.Format = BarcodeFormat.CODE_128
PictureBox1.Image = writer.Write(MRPID)
Me.ReportViewer1.RefreshReport()
As you can see, I am using XLing to generate my barcodes, which I have been successful in making work with the 3 lines of code you see above. However, I have no idea how I can pass this or have this generate on the report when ran. The barcode will be generated from the MRPID ie(TV001232). I understand this part is wrong "writer.Write(MRPID)" but I replaced the parameter value with MRPID so you could understand what I am trying to achieve.
Convert your image to Base64 string first using this:
Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
Dim base64String As String = ""
Using ms As New System.IO.MemoryStream()
image.Save(ms, format)
Dim imageBytes As Byte() = ms.ToArray()
base64String = Convert.ToBase64String(imageBytes)
End Using
Return base64String
End Function
So this:
myparam = New ReportParameter("MRPID", "Test MRP")
testParameter.Add(myparam)
Should be like this:
Dim writer As New BarcodeWriter
writer.Format = BarcodeFormat.CODE_128
myparam = New ReportParameter("MRPID", ImageToBase64(writer.Write(MRPID),<THE IMAGE FORMAT OF YOUR IMAGE>))
testParameter.Add(myparam)
Then, in your report set the following:
MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>
and in the Expression window:
=System.Convert.FromBase64String(Parameters!MRPID.Value)

VB.NET Return Form Object using Form Name

I'm basically writing a custom Error Logging Form for one of my applications because users cannot be trusted to report the errors to me.
I am obtaining the Form Name using the 'MethodBase' Object and then getting the DeclaringType Name.
Dim st As StackTrace = New StackTrace()
Dim sf As StackFrame = st.GetFrame(1)
Dim mb As MethodBase = sf.GetMethod()
Dim dt As String = mb.DeclaringType.Name
How can I then use this to obtain the Form Object so I can pass this to my 'screenshot method' that screenshots the particular form referenced.
Public Sub SaveAsImage(frm As Form)
'Dim fileName As String = "sth.png"
'define fileName
Dim format As ImageFormat = ImageFormat.Png
Dim image = New Bitmap(frm.Width, frm.Height)
Using g As Graphics = Graphics.FromImage(image)
g.CopyFromScreen(frm.Location, New Point(0, 0), frm.Size)
End Using
image.Save(_LogPath & Date.Now.ToString("ddMMyyyy") & ".png", format)
End Sub
I posted the same solution to a similar question. Try this:
Dim frm = Application.OpenForms.Item(dt)

Extract Images with text from PDF and Edit it using iTextSharp

I am trying to do following things in Windows Forms
1) Read a PDF in Windows Forms
2) Get the Images with Text in it
3) Color / fill the Image
4) save everything to a new file
I have tried Problem with PdfTextExtractor in itext!
But It didn't help.
Here is the code I've tried:
Public Shared Sub ExtractImagesFromPDF(sourcePdf As String, outputPath As String)
'NOTE: This will only get the first image it finds per page.'
Dim pdf As New PdfReader(sourcePdf)
Dim raf As RandomAccessFileOrArray = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
Try
For pageNumber As Integer = 1 To pdf.NumberOfPages
Dim pg As PdfDictionary = pdf.GetPageN(pageNumber)
' recursively search pages, forms and groups for images.'
Dim obj As PdfObject = FindImageInPDFDictionary(pg)
If obj IsNot Nothing Then
Dim XrefIndex As Integer = Convert.ToInt32(DirectCast(obj, PRIndirectReference).Number.ToString(System.Globalization.CultureInfo.InvariantCulture))
Dim pdfObj As PdfObject = pdf.GetPdfObject(XrefIndex)
Dim pdfStrem As PdfStream = DirectCast(pdfObj, PdfStream)
Dim bytes As Byte() = PdfReader.GetStreamBytesRaw(DirectCast(pdfStrem, PRStream))
If (bytes IsNot Nothing) Then
Using memStream As New System.IO.MemoryStream(bytes)
memStream.Position = 0
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(memStream)
' must save the file while stream is open.'
If Not Directory.Exists(outputPath) Then
Directory.CreateDirectory(outputPath)
End If
Dim path__1 As String = Path.Combine(outputPath, [String].Format("{0}.jpg", pageNumber))
Dim parms As New System.Drawing.Imaging.EncoderParameters(1)
parms.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0)
'Dim jpegEncoder As System.Drawing.Imaging.ImageCodecInfo = iTextSharp.text.Utilities.GetImageEncoder("JPEG")'
img.Save(path__1) 'jpegEncoder, parms'
End Using
End If
End If
Next
Catch
Throw
Finally
pdf.Close()
raf.Close()
End Try
End Sub
Now, the actual purpose of this is to get something like this
If this is the actual PDF, I will have to check if there any any items in that bin(by Text in that box)
If there are items then I have to color it like below
Can someone help me with this
The PDF can be retrieved here.

itextsharp merge resizes and unrotates pdf

I'm trying to merge pdf files using itextsharp.
The problem 'm getting its that any cropping or rotating I've applied to the individual files before the merge is somehow ignored. All original files were cropped and rotated as TIFFs then converted to pdf and now finally I'm trying to merge them.
I'd like the page size to match the added content, and I any rotation I've applied to come through.'
Thank you for any help,
Corbin de Bruin
Public Function MergePDFFiles(FileList As Dictionary(Of String, String), DeleteOldFile As Boolean) As Byte()
' Public Function MergePDFFiles(FileList As Dictionary(Of String, String), DeleteOldFile As Boolean) As MemoryStream()
Dim document As New Document()
Dim output As New MemoryStream()
Try
Dim writer As PdfWriter = PdfWriter.GetInstance(document, output)
writer.PageEvent = New PdfPageEvents()
document.Open()
Dim content As PdfContentByte = writer.DirectContent
' foreach
For Each FilePath As KeyValuePair(Of String, String) In FileList
If File.Exists(FilePath.Value) Then
Dim reader As New PdfReader(FilePath.Value)
Dim numberOfPages As Integer = reader.NumberOfPages
For currentPageIndex As Integer = 1 To numberOfPages
document.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex))
document.NewPage()
' you can see iTextSharp.tutorial.01 - 0403sample
If currentPageIndex.Equals(1) Then
Dim par As New Paragraph(FilePath.Key)
Debug.Print("FilePath.Key = " & FilePath.Key)
Dim bookmark As New Chapter(par, 0) With {.NumberDepth = 0}
document.Add(bookmark)
End If
Dim importedPage As PdfImportedPage = writer.GetImportedPage(reader, currentPageIndex)
Dim pageOrientation As Integer = reader.GetPageRotation(currentPageIndex)
If (pageOrientation = 90) OrElse (pageOrientation = 270) Then
content.AddTemplate(importedPage, 0, 1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height)
Else
content.AddTemplate(importedPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Next
End If
Next
Catch exception As Exception
Debug.Print("Failure")
Finally
document.Close()
End Try
If DeleteOldFile Then
'Delete(FileList)
End If
Return output.GetBuffer()
End Function
End Try
If DeleteOldFile Then
'Delete(FileList)
End If
Return output.GetBuffer()
This question has been answered over and over again on StackOverflow. It's amazing that nobody voted to close it as a duplicate.
In any case: as I've answered many times before, it is bad practice to use PdfWriter/PdfImportedPage to merge document. Please read chapter 6 of the book I wrote about iText, and you'll discover that whoever provided you with the code sample you copied was all wrong. You should use PdfCopy to merge files, not PdfWriter!
For examples, read the following StackOverflow answers:
How to keep original rotate page in itextSharp (dll)
How to merge multiple pdf files (generated in run time)?
Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying
and so on...
If you're fast enough in accepting this answer, you'll probably be lucky enough not do get downvoted for not searching the archives before posting an already answered question.

Put content of fileStream in dataset

I want to read Stream that i get from XtrapivotGrid of DevExpress. I can save it in the computer but what i want is to save it in one of my table in my dataset called dataset1.
For now i have that code who permit to save it the directory Temp:.
Using FS As New IO.FileStream("D:\Temp\qqc.layout", IO.FileMode.Create)
PivotGridControl1.SaveLayoutToStream(FS)
End Using
Dim read As New System.IO.FileStream("D:\Temp\qqc.layout", IO.FileMode.Open, IO.FileAccess.Read)
DataSet1.LayoutMainRapport.ReadXml(read)
DataSet1.AcceptChanges()
read.Close()
The table LayoutMainRapport have 3 columns:
ID(Int)
Name(nvarchar(50))
Content(xml).
The output from the stream is xml.
thanks in advance!
I believe you should convert your saved layout into string and then save this string to database (and of course you can load this layout back from the database string value):
Private Function SaveLayoutToString(ByVal dxControl As DevExpressControl) As String
Using ms As MemoryStream = New MemoryStream()
dxControl.SaveLayoutToStream(ms)
Return Convert.ToBase64String(ms.ToArray())
End Using
End Function
Private Sub RestoreLayoutFromString(ByVal dxControl As DevExpressControl, ByVal layout As String)
If String.IsNullOrEmpty(layout) Then
Return
End If
Using ms As MemoryStream = New MemoryStream(Convert.FromBase64String(layout))
dxControl.RestoreLayoutFromStream(ms)
End Using
End Sub
Here DevExpressControl dxControl is the DevExpress control which supports saving and loading layout (XtraPivotGrid, XtraGrid, XtraLayoutControl etc.)
I simply had to save a name for the xml and after save it into my dataset.
Dim saveDialog As SaveLayout = New SaveLayout
If saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim read As New IO.MemoryStream
PivotGridControl1.SaveLayoutToStream(read)
read.Position = 0
Dim lecteur As New IO.StreamReader(read)
Dim ligne As DataRow = DataSet1.LayoutMainRapport.NewRow()
ligne("Nom") = saveDialog.txtNom.Text
ligne("Contenu") = lecteur.ReadToEnd()
DataSet1.LayoutMainRapport.Rows.Add(ligne)
LayoutMainRapportTableAdapter.Update(DataSet1)
End If