Fill an Acroform using Struts2 actions

URL : http:localhost:8080/webapp/package/AccountPDF?id=2184515

public class AccountPDFAction extends ActionSupport implements ServletResponseAware
{
    private InputStream documentStream;
    private HttpServletResponse servletResponse;
    private Account account;
   
    public InputStream getDocumentStream() {
        return documentStream;
    }
    public HttpServletResponse getServletResponse() {
        return servletResponse;
    }
    public void setServletResponse(HttpServletResponse response) {
        this.servletResponse = response;
    }   
    public void setAccount(Account account) {
        this.account = account;
    }
    public Account getAccount() {
        return account;
    }
   
    @Override
    public String execute() throws Exception
    {
        String result = super.execute();
        String id_s = getRequest().getParameter(“id”);
        Long id = Long.parseLong(id_s);

        try
        {
            account = (Account)getHibernateSession()
                .createCriteria(Account.class)
                .add(Restrictions.eq(Account.FIELD_ID, id))
                .uniqueResult();
           
            PdfReader reader = new PdfReader(“Account.pdf”);
            PdfStamper stamper = new PdfStamper(reader, getServletResponse().getOutputStream());
            AcroFields form = stamper.getAcroFields();
           
            // Static field
            form.setField(“title”, “ACCOUNT SUMMARY”);
            // String field
            form.setField(“first_name”, (account.getFirstName() != null) ? account.getFirstName() : “”);
            // int field
            form.setField(“age”, account.getAge() > 0 ? String.valueOf(account.getAge()) : “”);
            // Long field
            form.setField(“initial_balance”, (account.getBalance() != null) ? account.getBalance().toString() : “”);
            // Date field
            form.setField(“creation_date”, (account.getCreation() != null) ? new SimpleDateFormat(“dd/MM/yyyy”).format(account.getCreation()) : “”);
           
            stamper.setFormFlattening(true);
            stamper.close();
            documentStream.close();
            result = SUCCESS;
           
        } catch(Exception e)
        {
            result = ERROR;
        }

        return result;
    }
}

<action name=”AccountPDF” class=”fr.package.AccountPDFAction”>
    <result name=”success” type=”stream”>
        <param name=”inputName”>documentStream</param>
        <param name=”contentType”>application/pdf</param>
        <param name=”bufferSize”>1024</param>
    </result>
    <result>/WEB-INF/pages/error.jsp</result>
</action>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: