I setup a little process to loop through a CSV file and generate PDF files using the data in the csv.
Everything works fine except the text in the generated PDF files is not aligned properly with the boxes in the PDF. If i remove the data and fill it manually the alignment is fine.
Am I missing a step or doing something wrong? Below is my code to autofill the PDF form and attached are the sample CSV file, PDF Form Template and a FreeSpire.PDF generated example showing the issue.
- Code: Select all
Dim doc As PdfDocument = New PdfDocument()
doc.LoadFromFile(Server.MapPath("~/templates/" + Label1.Text))
Dim form As PdfFormWidget = CType(doc.Form, PdfFormWidget)
Dim formWidgetCollection As PdfFormFieldWidgetCollection = form.FieldsWidget
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
For ii As Integer = 0 To formWidgetCollection.Count - 1
Dim field As PdfField = formWidgetCollection(ii)
If TypeOf field Is PdfTextBoxFieldWidget Then
If field.Name = "TF001" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(0).Text
End If
If field.Name = "TF002" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(1).Text
End If
If field.Name = "TF003" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(2).Text
End If
If field.Name = "TF004" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(3).Text
End If
If field.Name = "TF005" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(4).Text
End If
If field.Name = "TF006" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(5).Text
End If
If field.Name = "TF007" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(6).Text
End If
If field.Name = "TF008" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(7).Text
End If
If field.Name = "TF009" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(8).Text
End If
If field.Name = "TF010" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(9).Text
End If
If field.Name = "TF011" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(10).Text
End If
If field.Name = "TF012" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(11).Text
End If
If field.Name = "TF013" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(12).Text
End If
If field.Name = "TF014" Then
Dim textBoxField As PdfTextBoxFieldWidget = CType(field, PdfTextBoxFieldWidget)
textBoxField.Font = New PdfTrueTypeFont(Server.MapPath("~/bin/Arial.ttf"), 10.0F)
textBoxField.Text = row.Cells(13).Text
End If
End If
Next
doc.SaveToFile(Server.MapPath("~/pdf/" + row.Cells(1).Text + ".pdf"), FileFormat.PDF)
Next