Home Struts Struts Struts2.2.1 How to create pdf file in struts2.

Ask Questions?

View Latest Questions


 
 

How to create pdf file in struts2.
Posted on: February 16, 2011 at 12:00 AM
Advertisement
In this section, you will see how to create pdf file in struts2.

How to create pdf file in struts2.

In this example, you will see the how to create PDF in struts, and how to write message in generated PDF file in struts2. 

For this, we requires a jar file iText.jar. It is used in java for generating PDF.

index.jsp

<html>

<head>

<meta http-equiv="Refresh" content="0;URL=message.action">

<title>Generate_PDF_in_Struts</title>

</head>

<body><h1>Generate_PDF_in_Struts</h1><hr>

<h2 style="color: green;">Loading.....</h2>

</body>

</html>

MessageJSp.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC 

"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html><head><title>Generate_PDF_in_Struts</title></head>

<body><h1>Generate_PDF_in_Struts</h1><hr>

<s:form action="generatepdf.action">

<s:textarea name="message" label="Write Message In PDF :"></s:textarea>

<s:submit></s:submit> </s:form>

</body>

</html>

GeneratePDFAction.java (Action Class)

package net.roseindia.action;

import java.io.*;

import net.roseindia.model.MessageInPDF;

import com.lowagie.text.Document;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

public class GeneratePDFAction extends ActionSupport implements ModelDriven {

MessageInPDF obMessage;

public String execute() throws Exception {

String string = obMessage.getMessage();

Document document = new Document();

PdfWriter.getInstance(document, new FileOutputStream(

"c:\\StrutsExample.pdf"));

document.open();

document.add(new Paragraph(string));

document.close();

return SUCCESS; }

@Override

public Object getModel() {

obMessage = new MessageInPDF();

// TODO Auto-generated method stub

return obMessage; }

}

MessageInPDF.java (Model)

package net.roseindia.model;

public class MessageInPDF {

private String message;

public String getMessage() {

return message; }

public void setMessage(String message) {

this.message = message; }

}

struts.xml

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

 

<package name="default" namespace="/" extends="struts-default">

<action name="message">

<result>/jsp/MessageJSp.jsp</result>

</action>

<action name="generatepdf" class="net.roseindia.action.GeneratePDFAction">

<result name="input">/jsp/MessageJSp.jsp</result>

<result>jsp/success.jsp</result>

</action>

</package>

 

</struts>

RegistrationSuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC 

"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Success page </title></head>

<body><h1>Generate_PDF_in_Struts</h1><hr>

<h3 style="color: green;">PDF successfully Generated......</h3>

</body>

</html

Output

Download Source Code


Related Tags for How to create pdf file in struts2.:


DMCA.com