Import and Export into XML

Skip to end of metadata
Go to start of metadata
What is XML?

XML is an eXtensible Markup Language used to describe the documents containing structured information. Structured information contains different types of content like words, pictures etc. and some indication about the role that a particular type of content plays. For example, content in a section heading has a different meaning from that of content in a footnote.

A markup language is a mechanism to identify structures in a document. The XML specification defines a standard way to add markup to documents. The following list shows some key features of XML:

  • XML is a markup language like other markup languages such as HTML, WML etc.
  • XML was designed to describe or identify data
  • XML allows users to create their own tags
  • XML uses a Document Type Definition (DTD) or an XML Schema to specify rules for the data identification
  • XML with a DTD or XML Schema is designed to be self-descriptive
  • XML is recommended by W3C
PDF Forms and XML

Interactive forms are one of the best features of the PDF documents. PDF forms are more like your HTML forms. Like HTML forms, PDF forms also contain form fields like combo, textbox, radio button, checkbox etc. Users may enter data into these form fields for further processing.

Aspose.Pdf.Kit allows developers to export the values (data) of all of these form fields to a separate XML file. And whenever required, these values can also be imported back to the PDF form fields. These features of Aspose.Pdf.Kit are explained below with the help of examples.

Export Pdf Form Fields Into Xml

To export the contents of the PDF form fields, Aspose.Pdf.Kit provides a Form class and the path of the input PDF document is passed to its constructor as a parameter. Once then PDF document is loaded then ExportXml method of the Form class is called, which exports all values for the PDF form fields in the input PDF document to an XML file. The output XML file is passed to the ExportXml method as a FileStream instance. Please refer to the example given below for the demonstration purposes.

[Java]
String pdfIn = In_Template + "TestWithContent.pdf";

// Assign an input PDF document whose contents are needed to export
Form form = new Form(pdfIn);

// Export the values of all PDF form fields to the XML file
form.exportXml(OutPath+"Form_ExportXml.xml");

//  Release the object
form.close();
 
Import Xml Contents Into Pdf Form Fields

To import the values of the PDF form fields back to the PDF document, we will again make use of Form class. This time, we will pass the storage paths of an input PDF document and an output PDF document (which will be generated after values imported to it) to the constructor of the Form class. After that, ImportXml method of the Form class is called, which imports all values of the PDF form fields (stored in the XML file) back to the output PDF document. The input XML file is passed to the ImportXml method as a FileStream instance. After the values are exported to the output PDF document, Save method of the Form class is called to save the output PDF document with values added to it. Please refer to the example given below for the demonstration purposes.

[Java]
// Assign input and output PDF documents
form = new Form(In_Template + "test.pdf", OutPath + "Form_ImportXml_string.pdf");

// Select a xml file with contents of the form
String xmlFile = In_Path + "Xml.in.xml";

// Import the values from the XML to the output PDF form
form.importXml(xmlFile);

// save the output PDF document and release the object
form.close();
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.