未解決:
解決後:
代碼使用:
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document,
PdfWriter pdfWriter, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<String,String> userData =(Map<String,String>) model.get("userData");
//PdfPTable table = new PdfPTable(2);
Table table = new Table(2);
table.addCell("No.");
table.addCell("User Name");
// 使用iTextAsian.jar中的字體
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
for(Map.Entry<String,String> entry:userData.entrySet()) {
table.addCell(entry.getKey());
//table.addCell(entry.getValue());
table.addCell(new Paragraph(entry.getValue(),fontChinese));
}
document.add(table);
}
}
用到的包:
IText_2.1.7中文包集合下載地址:
https://download.csdn.net/download/hizhanyue/11084963
IText使用配置
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--必須1-->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!--必須2-->
<bean class="com.yiibai.springmvc.PDFController"/>
<!--必須3-->
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/views.xml</value>
</property>
</bean>
</beans>
views.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="UserSummary" class="com.yiibai.springmvc.UserPDFView"></bean>
</beans>