In this example, you will see how to set back ground color of pdf in struts2.
index.jsp
< html>< head>< title>SetBackGround</title></ head>< body>< a href="setbackground.action">SetBackGround</a></ body></ html> |
AddImageInPDF.java (Action Class)
package roseindia.action;import java.io.FileOutputStream; import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import com.lowagie.text.Document;import com.lowagie.text.Paragraph;import com.lowagie.text.Rectangle;import com.lowagie.text.pdf.PdfWriter;import com.opensymphony.xwork2.ActionSupport;public class SetBaclkGroundNAddTable extends ActionSupport { public String execute() throws Exception {Rectangle pageSize = new Rectangle(200, 200);pageSize.setBackgroundColor( new java.awt.Color(200, 100, 100));Document document = new Document(pageSize);ServletContext servletCxt = ServletActionContext.getServletContext(); String path = servletCxt.getRealPath( "/");String filePath = path + "PDFFile";PdfWriter.getInstance(document, new FileOutputStream(filePath+ "/BGColorInPDF.pdf"));document.open(); document.add( new Paragraph("Hello Roseindia..."));document.close(); return SUCCESS; }} |
struts.xml
< struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <package name="default" namespace="/" extends="struts-default"> <action name="setbackground" class="roseindia.action.SetBaclkGroundNAddTable"> <result>welcomepage/welcome.jsp</result> </action> </package></ struts> |
Welcome.jsp
< html>< head><title>Insert title here</title></head>< body>< p>Background of file successfully change...</p></ body></ html> |
Output
Advertisements
Ads
Ads