IText2.1.7动态导出PDF文档
@RequestMapping(value="/test")
public ModelAndView getSubDetailExport() throws Exception {
Map<String,Object> dataMap = new HashMap<String,Object>();
List<String> titles = new ArrayList<String>();
titles.add("第一列"); //1
titles.add("第二列"); //2
titles.add("第三列"); //3
titles.add("第四列"); //4
List<Map<String,Object>> subList = new ArrayList<Map<String,Object>>();
for(int i=0;i<10;i++){
Map<String,Object> subpd = new HashMap<String,Object>();
subpd.put("var1", "1111"); //1
subpd.put("var2", "1111"); //2
subpd.put("var3", "1111"); //3
subpd.put("var4", "1111"); //4
subList.add(subpd);
}
dataMap.put("varList", subList);
dataMap.put("title", "这是一个标题");
dataMap.put("titles", titles);
ObjectPdfView erv = new ObjectPdfView(); //执行excel操作
ModelAndView mv = new ModelAndView(erv,dataMap);
return mv;
}
import org.springframework.web.servlet.view.document.AbstractPdfView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.Color;
/**
* @描述:导出pdf文件
* @author:guoX
* @date:2017/6/7
*/
public class ObjectPdfView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Rectangle rectPageSize = new Rectangle(900,900);// 定义页面大小
document.setPageSize(rectPageSize);
String title = (String)model.get("title"); // 设置名字
title+=".pdf";
List<String> titles = (List<String>) model.get("titles");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(title.getBytes("GB2312"), "ISO_8859_1"));
document.addTitle(title);
document.addSubject("export information");
document.addAuthor("leno");
document.addCreator("leno");
document.addKeywords("pdf itext");
// 定义页头和页尾
HeaderFooter header = new HeaderFooter(new PdfParagraph(title, 20,
true), false);
header.setAlignment(Element.ALIGN_CENTER);
HeaderFooter footer = new HeaderFooter(new Phrase(
"This is page "), new Phrase("."));
footer.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);
document.setFooter(footer);
document.open();
PdfPTable table = new PdfPTable(titles.size());
// table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.setWidthPercentage(16 * titles.size());
float[] TableWidths = { 15, 40, 15, 20 };//按百分比分配单元格宽带
table.setTotalWidth(800);
table.setLockedWidth(true);
// 产生表格标题行
for (int i = 0; i < titles.size(); i++) {
PdfPCell cell = new PdfPCell(new PdfParagraph(titles.get(i), 14,
true));
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
cell.setBackgroundColor(Color.gray);
cell.setBorderColor(Color.black);
table.addCell(cell);
}
List<Map<String,Object>> varList = (List<Map<String,Object>>) model.get("varList");
for(int i=0; i<varList.size(); i++){
Map<String,Object> vpd = varList.get(i);
for(int j=0;j<titles.size();j++){
PdfPCell cell = null;
String varstr = vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "";
if (varstr != null) {
cell = new PdfPCell(new PdfParagraph(varstr));
}
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
cell.setBorderColor(Color.black);
table.addCell(cell);
}
}
document.add(table);
document.close();
}
}
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
public class PdfParagraph extends Paragraph {
private static final long serialVersionUID = -244970043180837974L;
public PdfParagraph(String content) {
super(content, getChineseFont(12, false));
}
public PdfParagraph(String content, int fontSize, boolean isBold) {
super(content, getChineseFont(fontSize, isBold));
}
// 设置字体-返回中文字体
protected static Font getChineseFont(int nfontsize, boolean isBold) {
BaseFont bfChinese;
Font fontChinese = null;
try {
bfChinese = BaseFont.createFont("c://windows//fonts//simsun.ttc,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
if (isBold) {
fontChinese = new Font(bfChinese, nfontsize, Font.BOLD);
} else {
fontChinese = new Font(bfChinese, nfontsize, Font.NORMAL);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fontChinese;
}
// ת������
protected Cell ChangeCell(String str, int nfontsize, boolean isBold)
throws IOException, BadElementException, DocumentException {
Phrase ph = ChangeChinese(str, nfontsize, isBold);
Cell cell = new Cell(ph);
// cell.setBorderWidth(3);
return cell;
}
// ת������
protected Chunk ChangeChunk(String str, int nfontsize, boolean isBold)
throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize, isBold);
Chunk chunk = new Chunk(str, FontChinese);
return chunk;
}
// ת������
protected Phrase ChangeChinese(String str, int nfontsize, boolean isBold)
throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize, isBold);
Phrase ph = new Phrase(str, FontChinese);
return ph;
}
}
import java.io.UnsupportedEncodingException;
public class StrHelp {
public static String getChinese(String s) {
try {
return new String(s.getBytes("gb2312"), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
return s;
}
}
}