由于jasper report使用itext库,因此最简单的测试字体是否将在pdf中正确显示的方法是直接使用itext测试它。
程序*,改编自iText:第11章:选择正确的字体
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfWriter;
public class FontTest {
/** The resulting PDF file. */
public static final String RESULT = "fontTest.pdf";
/** the text to render. */
public static final String TEST = "Test to render this text with the turkish lira character \u20BA";
public void createPdf(String filename) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
BaseFont bf = BaseFont.createFont(
"pathToMyFont/myFont.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 20);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36, 730, 569, 36);
column.addElement(new Paragraph(TEST, font));
column.go();
document.close();
}
public static void main(String[] args) throws IOException, DocumentException {
new Fonttest().createPdf(RESULT);
}
}
一些注意事项(如示例所示):
如果在“ fontTest.pdf”中 显示了字体,则在jasper报告中字体扩展名有问题。
如果您在“ fontTest.pdf”中 字体,则在jasper报告中您无能为力,您需要查找其他字体。
*最新的jasper-reports发行版使用特殊版本itext-2.1.7,此版本中的导入为com.lowagie.text
,如果您使用的是更高版本,则导入com.itextpdf.text
如改编示例中所示。