|
-
Oct 31st, 2008, 01:16 PM
#1
Re: iTextSharp help
OK, here is some quick stuff I put together for you.
Code:
Public Sub FillAcroForm(ByVal sourcePdf As String, ByVal fieldData As DataTable, ByVal outputPdf As String)
Try
'Open the pdf using pdfreader
Dim reader As New iTextSharp.text.pdf.PdfReader(sourcePdf)
'create a filestream for output
Dim fs As New System.IO.FileStream(outputPdf, IO.FileMode.Create, IO.FileAccess.Write)
'use stamper to copy the source pdf to output
Dim stamper As New iTextSharp.text.pdf.PdfStamper(reader, fs)
'Get the form from the pdf
Dim frm As iTextSharp.text.pdf.AcroFields = stamper.AcroFields
'get the fields from the form
Dim fields As System.Collections.Hashtable = frm.Fields
Dim matchedRows() As DataRow = Nothing
'Read each field name, look for the data in datatable, then set the field value
For Each key As String In fields.Keys
matchedRows = fieldData.Select("FieldName = '" & key & "'")
If matchedRows.Length > 0 Then
frm.SetField(key, matchedRows(0).Item("FieldValue").ToString())
End If
Next
stamper.Close()
fs.Close()
reader.Close()
Catch ex As Exception
Debug.Write(ex.Message)
End Try
End Sub
And You call the sub like this
Code:
'Create a datatable to hold the values for each field
Dim dt As New DataTable()
With dt.Columns
.Add("FieldName", GetType(String))
.Add("FieldValue", GetType(String))
End With
dt.Rows.Add("Length", "10")
dt.Rows.Add("Length2", "30")
'Fill the form, passing in the datatable
FillAcroForm("e:\formtest.pdf", dt, "e:\filled.pdf")
If you name your fields in using a splittable scheme, you can split the field name and look at the extra stuff to adjust your values when setting them. For example, instead of name a field "Length2", you can name it "Length_2". And when you read the field name, you spit it by "_", test the length of the array, if > 1 then you know you have some extra stuff. Read the 2nd element of the array to find out what it is.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|