问题
Possible Duplicate:
Using PDFBox to write UTF-8 encoded strings to a PDF
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();
}
}
}
Now, I'am calling function doIt:
app.doIt("test.pdf", "Skákal pes přes oves, přes zelenou louku.");
This completely works, but in output PDF I get: "þÿSkákal pes pYes oves, pYes zelenou louku."
I tried to find how to set UTF-8 encoding in PDFBox, but IMHO there is just no solution for this on the internet.
Do you have any ideas, how to get right text in output PDF?
Thank you.
回答1:
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" ) );
Here CheckRepFont.ttf
is your font file name as an example. Update it with actual one.
EDIT:
PDStream pdStream = new PDStream(doc);
PDSimpleFont font = PDType1Font.TIMES_ROMAN;
font.setToUnicode(pdStream);
来源:https://stackoverflow.com/questions/13274578/java-write-national-characters-to-pdf-using-pdfbox