Saving .xfdf as .pdf
I am currently using Excel to populate a PDF file with form fields. Everything works, but exports it as .xfdf. Does anyone know how I can save it as pdf instead?
FullFileName = FilePath & "Requisition - " & Trim(MyRecord.CompanyName) & " - " & _
Year & "-" & Month & "-" & Day & "-" & Hour & "-" & Minute & "-" & Seconds & ".xfdf"
Open FullFileName For Output As #1
Print #1, "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
Print #1, "<xfdf xmlns=""http://ns.adobe.com/xfdf/"" xml:space=""preserve"">"
Print #1, "<fields>"
Print #1, "<field name=""Submitted by""><value>" + Trim(MyRecord.RequestedBy) + "</value></field>"
Print #1, "<field name=""Job Name""><value>" + "Auto Cards" + "</value></field>"
Print #1, "<field name=""Shipping address""><value>" + Trim(MyRecord.StreetNumber) & " " & Trim(MyRecord.StreetName) & ", Unit " & Trim(MyRecord.Suite) & ", " & Trim(MyRecord.City) & ", " & Trim(MyRecord.Province) & ", " & Trim(MyRecord.PostalCode) + "</value></field>"
Print #1, "<field name=""Special Instructions""><value>" + "Print" + "</value></field>"
Print #1, "</fields>"
Print #1, "<f href=""..\Requisition_Fillable.pdf""/>"
Print #1, "</xfdf>"
Close #1 ' Close file.
source to share
XFDF is an XML flavor of FDF. FDF stands for Form Data Format. It is a file format for storing data without actual PDF content. FDF uses PDF syntax to store this data. XFDF uses XML.
You cannot "convert" an FDF or XFDF file to PDF because too much information is missing. You can import (X) FDF file to PDF file with corresponding AcroForm fields.
Please see the ImportXFDF example . For this example, I wrote a simple XFDF file: data.xfdf :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="Submitted by"><value>Santaaimonce</value></field>
<field name="Job name"><value>Developer</value></field>
<field name="Shipping address"><value>Alphabet Street
Minneapolis
Minnesota</value></field>
<field name="Special instructions"><value>Use iText to fill out interactive form using data stored in XFDF file.
If you are a C# developer, use iTextSharp.</value></field>
</fields>
<f href="Requisition_Fillable.pdf"/>
</xfdf>
This information is not enough to generate the PDF file, but as you can see there is a link to the file in the template format: Requisition_Fillable.pdf .
If you put data.xfdf
it Requisition_Fillable.pdf
in the same directory and you open data.xfdf
, you will see the following message:
Adobe Reader opened the XFDF file, found a link to the PDF template, opened the template and now asks if you want to import the data. If you click the Options button and you enable the import, you get the following result:
The problem you are facing is simple: you don't have a Requisition_Fillable.pdf file anywhere. You can also avoid people being able to change the filled data.
You can solve this problem by combining the XFDF file and its template programmatically:
protected void fillXfdf(String SRC, String XFDF, String DEST) throws IOException, DocumentException {
// We receive the XML bytes
XfdfReader xfdf = new XfdfReader(new FileInputStream(XFDF));
// We get the corresponding form
PdfReader reader = new PdfReader(SRC);
// We create an OutputStream for the new PDF
FileOutputStream baos = new FileOutputStream(DEST);
// Now we create the PDF
PdfStamper stamper = new PdfStamper(reader, baos);
// We alter the fields of the existing PDF
AcroFields fields = stamper.getAcroFields();
fields.setFields(xfdf);
// take away all interactivity
stamper.setFormFlattening(true);
// close the stamper
stamper.close();
reader.close();
}
Notice the following line:
stamper.setFormFlattening(true);
This will flatten the shape: the fields will no longer be fields, they will look like any other content. You get a regular PDF: xfdf_merged.pdf
Obviously this will only work if the form (in this case Requisition_Fillable.pdf
) matches the data in the XFDF stream. For example: my template will work with your data for "Submitted by"
and fields "Shipping address"
. It won't work for "Job Name"
and fields "Special Instructions"
because those fields don't exist in my template. In my template, these fields are named "Job Name"
and "Special Instructions"
; do you see lower case instead of upper case?
If you want to use my template, you will have to change these two field names either in the XFDF file or in the PDF template. Also: my example is written in Java. When working in VB, you will have to use the .NET version of iText called iTextSharp. An experienced VBA developer should have no problem porting my example from Java to VB. Just think of Java as if it were pseudocode.
DISCLAIMER: The above example requires iText. I am the CEO of iText Group and this example was taken from my book "iText in Action - Second Edition".
source to share