有用springmvc的IText开发过PDf的吗求教

本文讨论了在使用Spring MVC框架时遇到的IText版本不匹配导致PDF生成失败的问题,并提供了解决方案。通过重写特定方法以适应不同版本的IText库,成功解决了PDF页面缺失的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Override
protected void buildPdfDocument(Map<String, Object> model, Document document,
PdfWriter writer, HttpServletRequest request,
HttpServletResponse response)
throws Exception {
PdfReader reader = new PdfReader(request.getSession().getServletContext().getRealPath("/")+"temp.pdf");
ByteArrayOutputStream ba=new ByteArrayOutputStream();
PdfStamper stamper=new PdfStamper(reader,new FileOutputStream("/1.pdf"));
AcroFields fields = stamper.getAcroFields();
for (Iterator it = fields.getFields().keySet().iterator(); it
.hasNext();) {
System.out.println("---PDF----"+it.next());
}
Map<String, String> map=(Map<String, String>) model.get(DATA);
for (String key : map.keySet()) {
System.out.println("key:"+key);
String value = map.get(key);
System.out.println("value:"+value);
fields.setField(key, value);
fields.setField("userName","zhangsan");
}
// fillData(fields, (Map<String, String>) model.get(DATA));
stamper.setFormFlattening(true);
System.out.println("stamper=" + stamper);

stamper.close();
// response.setContentType("application/pdf");
// response.setContentLength(ba.size());
//ServletOutputStream outstream = response.getOutputStream();

// ba.writeTo(outstream);
reader.close();
// ba.flush();
// ba.close();
}
这是springmvc里间接继承AbstractView因为springmvc支持IText1.3写了个中间类让他支持5.4的pdf。
是不行有 问题。报过ExceptionConverter: java.io.IOException: The document has no pages. 这是现在下面代码报错的

顺便问下:Itext怎么搞了两套啊,一个IText和ItextPdf一个1.3和一个5.5
现在再重写的方法里都不知道写什么了
求大神?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值