(网上资料)
package **.**.**.action;
import java.awt.Color;
import java.io.FileOutputStream;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.easydone.util.ParamUtil;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* <p>Title: 本程序演示一个 action 怎样操作PDF文档</p>
* <p>Description: 所用开源包:itext-1.4.jar,iTextAsian.jar</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: 聚能易成</p>
* @author dirboy
* @version 1.0
*/
public class PdfAction extends Action {
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//创建一个文档对象,设置文档的页面大小
Document document = null;
String pdfName = (new Random()).nextInt()+".pdf";
String pdf = ParamUtil.getStr((String)request.getParameter("pdf"));
String sysPath = request.getRealPath("/");
try {
Rectangle pSize=new Rectangle(650,500);
//Rectangle rectPageSize = new Rectangle(PageSize.A4);// 定义A4页面大小
//rectPageSize = rectPageSize.rotate();// 加上这句可以实现A4页面的横置
//文档的背景色
pSize.setBackgroundColor(Color.white);
//设置字体,需要包iTextAsian.jar
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
//创建一个文档对象,设置初始化大小和页边距
document = new Document(pSize,50,50,50,50);
//定义一个表格
PdfPTable table = new PdfPTable(3);
Paragraph tinfo = null;
for(int i=0;i<9;i++){
PdfPCell cell = new PdfPCell();
tinfo = new Paragraph("中文", FontChinese);
cell.addElement(tinfo);
table.addCell(cell);
}
//义一个图片
Image jpeg = Image.getInstance("C://aa.jpeg");
jpeg.setAlignment(Image.ALIGN_CENTER);
//定义输出位置并把文档对象装入输出对象中
String savePath = sysPath+"/pdf/"+pdfName;
PdfWriter.getInstance(document, new FileOutputStream(savePath));
//打开文档对象
document.open();
document.add(table);
document.add(jpeg);
//加入文档内容,中文处理
Paragraph t = new Paragraph(pdf, FontChinese);
document.add(t);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("message","get Info Error"+e.getMessage());
return mapping.findForward("globalException");
}finally{
document.close();
}
request.setAttribute("message","下载地址:"+"<a href='/pdf/"+pdfName+"'>下载</a>");
return mapping.findForward("success");
}
}
package **.**.**.action;
import java.awt.Color;
import java.io.FileOutputStream;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.easydone.util.ParamUtil;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* <p>Title: 本程序演示一个 action 怎样操作PDF文档</p>
* <p>Description: 所用开源包:itext-1.4.jar,iTextAsian.jar</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: 聚能易成</p>
* @author dirboy
* @version 1.0
*/
public class PdfAction extends Action {
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//创建一个文档对象,设置文档的页面大小
Document document = null;
String pdfName = (new Random()).nextInt()+".pdf";
String pdf = ParamUtil.getStr((String)request.getParameter("pdf"));
String sysPath = request.getRealPath("/");
try {
Rectangle pSize=new Rectangle(650,500);
//Rectangle rectPageSize = new Rectangle(PageSize.A4);// 定义A4页面大小
//rectPageSize = rectPageSize.rotate();// 加上这句可以实现A4页面的横置
//文档的背景色
pSize.setBackgroundColor(Color.white);
//设置字体,需要包iTextAsian.jar
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
//创建一个文档对象,设置初始化大小和页边距
document = new Document(pSize,50,50,50,50);
//定义一个表格
PdfPTable table = new PdfPTable(3);
Paragraph tinfo = null;
for(int i=0;i<9;i++){
PdfPCell cell = new PdfPCell();
tinfo = new Paragraph("中文", FontChinese);
cell.addElement(tinfo);
table.addCell(cell);
}
//义一个图片
Image jpeg = Image.getInstance("C://aa.jpeg");
jpeg.setAlignment(Image.ALIGN_CENTER);
//定义输出位置并把文档对象装入输出对象中
String savePath = sysPath+"/pdf/"+pdfName;
PdfWriter.getInstance(document, new FileOutputStream(savePath));
//打开文档对象
document.open();
document.add(table);
document.add(jpeg);
//加入文档内容,中文处理
Paragraph t = new Paragraph(pdf, FontChinese);
document.add(t);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("message","get Info Error"+e.getMessage());
return mapping.findForward("globalException");
}finally{
document.close();
}
request.setAttribute("message","下载地址:"+"<a href='/pdf/"+pdfName+"'>下载</a>");
return mapping.findForward("success");
}
}