读书人

iText写pdf模板PdfReader创建报错有关

发布时间: 2012-03-28 15:40:03 作者: rapoo

iText写pdf模板PdfReader创建报错问题!!!!跪求大虾解决!谢谢
我的代码如下

Java code
try {            /* 打开已经定义好字段以后的pdf模板 */            PdfReader reader = new PdfReader("D:\\感谢函.pdf");             /* 将要生成的目标PDF文件名称 */            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(                    "D:\\感谢函全含.pdf"));            /* 取出报表模板中的所有字段 */            AcroFields form = stamper.getAcroFields();            /* 为字段赋值,注意字段名称是区分大小写的 */            form.setField("customerName", "远东宏信");            form.setField("year", "2011");            form.setField("month", "11");            form.setField("day", "14");            form.setField("yearO", "2010");            form.setField("monthO", "12");            form.setField("dayO", "15");            stamper.setFormFlattening(true);            /* 必须要调用这个,否则文档不会生成的 */            stamper.close();        } catch (DocumentException de) {            System.err.println(de.getMessage());        } catch (IOException ioe) {            System.err.println(ioe.getMessage());        }


在我D盘有个感谢函.pdf并用Adobe Acrobat 8 Professional做好了文本域。但感谢函全含.pdf没有。
可断点调试刚走完第一行代码就报错。说
Exception: PDF header signature not found.
java.io.IOException: PDF header signature not found.
at com.lowagie.text.pdf.PRTokeniser.checkPdfHeader(Unknown Source)
at com.lowagie.text.pdf.PdfReader.readPdf(Unknown Source)
at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source)
at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source

我用的JAR包是iText-2.1.7.jar
请问有哪里写错了吗?


[解决办法]
BaseFont qzfont = BaseFont.createFont(font, encoding, BaseFont.NOT_EMBEDDED);
pdfStamp.setFontAndSize(qzfont, fontsize);

加上这个字体设置
[解决办法]
需要用到一个iTextAsian.jar 亚洲语言包。
具体怎么用,google下吧。

读书人网 >J2EE开发

热点推荐