Some PDF Forms no longer editable after prefilling in vb.net application

I created a vb.net app that prefills 10 Veterans forms. The program uses the itextSharp library. All of the forms are editable after prefilling on my computer but I have Adobe Reader/Writer licensed edition on my computer.

When I compiled the code and put the program on another computer that only has the free version of Adobe reader, some of the forms are no longer editable. I use this block of code on all forms.

using (PdfStamper stamper = new PdfStamper(new PdfReader(pathPDF), System.IO.File.OpenWrite(pathCreated)))
        {
            stamper.FormFlattening = false;
            stamper.AcroFields.Xfa.FillXfaForm(pathXml);

            stamper.Close();
        }

Is there a way of ensure the form is edible after prefilling?
waynerayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin HopkinsSenior Web DeveloperCommented:
Hello wayneray,
Assuming the original xfa was saved with the extended rights. Then you probably just need to create the stamper in append mode.
using (PdfStamper stamper = new PdfStamper(new PdfReader(pathPDF), System.IO.File.OpenWrite(pathCreated), '\0', true))
        {
            stamper.FormFlattening = false;
            stamper.AcroFields.Xfa.FillXfaForm(pathXml);

            stamper.Close();
        }

Open in new window


Hope this helps,
Dustin
1
waynerayAuthor Commented:
Dustin, thanks so much for your fast response. Appending sounds like a possible solution. Below is my code that works for some of the 10 forms. Some of the forms however are not editable after prefilling. How would I incorporate your code?


'Dim pdfTemplate As String = "c:\DAV\forms\VBA-21-22-ARE.pdf"
        Dim newFile As String = "c:\DAV\forms\prefilled\VBA-21-22-ARE.pdf"

        Dim pdfReader As New PdfReader(pdfTemplate)
        pdfReader.unethicalreading = True
        Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
            newFile, FileMode.Create))

        Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

        ' set form pdfFormFields

        pdfFormFields.SetField("nameofvet[0]", fullName)
        pdfFormFields.SetField("filenumber[0]", vafnum)
        pdfFormFields.SetField("nameofservice[0]", vetgroup)
        pdfFormFields.SetField("jobtitile[0]", claim)
        pdfFormFields.SetField("claimantname[0]", personpresent)
        pdfFormFields.SetField("Eveningphonenumber[0]", otherphone)
        pdfFormFields.SetField("daytime[0]", phone)
        pdfFormFields.SetField("ActiveService1", branchos)
        pdfFormFields.SetField("emailaddress[0]", email)
        pdfFormFields.SetField("SSNno[0]", ssn)


        pdfFormFields.SetField("address[0]", address2)
       

        Dim sTmp As String = "form 21-22 Completed for " + _
        pdfFormFields.GetField("NameUsed")
        MessageBox.Show(sTmp, "Finished")

        ' flatten the form to remove editting options, set it to false
        ' to leave the form open to subsequent manual edits



        pdfStamper.FormFlattening = False


        ' close the pdf
        pdfStamper.Close()
0
Dustin HopkinsSenior Web DeveloperCommented:
Sure, I had just made the change to the sample you provided, here is your updated code. Though I haven't tested it.
		Dim pdfTemplate As String = "c:\DAV\forms\VBA-21-22-ARE.pdf"
        Dim newFile As String = "c:\DAV\forms\prefilled\VBA-21-22-ARE.pdf"

        Dim pdfReader As New PdfReader(pdfTemplate)
        pdfReader.unethicalreading = True
        Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
            newFile, FileMode.Create), "\0", true)
        Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

        ' set form pdfFormFields

        pdfFormFields.SetField("nameofvet[0]", fullName)
        pdfFormFields.SetField("filenumber[0]", vafnum)
        pdfFormFields.SetField("nameofservice[0]", vetgroup)
        pdfFormFields.SetField("jobtitile[0]", claim)
        pdfFormFields.SetField("claimantname[0]", personpresent)
        pdfFormFields.SetField("Eveningphonenumber[0]", otherphone)
        pdfFormFields.SetField("daytime[0]", phone)
        pdfFormFields.SetField("ActiveService1", branchos)
        pdfFormFields.SetField("emailaddress[0]", email)
        pdfFormFields.SetField("SSNno[0]", ssn)


        pdfFormFields.SetField("address[0]", address2)
        

        Dim sTmp As String = "form 21-22 Completed for " + _
        pdfFormFields.GetField("NameUsed")
        MessageBox.Show(sTmp, "Finished")

        ' flatten the form to remove editting options, set it to false
        ' to leave the form open to subsequent manual edits



        pdfStamper.FormFlattening = False


        ' close the pdf
        pdfStamper.Close()

Open in new window


Here is the code I use for this purpose (converted from c# so it may not be perfect). Basically I read the template file into a byte array and pass it along with a list of key value pairs to this function, which does the stamping and returns the resulting byte array to either pass to a browser for download or to save in a file.
		Private Shared Function BuildDocument(templateFileBytes As Byte(), dataTokens As IEnumerable(Of KeyValuePair(Of String, String))) As Byte()
	Dim pdfReader = New PdfReader(templateFileBytes)
	Dim stream = New MemoryStream()
	Dim stamper = New PdfStamper(pdfReader, stream, "\0", True)
	For Each token As KeyValuePair(Of String, String) In dataTokens
		stamper.AcroFields.SetField(token.Key, token.Value)
	Next
	stamper.FormFlattening = False
	stamper.Close()
	pdfReader.Close()
	stream.Flush()
	stream.Close()
	Return stream.ToArray()

End Function

Open in new window



Hope this helps,
Dustin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
waynerayAuthor Commented:
Dustin, this modified part fixed my problem.

Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
            newFile, FileMode.Create), "\0", True)

Your are awesome. I know Disabled Vet group that really appreciates your help. The only reason I gave an "A" is because I couldn't put "A+".

Thanks and I will also consider incorporating your other suggestion.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.