Home Struts Struts Struts2.2.1 How to insert image in PDF file in struts2

Ask Questions?

View Latest Questions


 
 

How to insert image in PDF file in struts2
Posted on: February 17, 2011 at 12:00 AM
Advertisement
In this example, you will see how to insert image in pdf file.

How to insert image in PDF file in struts2

In this example, we will see how to insert image in PDF file using struts2.2.1 framework..

index.jsp

<html>

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

<body>

<a href="addImage.action">Add Image in PDF....</a>

</body>

</html>

AddImageInPDF.java (Action Class)

package net.roseindia.action;

import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.lowagie.text.Document;

import com.lowagie.text.Image;

import com.lowagie.text.pdf.PdfWriter;

import com.opensymphony.xwork2.ActionSupport;

public class AddImageInPDF extends ActionSupport {

@Override

public String execute() throws Exception {

Document doc = new Document();

ServletContext servletcontext = ServletActionContext

.getServletContext();

String path = servletcontext.getRealPath("/");

String filepath = path + "pdf" + "/AddImageInPDF.pdf";

String imagePath = path + "images" + "/roseindialogo.jpeg";

System.out.println(path + "pdf" + "\roseindialogo.jpeg");

PdfWriter.getInstance(doc, new FileOutputStream(filepath));

Image image = Image.getInstance(imagePath);

doc.open();

doc.add(image);

doc.close();

return SUCCESS; }

}

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="addImage" class="net.roseindia.action.AddImageInPDF">

<result>Welcome.jsp</result> </action>

</package>

</struts>

Welcome.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><title>Insert title here</title></head>

<body>Image successfully added in pdf.</body>

</html>

Output

Download Source Code


Related Tags for How to insert image in PDF file in struts2:


DMCA.com