itext中文显示乱码或者不显示itext-2.1.7

 

未解决:

解决后:

代码使用:

    @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>

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值