Jump to content
We've recently updated our Privacy Statement, available here ×

Solved - Jasper 7.1 - Chinese chars display in PDF - itext


SETTURGOPI

Recommended Posts

error message. The defaukt itext.jar in WEB-INF doesnt work and doesnt diplay chinese chars.

 

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font :

pdfFontName : STSong-Light

pdfEncoding : UniGB-UCS2-H

isPdfEmbedded : true

 

Solution URL --> http://www.voidcn.com/article/p-yjfqxujc-bqt.html --> is in chinsese language.

Solutions is transalated and pasted below.

 

 

When using jasperreport to do the report, the following error occurred, tossing for a long time, online check a lot of information, and finally solved 

net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font :

pdfFontName : STSong-Light 
pdfEncoding : UniGB-UCS2-H(Chinese Siplified) 

isPdfEmbedded : true

The cause of this problem is that the itext-asian.jar package has been updated and its package name has been changed to com.itextpdf.text.pdf.fonts. But the font file found in the jasperreport package is still the path com.lowagie.text.pdf.fonts.

My solution:

Copy all the files in the itext-asian.jar package, modify the package name, and repackage it into the custom-fonts.jar package. Just introduce it.

 

Dramatically, after solving the problem, I found a solution that is particularly similar to myself, with more detailed explanations, supplementing its link: http://blog.csdn.net/sanqima/article/details/50374151 

 

Also set the default font, add the following configuration in jrxml, do not have to set each control

=======================

The font and encoding files in iText5.x and above are loaded from String RESOURCE_PATH = "com/itextpdf/text/pdf/fonts/", and the package name of isotasian1.5.x.jar is com.lowagie.text. Pdf.fonts, the package name is inconsistent, resulting in a path error. The solution is as follows:
1. Unzip the itextasian1.5.x.jar and find the lowagie file in itextasian1.5.x/com. The lowagie name is: itextpdf, as shown in Figure (1):

 

2. Click [start] - "cmd -" in the lower right corner of the desktop to enter the itextasian folder and type: jar cvf iTextAsian_1 .5 .2 .jar com /itextpdf/text/pdf/fonts /* The folder itostasin can be packaged into iTextAsian_1.5.2.jar. In the cmd window, go to the ittextasian folder and use the package command: jar cvf iTextAsian_1.5.2.jar com/itextpdf/text/pdf/fonts/* The code for pdf operation using itext.jar and iterasian.jar is as follows: //shuiyin.java package com .pdf ; import java .io .File ; import java .io .FileNotFoundException ; import java .io .FileOutputStream ; import java .io .IOException ; import com .itextpdf .text .Document ; import com .itextpdf .text .DocumentException ; import com .itextpdf .text .Image ; import com .itextpdf .text .Paragraph ; import com .itextpdf .text .pdf .BaseFont ; import com .itextpdf .text .pdf .PdfContentByte ; import com .itextpdf .text .pdf .PdfReader ; import com .itextpdf .text .pdf .PdfStamper ; import com .itextpdf .text .pdf .PdfWriter ; public class Shuiying { public static void main(String[] args) { Document document = new Document() ; try { PdfWriter .getInstance (document, new FileOutputStream( "src/pdfwen/shuiyinTmp.pdf" )) ; document .open () ; document .add (new Paragraph( " " )) ; document .close () ; PdfReader reader = new PdfReader( "src/pdfwen/shuiyinTmp.pdf" ) ; PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( "src/pdfwen/shuiyin.pdf" )) ; Image img = Image .getInstance ( "src/img/watermark.jpg" ) ; img .setAbsolutePosition ( 50 , 385 ) ; PdfContentByte under = stamper .getUnderContent ( 1 ) ; //获得第一页的内容 under .addImage (img) ; //添加图片,完成水印功能 BaseFont chinese = BaseFont .createFont ( "STSong-Light" , "UniGB-UCS2-H" , BaseFont .NOT _EMBEDDED) ; under .beginText () ; //标记文本开始 under .setFontAndSize (chinese, 42 ) ; under .setTextMatrix ( 70 , 550 ) ; under .showText ( "下面是添加的水印图片." ) ; under .endText () ; //标记文本结束 stamper .close () ; //水印结束 File file = new File( "src/pdfwen/shuiyinTmp.pdf" ) ; file .delete () ; //删除临时文件 System .out .print ( "添加水印成功!" ) ; } catch (FileNotFoundException e) { e .printStackTrace () ; }catch (DocumentException e1) { e1 .printStackTrace () ; }catch (IOException e2) { e2 .printStackTrace () ; } } } The effect is as follows: Corrected itextasian.jar: http://download.csdn.net/detail/sanqima/9371147

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...