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