In this example, we will see how to insert image in PDF file using struts2.2.1 framework..
index.jsp
<html>ADS_TO_REPLACE_1 <head><title>Insert title here</title></head> <body> <a href="addImage.action">Add Image in PDF....</a>ADS_TO_REPLACE_2 </body> </html> |
AddImageInPDF.java (Action Class)
package net.roseindia.action; import java.io.FileOutputStream;ADS_TO_REPLACE_3 import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.lowagie.text.Document;ADS_TO_REPLACE_4 import com.lowagie.text.Image; import com.lowagie.text.pdf.PdfWriter; import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_5 public class AddImageInPDF extends ActionSupport { @Override public String execute() throws Exception {ADS_TO_REPLACE_6 Document doc = new Document(); ServletContext servletcontext = ServletActionContext .getServletContext();ADS_TO_REPLACE_7 String path = servletcontext.getRealPath("/"); String filepath = path + "pdf" + "/AddImageInPDF.pdf"; String imagePath = path + "images" + "/roseindialogo.jpeg";ADS_TO_REPLACE_8 System.out.println(path + "pdf" + "\roseindialogo.jpeg"); PdfWriter.getInstance(doc, new FileOutputStream(filepath)); Image image = Image.getInstance(imagePath);ADS_TO_REPLACE_9 doc.open(); doc.add(image); doc.close();ADS_TO_REPLACE_10 return SUCCESS; } } |
struts.xml
<struts>ADS_TO_REPLACE_11 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default">ADS_TO_REPLACE_12 <action name="addImage" class="net.roseindia.action.AddImageInPDF"> <result>Welcome.jsp</result> </action> </package>ADS_TO_REPLACE_13 </struts> |
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>ADS_TO_REPLACE_14 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>ADS_TO_REPLACE_15 <head><title>Insert title here</title></head> <body>Image successfully added in pdf.</body> </html> |
OutputADS_TO_REPLACE_16
ADS_TO_REPLACE_17
Advertisements
Ads
Ads