Aspose Demos > Java Components > Aspose.Pdf.Kit for Java > Java > Export Form values to XML

Export Form values to XML - Aspose.Pdf.Kit

This demo shows, how to Export form Field values to XFDF document. Using ExportXfdf method in Form class of Aspose.Pdf.Kit component, developers can export the PDF Form Field's values into an FDF file.

For more related information, please visit Import and Export into XML

Click Execute Demo to see how demo takes PDF document exporting results into XFDF document and enabling user to download results of the process.

JAVA

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

package com.aspose.pdf.kit.demos.jsf;

import java.io.Serializable;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.aspose.pdf.kit.*;

public class ExportXFDF extends BaseDemo
{
    private final String OUTPUT_FILE = "online.xfdf";

    // Default Constructor
    public ExportXFDF() {
        setFilename(OUTPUT_FILE);
    }
   
    /**
     * Execute request
     *
     * @return success
     */
    public Boolean execute(HttpServletRequest request, HttpServletResponse response) {

        try
        {
            String path = "/resources/";

            //Call the custructer of Form.
            Form form = new Form(getFileName(request, path+"OnlineFilled.pdf"));
            form.exportXfdf(
                getFileName(request, path+"OnlineFilled.pdf"),
                getFileName(request, path+"Online.xfdf"));
            //Close the form.
            form.close();

            InputStream is = getFile(request, path + "Online.xfdf");

            ByteArrayOutputStream outstream = new ByteArrayOutputStream(1024);

            byte[] buffer = new byte[4096]; int len;
            while ( (len = is.read(buffer)) > 0) {
              outstream.write(buffer, 0, len);
            }
            outstream.close();
            setResponse(OUTPUT_FILE, outstream);
            return true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return false;
    }
}

JAVA

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

package com.aspose.pdf.kit.demos.jsf;

import java.io.Serializable;

import java.io.*;
import java.util.*;
import java.net.URL;

import javax.servlet.*;
import javax.servlet.http.*;

import java.text.SimpleDateFormat;

import javax.faces.context.FacesContext;

import com.icesoft.faces.context.ByteArrayResource;
import com.icesoft.faces.context.Resource;

import java.lang.reflect.*;

public abstract class BaseDemo
{
    // Default Constructor
    public BaseDemo() {
        initLicense(new com.aspose.pdf.kit.License());
    }

    public abstract Boolean execute(HttpServletRequest request, HttpServletResponse response);

    protected String getFileName(HttpServletRequest request, String absoluteName)
        throws java.io.FileNotFoundException
    {
        URL url = getClass().getClassLoader().getResource(absoluteName);
        if (null==url && null!=request)
        {
            String path =
                request.getRealPath(absoluteName).replace("\\", "/");

            System.err.println("Failed to load " + absoluteName);
            System.err.println("Loading file from " + path);

            return path;
        }

        String path = new File(url.getPath()).getAbsolutePath();
        //return url.toString();
        return path;
    }

    protected InputStream getFile(HttpServletRequest request, String absoluteName)
        throws java.io.FileNotFoundException
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream(absoluteName);
        if (null==is && null!=request)
        {
            String path =
                request.getRealPath(absoluteName).replace("\\", "/");

            System.err.println("Failed to load " + absoluteName);
            System.err.println("Loading file from " + path);
            is = new FileInputStream(path);
        }

        return is;
    }

    public String generate() {

        FacesContext context = FacesContext.getCurrentInstance();

        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

        try
        {
            System.out.println("BaseGenerate called for " + this.toString());

            if (execute(request, response))
            {
                //context.responseComplete();
            }


        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return "stay";
    }

    protected final String FILE_NAME = "result.pdf";

    public Resource getGeneratedfile() {
        byte[] res = getResult();
        if (null==res || res.length<1) {
            return null;
        }
        return new ByteArrayResource(res);
    }

    private String fileName = FILE_NAME;
    public String getFilename() {
        return fileName;
    }

    public void setFilename(String v) {

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MMM-dd_hh_mm_ss-", Locale.US);
        String curDate = formatter.format(new Date());

        this.fileName = curDate + v;
    }

    public  byte[] getResult()
    {
        return response;
    }

    protected void setResponse(String fName, ByteArrayOutputStream out) throws IOException {
        setFilename(fName);
        response = out.toByteArray();
        out.close();
        // outputStream.write(this.response);
    }
    protected byte[] response;

    /**
     *
     * Initializes license (if demo license is available).
     * you may insert your licensing code here that would use obj's
     * setLicense based on InputStream rather than passing it on
     * to 3rd-party class.
     *
     */
    public static void initLicense(Object obj) {
        try {

            Class myclass = Class.forName("com.aspose.demos.Common");
            if (null==myclass) {
                System.out.println("initLicense not found");
                return ;
            }

            //Use reflection to list methods and invoke them
            Method[] methods = myclass.getMethods();
            Object object = myclass.newInstance();

            Class partypes[] = new Class[1];
            partypes[0] = Object.class;
           
            Method meth = myclass.getMethod("initLicense", partypes);
            if (null!=meth) {

                    System.out.println("Calling " + meth.getName());
                    Object arglist[] = new Object[1];
                    arglist[0] = obj;

                    meth.invoke(object, arglist);
            }
        } catch (ClassNotFoundException cnfe) {
            // Ignore
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

XHTML

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ice="http://www.icesoft.com/icefaces/component"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jstl/core"
      >

<ui:composition template="/WEB-INF/includes/templates/page-template.xhtml">
                        
    <ui:define name="pageTitle">
        Export XFDF - Aspose.Pdf Demos
    </ui:define>

    <ui:define name="page-content">
        <ui:decorate template="/WEB-INF/includes/templates/tabbed_container.xhtml">
            <ui:define name="example">

   

<table width="90%" align="center" cellspacing="0" cellpadding="0" border="0">
  <tr>
    <td width="19"><img src="../../../../Common/images/heading_lft.jpg" alt="" width="19" height="41" /></td>
    <td width="100%" class="demos-heading-bg"><h2 class="demos-heading-bg"> &nbsp;Export Form values to XML - Aspose.Pdf.Kit</h2></td>
    <td width="19"><img src="../../../../Common/images/heading_rt.jpg" alt="" width="19" height="41" /></td>
  </tr>
</table>

 <p class="componentDescriptionTxt"> This demo shows, how to <b> Export form Field</b> values to XFDF document.

 Using <b> ExportXfdf </b> method in <a href="http://www.aspose.com/documentation/java-components/aspose.pdf.kit-for-java/com/aspose/pdf/kit/form.html"> Form </a> class of Aspose.Pdf.Kit component, developers can export the PDF Form Field's values into an FDF file.

<br/><br/>
For more related information, please visit <a href="http://www.aspose.com/documentation/java-components/aspose.pdf.kit-for-java/import-and-export-into-xml.html"> Import and Export into XML </a>

<br/> <br/> Click <b> Execute Demo </b> to see how demo takes <a href="../resources/OnlineFilled.pdf"> PDF document </a> 
        exporting results into XFDF document and enabling user to download results
        of the process.
        </p>

            <h:commandButton id="generate" 
                action="#{mycontext.generate}"
                actionListener="#{mycontext.action}"
                value="Execute Demo"> 
                <f:attribute name="demo" value="exportxfdf" />
            </h:commandButton>

            <ice:outputResource id="outputResource"
                mimeType="application/text"
                resource="#{mycontext.generatedfile}"
                fileName="#{mycontext.filename}"
                shared="false" />

            </ui:define>
        </ui:decorate>
    <br/><br/><br/><br/><br/><br/><br/></ui:define>
</ui:composition>
</html>