【发布时间】:2013-09-05 02:24:00
【问题描述】:
以下是使用Java编写PDF的代码。
代码
public class PDFTest {
public static void main(String args[]) {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
File file = new File("C://test//itext-test.pdf");
FileOutputStream fileout = new FileOutputStream(file);
PdfWriter.getInstance(document, fileout);
document.addAuthor("Me");
document.addTitle("My iText Test");
document.open();
Chunk chunk = new Chunk("iText Test");
Paragraph paragraph = new Paragraph();
String test = "și";
String test1 = "şi";
if (test.equalsIgnoreCase(test1)) {
// System.out.println("equal ignore case true");
paragraph.add(test + " New Font equal with Old Font");
} else {
// System.out.println("equal ignore case X true");
paragraph.add(test1 + " New Font Not equal with Old Font");
}
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
当我使用罗马尼亚语进行测试时,我发现创建的 PDF 中缺少 "ș"。
文档如下所示:
高度赞赏有关此问题的任何建议或参考链接。
**EDITED**
我使用了下面的 unicode 示例,输出仍然相同。 "ș" 仍然下落不明。
Code
static String RESULT = "C://test/itext-unicode4.pdf";
static String FONT = "C://Users//PenangIT//Desktop//Arial Unicode.ttf";
public static void main(String args[])
{
try
{
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
doc.open();
BaseFont bf;
bf = BaseFont.createFont(FONT,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
doc.add(new Paragraph("Font : "+bf.getPostscriptFontName()+" with encoding: "+bf.getEncoding()));
doc.add(new Paragraph(" TESTING "));
doc.add(new Paragraph(" TESTING 1 și "));
doc.add(new Paragraph(" TESTING 2 şi "));
doc.add(Chunk.NEWLINE);
doc.close();
}
catch(Exception ex)
{
}
输出如下所示
编码也一样。 "ș" 仍然不见了。
【问题讨论】:
-
你想要什么。我没有得到。
-
@RahulKulhari 在 PDF 中写入文件时缺少某些字体。 PDF 只得到
i而不是și。 -
您没有选择任何特定的字体。因此,使用默认字体肯定是每个 PDF 查看器必须支持的标准 14 字体之一,无需多言。不幸的是,他们只需要为有限的字符集支持它,并且很可能您的特殊字符不在该字符集中。因此,您应该继续明确使用支持您的字符的字体并将其嵌入到 PDF 中。
标签: java pdf fonts itext pdf-writer