Spring pdf视图
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>pdf</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pdf</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
2.pdf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="customPdfView" class="org.springframework.zwm.CustomPdfView" />
<bean name="/pdf.html" class="org.springframework.zwm.PdfController" />
</beans>
3.PdfController
package org.springframework.zwm;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class PdfController implements Controller {
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
Map model = new HashMap();
model.put("information", "Information");
//...model.put....
return new ModelAndView("customPdfView", model);
}
}
4.CustomPdfView.java
package org.springframework.zwm;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;
/**
* @about org.springframework.zwm
* @author zwm
*
*/
public class CustomPdfView extends AbstractPdfView {
protected void buildPdfDocument(Map model, Document document,
PdfWriter writer, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 打开文档,以写入内容
document.open();
// 创建目录标识
Paragraph title1 = new Paragraph((String) model.get("information"),
FontFactory.getFont(FontFactory.defaultEncoding, 19, Font.BOLD,
Color.BLUE));
// 第一章
Chapter chapter1 = new Chapter(title1, 1);
// 将编号级别设为0,就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
// 创建第一部分
Paragraph partOne = new Paragraph("PartⅠ Industry", FontFactory
.getFont(FontFactory.defaultEncoding, 16, Font.BOLD, Color.black));
// 创建第二部分
Paragraph partTwo = new Paragraph("PartⅡ 春天免费框架预览", FontFactory
.getFont(FontFactory.defaultEncoding, 16, Font.BOLD,
Color.BLACK));
// 创建第三部分
Paragraph partThree = new Paragraph("PartⅢ \"Spring\"", FontFactory
.getFont(FontFactory.defaultEncoding, 16, Font.BOLD,
Color.black));
partThree.setAlignment(2);
// 创建节
Paragraph partOneOne = new Paragraph("PartⅠ Industry About",
FontFactory.getFont(FontFactory.defaultEncoding, 16, Font.BOLD,
Color.black));
Section section1 = chapter1.addSection(partOne);
section1.addSection(partOneOne);
chapter1.addSection(partTwo).addSection(partTwo);
section1.addSection(partThree);
// ///
// 第二章
Paragraph title2 = new Paragraph("Technology", FontFactory.getFont(
FontFactory.defaultEncoding, 19, Font.BOLD,
new Color(0, 0, 255)));
Chapter chapter2 = new Chapter(title2, 2);
chapter2.setNumberDepth(0);
Paragraph title2One = new Paragraph("China Telecom",
FontFactory.getFont(FontFactory.defaultEncoding, 19, Font.BOLD,
Color.BLUE));
chapter2.addSection(title2One);
//
try {
document.add(chapter1);
document.add(chapter2);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}