iText:无法打印∈,∩,Σ,∫,Δ√,mathematical等数学字符 [英] iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠
问题描述
我正在使用iText 4.0.2版本进行pdf生成。我有一些字符/符号可以打印,如∈,∩,Σ,∫,△(数学符号)等等。
我的代码:
I am using iText 4.0.2 version for pdf generation. I have some characters/symbols to print like ∈, ∩, ∑, ∫, ∆ (Mathematical symbols) and many others. My code :
Document document = new Document(PageSize.A4, 60, 60, 60, 60);
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/adeel/experiment.pdf"));
document.open();
String str = "this string will contains special character like this ∈, ∩, ∑, ∫, ∆";
BaseFont bfTimes = null;
try {
bfTimes = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Font fontnormal = new Font(bfTimes, 12, Font.NORMAL, Color.BLACK);
Paragraph para = new Paragraph(str, fontnormal);
document.add(para);
document.close();
writer.close();
System.out.println("Done!");
} catch (DocumentException e)
{
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
在上面的代码中,我将所有符号放在 str
并生成pdf文件。代替符号,我尝试在 str
中为符号添加unicode字符,但它没有用。
In the above code I am putting all the symbols in str
and generating the pdf file. In place of symbols I tried putting unicode characters in str
for the symbols but It didn't worked as well.
推荐答案
请查看 MathSymbols 示例。
- 首先,您需要一种支持所需符号的字体。顺便说一句,FreeSans.ttf就是这样的字体。然后你需要使用正确的编码。
- 你正在使用UNICODE,所以你需要
Identity-H
作为编码。 / li>
- 你还应该使用诸如
\ u2208
,\ u229
之类的符号,\ u2211
,\ u222b
,\ u2206
。这不是必须的,但这是一个很好的做法。
- First you need a font that supports the symbols you need. Incidentally, FreeSans.ttf is such a font. Then you need to use the right encoding.
- You're using UNICODE, so you need
Identity-H
as the encoding. - You should also use notations such as
\u2208
,\u2229
,\u2211
,\u222b
,\u2206
. That's not a must, but it's good practice.
这就是它的完成方式:
public static final String DEST = "results/fonts/math_symbols.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static final String TEXT = "this string contains special characters like this \u2208, \u2229, \u2211, \u222b, \u2206";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
Paragraph p = new Paragraph(TEXT, f);
document.add(p);
document.close();
}
结果如下: math_symbols.pdf
重要提示:您应该始终使用iText的最新官方版本。 iText 4.0.2 不是正式版。
Important: you should always use the most recent, official version of iText. iText 4.0.2 is not an official version.
这篇关于iText:无法打印∈,∩,Σ,∫,Δ√,mathematical等数学字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!