问题
The workflow is like this:
- we download a template form, prefill values which will be static, export a XML template
- the XML is parsed with a .NET forms app, dynamic values are added
- the resulting XML needs to be imported back into the PDF template
All goes well using the MergeXfaData method on iTextSharp, however for some reason, date/time fields are not being filled in (textfields and checkboxes work ok). Cannot figure out why. Searching through forums I discovered someone saying XFA only works for textfields. Why is this and how to work around?
Also as a next step I will need to attach an attachment to the PDF form. The attachments are also PDF but iTextSharp would not attach them. Have searched many forums but none of the mentioned methods works for me.
Thank you for your answers
回答1:
@Yuri, I just tried it with a date/time field and it worked perfectly. I create a simple PDF in Live Cycle with two fields, 1 text and 1 date/time, and two buttons, Submit and Print. The sample PDF is here:
I filled out the form and got this for XML:
<?xml version="1.0" encoding="UTF-8"?>
<topmostSubform>
<Text1>Chris</Text1>
<DateTimeField1>2012-04-12</DateTimeField1>
</topmostSubform>
When I imported it into the PDF and viewed the PDF in Acrobat the date/time field was filled out. I'm using iTextSharp 5.0.5.0 with the following code:
Option Explicit On
Option Strict On
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim PDF_Input_File As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Input.pdf")
Dim PDF_Output_File As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Export.pdf")
''//Create our reader
Dim reader As New PdfReader(PDF_Input_File)
''//Create our file stream to output to
Using FS As New System.IO.FileStream(PDF_Output_File, FileMode.Create, FileAccess.Write, FileShare.Read)
''//Create the stamper
Dim stamper As New PdfStamper(reader, FS)
''//Just loading the XML raw instead of reading from disk, less files to attach to the post
Dim Bytes = System.Text.Encoding.UTF8.GetBytes("<?xml version=""1.0"" encoding=""UTF-8""?><topmostSubform><Text1>Chris</Text1><DateTimeField1>2012-04-12</DateTimeField1></topmostSubform>")
Using MS As New MemoryStream(Bytes)
''//Fill out the form
stamper.AcroFields.Xfa.FillXfaForm(MS)
End Using
stamper.Close()
End Using
reader.Close()
Me.Close()
End Sub
End Class
来源:https://stackoverflow.com/questions/5637151/itextsharp-xfa-fill-date-time-field