自动填写 I-9 PDF XFA 表格
Auto-Fill I-9 PDF XFA Form
早上好。我希望有人可以帮助我解决这个问题。去年,我使用 iTextSharp
设置了一个 VB.NET 程序,用户可以在其中输入信息以填写 I9,然后该信息将填写 PDF 并打印。使用新的 I9,我遇到了不明问题。
首先,代码不会出错。我只是得到了一个糟糕的结果,因为我得到的不是填写的表格,而是一个显示 "The document you are trying to load requires Adobe Reader 8 or higher. You may not have the Adobe Reader installed..." 等的 PDF。因此,我确保我拥有最新的 Reader 版本,再次尝试并得到相同的结果。
考虑到字段名称结构可能发生了变化,我尝试像第一次那样读入 format/fields。 (下面的代码)。但是,现在它告诉我没有要读取的字段 (AcroFields.Fields.Count = 0
)。
Private Sub ListFieldNames(pdfTemplate As String)
Dim pdfTemplate As String = "c:\Temp\PDF\fw4.pdf"
Dim pdfReader As PdfReader = New PdfReader(pdfTemplate)
Dim de As KeyValuePair(Of String, iTextSharp.text.pdf.AcroFields.Item)
For Each de In pdfReader.AcroFields.Fields
Console.WriteLine(de.Key.ToString())
Next
End Sub
因此,我开始进行一些搜索,并找到了对他们可以切换到的另一种 PDF 结构的引用; XFA。老实说,我仍然没有找到令人满意的 documentation/samples,但我确实找到了一些代码,看起来应该可以读取 XFA PDF 的结构。 (下面的代码)。我在这里尝试了两种不同的方法。第一个本质上表明 xfaFields 中没有 xmlNode。第二个确实找到了一个名为 "data" 的节点(这是它找到的唯一一个节点),但没有找到任何子节点。
Private Sub ReadXfa(pdfTemplate As String)
pdfReader.unethicalreading = True
Dim readerPDF As New PdfReader(pdfTemplate)
Dim xfaFields = readerPDF.AcroFields.Xfa.DatasetsSom.Name2Node
For Each xmlNode In xfaFields
Console.WriteLine(xmlNode.Value.Name + ":" + xmlNode.Value.InnerText)
Next
'Example of how to get a field value
' Dim lastName = xfaFields.First(Function(a) a.Value.Name = "textFieldLastNameGlobal").Value.InnerText
Dim reader As New PdfReader(pdfTemplate)
Dim xfa As New XfaForm(reader)
Dim node As XmlNode = xfa.DatasetsNode()
Dim list As XmlNodeList = node.ChildNodes()
For i As Integer = 0 To list.Count - 1
Console.WriteLine(list.Item(i).LocalName())
If "data".Equals(list.Item(i).LocalName()) Then
node = list.Item(i)
Exit For
End If
Next
list = node.ChildNodes()
For i As Integer = 0 To list.Count - 1
Console.WriteLine(list.Item(i).LocalName())
Next
reader.Close()
End Sub
https://www.uscis.gov/system/files_force/files/form/i-9.pdf?download=1
以上link转到政府提供的i9 PDF
所以...我想我有多个问题。最简单的是,如果有人做过这个 process/if 他们可以帮助我。除此之外,如果有人能为我指明如何从这个新 PDF 文件 read/write 的正确方向,那就太好了。坦率地说,我什至不确定如何确定他们使用的 "type" 形式 - AcroField
、XFA
、其他?
非常感谢您的 time/help!
首先,抱歉我不再做vb.net,但您应该可以转换后面的代码。
您已经发现新表格是 XFA。有一种简单的非编程方式可以查看表单字段和数据。您注意到您升级了 Adobe Reader 版本,所以我猜您正在使用 Reader DC。从菜单选项:
Edit => Form Options => Export Data...
这会将表单导出到您可以检查的 XML
文件。 XML 文件提示您需要相应的 XML 文档来填写表单,这与使用 AcroForm 的方式完全不同。
这里有一些简单的代码可以帮助您入门。首先是读取空白 XML 文档并更新它的方法:
public string FillXml(Dictionary<string, string> fields)
{
// XML_INFILE => physical path to XML file exported from I-9
XDocument xDoc = XDocument.Load(XML_INFILE);
foreach (var kvp in fields)
{
// handle multiple elements in I-9 form
var elements = xDoc.XPathSelectElements(
string.Format("//{0}", kvp.Key)
);
if (elements.Count() > 0)
{
foreach (var e in elements) { e.Value = kvp.Value; }
}
}
return xDoc.ToString();
}
现在我们有了创建有效 XML 的方法,用一些示例数据填充表单字段:
var fields = new Dictionary<string, string>()
{
{ "textFieldLastNameGlobal", "Doe" },
{ "textFieldFirstNameGlobal", "Jane" }
};
var filledXml = FillXml(fields);
using (var ms = new MemoryStream())
{
// PDF_READER => I-9 PdfReader instance
using (PDF_READER)
{
// I-9 has password security
PdfReader.unethicalreading = true;
// maintain usage rights on output file
using (var stamper = new PdfStamper(PDF_READER, ms, '[=12=]', true))
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(filledXml);
stamper.AcroFields.Xfa.FillXfaForm(doc.DocumentElement);
}
}
File.WriteAllBytes(OUTFILE, ms.ToArray());
}
要回答您的最后一个问题,即如何确定形式 'type',请像这样使用 PdfReader
实例:
PDF_READER.AcroFields.Xfa.XfaPresent
true
表示 XFA,false
表示 AcroForm。
这是我的最终代码,以防有人可以使用它...我确实有一个 On Error Resume Next 到位,因为 i9 是一个非常挑剔的表格,我选择以稍微不同的方式填写一些东西比他们要我做的还要多。我还切掉了我设置一些变量的地方,以使其更短。再次感谢kuujinbo的帮助!
Private Sub ExportI9()
Dim pdfTemplate As String = Path.Combine(Application.StartupPath, "PDFs17-I9.pdf")
pdfTemplate = Replace(pdfTemplate, "bin\Debug\", "")
Dim fields = New Dictionary(Of String, String)() From {
{"textFieldLastNameGlobal", Me.tbLast.Text},
{"textFieldFirstNameGlobal", Me.tbFirst.Text},
{"textFieldMiddleInitialGlobal", Mid(Me.tbMiddle.Text, 1, 1)},
{"textFieldOtherNames", Me.tbOtherName.Text},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldAddress", addr1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldAptNum", ""},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldCityOrTown", city1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/State", state1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldZipCode", zip1},
{"dateFieldBirthDate", Me.dtpBirth.Value},
{"SSN", Me.tbSSN.Text},
{"fieldEmail", ""},
{"fieldPhoneNum", sphone},
{"radioButtonListCitizenship", citizenship},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subCitizenshipStatus/textFieldResidentType", alienuscis},
{"dateAlienAuthDate", dauth},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subAuthorizedAlien/numFormI94Admission", Me.tbi94.Text},
{"numForeignPassport", Me.tbPassport.Text},
{"CountryofIssuance", Me.tbPassportCountry.Text},
{"numAlienOrUSCIS", usc},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subAuthorizedAlien/textFieldResidentType", alienuscis},
{"rbListPerparerOrTranslator", 3},
{"dropdownMultiPreparerOrTranslator", 1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow2/textFieldFirstName", prepfirst},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow2/textFieldLastName", preplast},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/textFieldAddress", Replace(prepadd, "#", "No. ")},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/textFieldCityOrTown", prepcity},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/State", prepstate},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/textFieldZipCode", prepzip},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/selectListA1DocumentTitle", doctitle1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/selectListBDocumentTitle", doctitle2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/selectListCDocumentTitle", doctitle3},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/textFieldIssuingAuthority", issued1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/textFieldIssuingAuthority", issued2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/textFieldIssuingAuthority", issued3},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/dateExpiration", expdate1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/dateExpiration", expdate2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/dateExpiration", expdate3},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/textFieldDocumentNumber", docnum1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/textFieldDocumentNumber", docnum2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/textFieldDocumentNumber", docnum3},
{"form1/section2and3Page2/subSection2/subCertification/subAttest/dateEmployeesFirstDay", CDate(Me.dtpHired.Value).ToShortDateString},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow2/textFieldLastName", certlast},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow2/textFieldFirstName", certfirst},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/textFieldAddress", orgadd},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/textFieldCityOrTown", orgcity},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/State", orgstate},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/textFieldZipCode", orgzip},
{"textBusinessOrgName", orgname}
}
Dim PDFUpdatedFile As String = pdfTemplate
PDFUpdatedFile = Replace(PDFUpdatedFile, "I9", Me.tbSSN.Text & "-I9")
If System.IO.File.Exists(PDFUpdatedFile) Then System.IO.File.Delete(PDFUpdatedFile)
Dim readerPDF As New PdfReader(pdfTemplate)
Dim filledXml = FillXml(fields)
Using ms = New MemoryStream()
Using readerPDF
' I-9 has password security
PdfReader.unethicalreading = True
Dim stamper As New PdfStamper(readerPDF, ms, ControlChars.NullChar, True)
Using stamper
Dim doc As New XmlDocument()
doc.LoadXml(filledXml)
stamper.AcroFields.Xfa.FillXfaForm(doc.DocumentElement)
End Using
End Using
File.WriteAllBytes(PDFUpdatedFile, ms.ToArray())
End Using
End Sub
Public Function FillXml(fields As Dictionary(Of String, String)) As String
' XML_INFILE => physical path to XML file exported from I-9
Dim xmlfile As String
xmlfile = Path.Combine(Application.StartupPath, "PDFs17-I9_data.xml")
xmlfile = Replace(xmlfile, "bin\Debug\", "")
Dim kvp As KeyValuePair(Of String, String)
Dim xDoc As XDocument = XDocument.Load(xmlfile)
For Each kvp In fields
' handle multiple elements in I-9 form
Dim elements = xDoc.XPathSelectElements(String.Format("//{0}", kvp.Key))
If elements.Count() > 0 Then
For Each e As XElement In elements
On Error Resume Next
e.Value = kvp.Value
Next
End If
Next
Return xDoc.ToString()
End Function
早上好。我希望有人可以帮助我解决这个问题。去年,我使用 iTextSharp
设置了一个 VB.NET 程序,用户可以在其中输入信息以填写 I9,然后该信息将填写 PDF 并打印。使用新的 I9,我遇到了不明问题。
首先,代码不会出错。我只是得到了一个糟糕的结果,因为我得到的不是填写的表格,而是一个显示 "The document you are trying to load requires Adobe Reader 8 or higher. You may not have the Adobe Reader installed..." 等的 PDF。因此,我确保我拥有最新的 Reader 版本,再次尝试并得到相同的结果。
考虑到字段名称结构可能发生了变化,我尝试像第一次那样读入 format/fields。 (下面的代码)。但是,现在它告诉我没有要读取的字段 (AcroFields.Fields.Count = 0
)。
Private Sub ListFieldNames(pdfTemplate As String)
Dim pdfTemplate As String = "c:\Temp\PDF\fw4.pdf"
Dim pdfReader As PdfReader = New PdfReader(pdfTemplate)
Dim de As KeyValuePair(Of String, iTextSharp.text.pdf.AcroFields.Item)
For Each de In pdfReader.AcroFields.Fields
Console.WriteLine(de.Key.ToString())
Next
End Sub
因此,我开始进行一些搜索,并找到了对他们可以切换到的另一种 PDF 结构的引用; XFA。老实说,我仍然没有找到令人满意的 documentation/samples,但我确实找到了一些代码,看起来应该可以读取 XFA PDF 的结构。 (下面的代码)。我在这里尝试了两种不同的方法。第一个本质上表明 xfaFields 中没有 xmlNode。第二个确实找到了一个名为 "data" 的节点(这是它找到的唯一一个节点),但没有找到任何子节点。
Private Sub ReadXfa(pdfTemplate As String)
pdfReader.unethicalreading = True
Dim readerPDF As New PdfReader(pdfTemplate)
Dim xfaFields = readerPDF.AcroFields.Xfa.DatasetsSom.Name2Node
For Each xmlNode In xfaFields
Console.WriteLine(xmlNode.Value.Name + ":" + xmlNode.Value.InnerText)
Next
'Example of how to get a field value
' Dim lastName = xfaFields.First(Function(a) a.Value.Name = "textFieldLastNameGlobal").Value.InnerText
Dim reader As New PdfReader(pdfTemplate)
Dim xfa As New XfaForm(reader)
Dim node As XmlNode = xfa.DatasetsNode()
Dim list As XmlNodeList = node.ChildNodes()
For i As Integer = 0 To list.Count - 1
Console.WriteLine(list.Item(i).LocalName())
If "data".Equals(list.Item(i).LocalName()) Then
node = list.Item(i)
Exit For
End If
Next
list = node.ChildNodes()
For i As Integer = 0 To list.Count - 1
Console.WriteLine(list.Item(i).LocalName())
Next
reader.Close()
End Sub
https://www.uscis.gov/system/files_force/files/form/i-9.pdf?download=1
以上link转到政府提供的i9 PDF
所以...我想我有多个问题。最简单的是,如果有人做过这个 process/if 他们可以帮助我。除此之外,如果有人能为我指明如何从这个新 PDF 文件 read/write 的正确方向,那就太好了。坦率地说,我什至不确定如何确定他们使用的 "type" 形式 - AcroField
、XFA
、其他?
非常感谢您的 time/help!
首先,抱歉我不再做vb.net,但您应该可以转换后面的代码。
您已经发现新表格是 XFA。有一种简单的非编程方式可以查看表单字段和数据。您注意到您升级了 Adobe Reader 版本,所以我猜您正在使用 Reader DC。从菜单选项:
Edit => Form Options => Export Data...
这会将表单导出到您可以检查的 XML
文件。 XML 文件提示您需要相应的 XML 文档来填写表单,这与使用 AcroForm 的方式完全不同。
这里有一些简单的代码可以帮助您入门。首先是读取空白 XML 文档并更新它的方法:
public string FillXml(Dictionary<string, string> fields)
{
// XML_INFILE => physical path to XML file exported from I-9
XDocument xDoc = XDocument.Load(XML_INFILE);
foreach (var kvp in fields)
{
// handle multiple elements in I-9 form
var elements = xDoc.XPathSelectElements(
string.Format("//{0}", kvp.Key)
);
if (elements.Count() > 0)
{
foreach (var e in elements) { e.Value = kvp.Value; }
}
}
return xDoc.ToString();
}
现在我们有了创建有效 XML 的方法,用一些示例数据填充表单字段:
var fields = new Dictionary<string, string>()
{
{ "textFieldLastNameGlobal", "Doe" },
{ "textFieldFirstNameGlobal", "Jane" }
};
var filledXml = FillXml(fields);
using (var ms = new MemoryStream())
{
// PDF_READER => I-9 PdfReader instance
using (PDF_READER)
{
// I-9 has password security
PdfReader.unethicalreading = true;
// maintain usage rights on output file
using (var stamper = new PdfStamper(PDF_READER, ms, '[=12=]', true))
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(filledXml);
stamper.AcroFields.Xfa.FillXfaForm(doc.DocumentElement);
}
}
File.WriteAllBytes(OUTFILE, ms.ToArray());
}
要回答您的最后一个问题,即如何确定形式 'type',请像这样使用 PdfReader
实例:
PDF_READER.AcroFields.Xfa.XfaPresent
true
表示 XFA,false
表示 AcroForm。
这是我的最终代码,以防有人可以使用它...我确实有一个 On Error Resume Next 到位,因为 i9 是一个非常挑剔的表格,我选择以稍微不同的方式填写一些东西比他们要我做的还要多。我还切掉了我设置一些变量的地方,以使其更短。再次感谢kuujinbo的帮助!
Private Sub ExportI9()
Dim pdfTemplate As String = Path.Combine(Application.StartupPath, "PDFs17-I9.pdf")
pdfTemplate = Replace(pdfTemplate, "bin\Debug\", "")
Dim fields = New Dictionary(Of String, String)() From {
{"textFieldLastNameGlobal", Me.tbLast.Text},
{"textFieldFirstNameGlobal", Me.tbFirst.Text},
{"textFieldMiddleInitialGlobal", Mid(Me.tbMiddle.Text, 1, 1)},
{"textFieldOtherNames", Me.tbOtherName.Text},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldAddress", addr1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldAptNum", ""},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldCityOrTown", city1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/State", state1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Top/subEmployeeInfo/subSection1Row2/textFieldZipCode", zip1},
{"dateFieldBirthDate", Me.dtpBirth.Value},
{"SSN", Me.tbSSN.Text},
{"fieldEmail", ""},
{"fieldPhoneNum", sphone},
{"radioButtonListCitizenship", citizenship},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subCitizenshipStatus/textFieldResidentType", alienuscis},
{"dateAlienAuthDate", dauth},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subAuthorizedAlien/numFormI94Admission", Me.tbi94.Text},
{"numForeignPassport", Me.tbPassport.Text},
{"CountryofIssuance", Me.tbPassportCountry.Text},
{"numAlienOrUSCIS", usc},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subAuthorizedAlien/textFieldResidentType", alienuscis},
{"rbListPerparerOrTranslator", 3},
{"dropdownMultiPreparerOrTranslator", 1},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow2/textFieldFirstName", prepfirst},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow2/textFieldLastName", preplast},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/textFieldAddress", Replace(prepadd, "#", "No. ")},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/textFieldCityOrTown", prepcity},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/State", prepstate},
{"form1/section1Page1/subSection1PositionWrapper/subSection1Bottom/subPreparerTranslator/subPrepererTranslator1/subTranslatorSignature/subRow3/textFieldZipCode", prepzip},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/selectListA1DocumentTitle", doctitle1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/selectListBDocumentTitle", doctitle2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/selectListCDocumentTitle", doctitle3},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/textFieldIssuingAuthority", issued1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/textFieldIssuingAuthority", issued2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/textFieldIssuingAuthority", issued3},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/dateExpiration", expdate1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/dateExpiration", expdate2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/dateExpiration", expdate3},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subDocListA1/textFieldDocumentNumber", docnum1},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListB/textFieldDocumentNumber", docnum2},
{"form1/section2and3Page2/subSection2/subVerificationListsBorder/subListBandCBorder/subDocListC/textFieldDocumentNumber", docnum3},
{"form1/section2and3Page2/subSection2/subCertification/subAttest/dateEmployeesFirstDay", CDate(Me.dtpHired.Value).ToShortDateString},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow2/textFieldLastName", certlast},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow2/textFieldFirstName", certfirst},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/textFieldAddress", orgadd},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/textFieldCityOrTown", orgcity},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/State", orgstate},
{"form1/section2and3Page2/subSection2/subCertification/subEmployerInformation/subEmployerInfoRow3/textFieldZipCode", orgzip},
{"textBusinessOrgName", orgname}
}
Dim PDFUpdatedFile As String = pdfTemplate
PDFUpdatedFile = Replace(PDFUpdatedFile, "I9", Me.tbSSN.Text & "-I9")
If System.IO.File.Exists(PDFUpdatedFile) Then System.IO.File.Delete(PDFUpdatedFile)
Dim readerPDF As New PdfReader(pdfTemplate)
Dim filledXml = FillXml(fields)
Using ms = New MemoryStream()
Using readerPDF
' I-9 has password security
PdfReader.unethicalreading = True
Dim stamper As New PdfStamper(readerPDF, ms, ControlChars.NullChar, True)
Using stamper
Dim doc As New XmlDocument()
doc.LoadXml(filledXml)
stamper.AcroFields.Xfa.FillXfaForm(doc.DocumentElement)
End Using
End Using
File.WriteAllBytes(PDFUpdatedFile, ms.ToArray())
End Using
End Sub
Public Function FillXml(fields As Dictionary(Of String, String)) As String
' XML_INFILE => physical path to XML file exported from I-9
Dim xmlfile As String
xmlfile = Path.Combine(Application.StartupPath, "PDFs17-I9_data.xml")
xmlfile = Replace(xmlfile, "bin\Debug\", "")
Dim kvp As KeyValuePair(Of String, String)
Dim xDoc As XDocument = XDocument.Load(xmlfile)
For Each kvp In fields
' handle multiple elements in I-9 form
Dim elements = xDoc.XPathSelectElements(String.Format("//{0}", kvp.Key))
If elements.Count() > 0 Then
For Each e As XElement In elements
On Error Resume Next
e.Value = kvp.Value
Next
End If
Next
Return xDoc.ToString()
End Function