论坛首页 Java企业应用论坛

『讨论』pdf2box的中文支持问题

浏览 16992 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-08-28  
需求:某个pdf文件作为模板,传入一些参数,动态生成pdf文件,发送到客户端。
实现:采用pdfbox(www.pdfbox.org)
问题:传入的参数如果包含中文,生成的那个部份是乱码
分析:在google和csdn.net上搜到有人发布过中文支持的补丁(for 版本0.6.6),但没有地方可以下载到。
我用的是最新版本0.7.1
怀疑pdfbox将传入的字串进行 new String(strPassed.getBytes(),"iso8859-1")的转换,再输出过去的,现象如此,没有查源码。

用过这个东东的各位给点建议?
亦或有其它的好用的库?(iText据说加个jar就可以解决中文问题,可是它有没有把pdf作为模板的功能呢?看官方文档其xml的支持不咋地:cry: 
jasperreport又太复杂了吧)
另:有什么实用工具可以把pdf文档转成word文档?(pdf professional、pdf2word,easy pdf2word converter、bcldrake都有中文乱码问题)
谢过先~
   发表时间:2005-08-29  
I used template PDF with iText.The following code is used to resolve font.
AcroFields form1 = ...
BaseFont bfJapan = BaseFont.createFont("KozMinPro-Regular",
                    "UniJIS-UCS2-H", BaseFont.EMBEDDED);;
Font FontJapan = new Font(bfJapan, 12, Font.NORMAL);;
form1.setFieldProperty("name", "textfont", bfJapan, null);;
...

You should set correct font in the field.
1 请登录后投票
   发表时间:2005-08-29  
谢谢!~
看到你前面给出的代码,我又去iText站细查了一下,虽然它的Documentation对于acroForm部份是under construction,但Tutorial: iText by Example确实有个把pdf作为模板的例子:Register.java
之前除了没解决中文问题,我已经用pdfbox写好了转换方法,幸好代码骨架做的说的过去,数据bean做了封装,很轻松参考您和Register.java改好了。
iText的中文解决方案也在例子中查到了(跟你的一样):
引用:
            // step 4: we add content to the document
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);;
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);;
            Paragraph p = new Paragraph(chinese, FontChinese);;
            document.add(p);;

除了对FontChinese变量定义的不理解,跑起来也不错。。。(其实没加这块代码我的测试pdf也能正常显示中文:)

现在又碰到一个有点傻眼的问题,我用代码填到TextField里去的内容,用鼠标点它时才显示,blur时就不显示内容,我本要用这个pdf来打印的,郁闷~

是不是pdf中除了acroForm(我猜想类似html的form)中的field可以方便的设置值,对一些静态文字的生成,只能把textfield设为readonly模拟做到?
0 请登录后投票
   发表时间:2005-08-29  
form1.setFieldProperty("name", "textfont", bfJapan, null);;

If you don't set the font property of TextField, you cannot see the word with the asian font properly.  If you want to print it only, just add the following sentence.
stamp2.setFormFlattening(true);;


If you have alreay added basefont, please try to change another one.
0 请登录后投票
   发表时间:2005-08-29  
还是没有正常显示那个textfield,只有focus的时候才能显示。这个效果只对中文有效。而且在手动更改这个textfield内容后回车,就好了。
无论设没设formflatting都一样。您说的basefont的设置俺不知道在哪搞
俺叫acrobat professional 给俺完全检查错误,并修正了。(高级-辅助工具-完全检查)


这是俺的代码:
            PdfReader reader = new PdfReader("X:\\work\\pdfboxdemo\\template.pdf");;

            int n = reader.getNumberOfPages();;
            // filling in the form
            PdfStamper stamp1 = new PdfStamper(reader, new FileOutputStream("generated.pdf"););;
//            stamp1.setFormFlattening(true);;

            AcroFields form1 = stamp1.getAcroFields();;
//            BaseFont bfJapan = BaseFont.createFont("KozMinPro-Regular",
//                                "UniJIS-UCS2-H", BaseFont.EMBEDDED);;

            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);;
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);;


//            form1.setFieldProperty("name", "textfont", bfJapan, null);;
            form1.setFieldProperty("name", "textfont", bfChinese, null);;

            form1.setField("haha","刘松");;
            stamp1.setFormFlattening(true);;
            stamp1.close();;
0 请登录后投票
   发表时间:2005-08-29  
BaseFont createFont(String name,  String encoding,
                                  boolean embedded); throws DocumentException, IOException

You may use "STSongStd-Light" or "STSong-Light" in name, use "Adobe-GB1-UCS2", "UniGB-UCS2-H" or "UniGB-UCS2-V" in encoding.  Try to assemble them again.
Reference to com.lowagie.text.pdf.fonts.
In cjkfonts.properties, you will find :
STSong-Light=Adobe-GB1-UCS2_UniGB-UCS2-H_UniGB-UCS2-V_
STSongStd-Light=Adobe-GB1-UCS2_UniGB-UCS2-H_UniGB-UCS2-V_
0 请登录后投票
   发表时间:2005-08-30  
还是不行。可能我没把问题描述清楚,附件是生成的pdf文件的效果,点击输入框才显示完整内容:"the word: <汉字> does not appear. "
否则只显示"the word: <> does not appear. "

输入框的字体设成了Times Bold,有的字体连英文都不正常显示。
组合:name: "STSongStd-Light" or "STSong-Light"
         encoding: "UniGB-UCS2-H" or "UniGB-UCS2-V"
没有起作用。"Adobe-GB1-UCS2"不能使用,说这个组合不能被recoganized.



sorry,是我前个回贴没有问题描述清,"不能显示汉字"可以理解为"显示乱码",我想要表达的是"汉字不可见",对于我的表达能力,只能再粪特一次了。
0 请登录后投票
   发表时间:2005-08-30  
I know your means because I met the same thing berfore.
You must set correct font in the field in templage file to show chinese word. I opened your pdf and found there is two "*" in the bracket.  There is a wrong font or encoding.
0 请登录后投票
   发表时间:2005-09-01  
I tried almost all the fonts for the field,but none does the right thing.
actually,except for the font `ms outlook',who renders narrow rectangle,the others take the same behavior(as previous said).

so,I wonder if the wrong encoding caused it. but,how to set encoding for my template pdf file? ( I looked throught the acrobat professional menu several times,but no entry found)

thanks for your kindness~
0 请登录后投票
   发表时间:2005-09-03  
Give me your template file.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics