Home Struts Struts Struts2.2.1 How to set pdf background color.

Ask Questions?

View Latest Questions


How to set pdf background color.
Posted on: February 17, 2011 at 12:00 AM
Here, we are going to introduce you to how to set background of pdf in struts.

How to set pdf background color.

In this example, you will see how to set back ground color of pdf in struts2. 







<a href="setbackground.action">SetBackGround</a>



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.add(new Paragraph("Hello Roseindia..."));


return SUCCESS; }




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

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

<action name="setbackground" class="roseindia.action.SetBaclkGroundNAddTable">







<head><title>Insert title here</title></head>


<p>Background of file successfully change...</p>




Download Source Code

Related Tags for How to set pdf background color.: