Java:使用PDFBox将国家字符写入PDF [英] Java: Write national characters to PDF using PDFBox
问题描述
Possible Duplicate:
Using PDFBox to write UTF-8 encoded strings to a PDF
我需要创建带有捷克民族字符的PDF,而我正在尝试使用PDFBox库. 我已经从一些教程中复制了以下代码:
I need to create PDF with Czech national characters, and I'm trying to do it with PDFBox library. I have copied following code from some tutorials:
public void doIt(String file, String message) throws IOException, COSVisitorException
{
PDDocument doc = null;
try
{
doc = new PDDocument();
PDSimpleFont font = PDType1Font.TIMES_ROMAN;
TextToPDF textToPdf = new TextToPDF();
textToPdf.setFont(font);
textToPdf.setFontSize(12);
doc = textToPdf.createPDFFromText(new StringReader(message));
doc.save(file);
}
finally
{
if( doc != null )
{
doc.close();
}
}
}
现在,我正在调用函数doIt:
Now, I'am calling function doIt:
app.doIt("test.pdf", "Skákal pes přes oves, přes zelenou louku.");
这完全有效,但是在输出PDF中,我得到:þÿSkákalpes pYes oves,pYes zelenou louku."
This completely works, but in output PDF I get: "þÿSkákal pes pYes oves, pYes zelenou louku."
我试图找到如何在PDFBox中设置UTF-8编码,但是恕我直言,互联网上对此没有解决方案.
I tried to find how to set UTF-8 encoding in PDFBox, but IMHO there is just no solution for this on the internet.
您有什么想法,如何在输出的PDF中获得正确的文本?
Do you have any ideas, how to get right text in output PDF?
谢谢.
推荐答案
我认为其PDType1Font.TIMES_ROMAN
字体不支持您的捷克国家字符.如果您可以设法获取Czech national characters
的.ttf
文件,请使用以下方法获取PDFont
并使用相同的方法:
I think its PDType1Font.TIMES_ROMAN
font which is not supporting your Czech national characters. If you can manage to get the .ttf
files for the Czech national characters
, then use below to get PDFont
as below and use the same:
PDFont font = PDTrueTypeFont.loadTTF( doc, new File( "CheckRepFont.ttf" ) );
此处CheckRepFont.ttf
是您的字体文件名作为示例.用实际的更新它.
Here CheckRepFont.ttf
is your font file name as an example. Update it with actual one.
PDStream pdStream = new PDStream(doc);
PDSimpleFont font = PDType1Font.TIMES_ROMAN;
font.setToUnicode(pdStream);
这篇关于Java:使用PDFBox将国家字符写入PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!