java 如何从关于使用第三方字体的 Unicode 字符集语言创建 PDF 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6181518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a PDF document from languages of Unicode char set regarding using third party Fonts
提问by lisak
I'm using PDFBoxand iTextto create a simple (just paragraphs) pdf document from various languages. Something like :
我正在使用PDFBox和iText从各种语言创建一个简单的(只是段落)pdf 文档。就像是 :
pdfBox:
pdfBox:
private static void createPdfBoxDocument(File from, File to) {
PDDocument document = null;
try {
document = new TextToPDF().createPDFFromText(new FileReader(from));
document.save(new FileOutputStream(to));
} finally {
if (document != null)
document.close();
}
}
private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDType1Font font = PDType1Font.TIMES_ROMAN;
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("?");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
}
itext:
文本:
private static Font blackFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.BLACK);
private static void createITextDocument(File from, File to) {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(to));
document.open();
addContent(document, getParagraphs(from));
document.close();
}
private static void addContent(Document document, List<String> paragraphs) {
for (int i = 0; i < paragraphs.size(); i++) {
document.add(new Paragraph(paragraphs.get(i), blackFont));
}
}
The input files are encoded in UTF-8 and some languages of Unicode char set, like Russian alphabet etc., are not rendered properly in pdf. The Fonts in both libraries don't support Unicode charset I suppose and I can't find any documentation on how to add and use third party fonts. Could please anybody help me out with an example ?
输入文件以 UTF-8 编码,某些 Unicode 字符集语言(如俄语字母表等)在 pdf 中无法正确呈现。我想这两个库中的字体都不支持 Unicode 字符集,而且我找不到任何关于如何添加和使用第三方字体的文档。请有人帮我举个例子吗?
回答by lisak
If you are using iText, it has quite good support.
如果您使用的是 iText,它有很好的支持。
In iText in Action (chapter 2.2.2) you can read more.
在 iText in Action(第 2.2.2 章)中,您可以阅读更多内容。
You have to download some unicode Fonts like arialuni.ttfand do it like this :
你必须下载一些 unicode 字体,比如arialuni.ttf并这样做:
public static File fontFile = new File("fonts/arialuni.ttf");
public static void createITextDocument(File from, File to) throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(to));
document.open();
writer.getAcroForm().setNeedAppearances(true);
BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(unicode));
addContent(document, getParagraphs(from), fs);
document.close();
}
private static void addContent(Document document, List<String> paragraphs, FontSelector fs) throws DocumentException {
for (int i = 0; i < paragraphs.size(); i++) {
Phrase phrase = fs.process(paragraphs.get(i));
document.add(new Paragraph(phrase));
}
}
arialuni.ttf fonts work for me, so far I checked it support for
arialuni.ttf 字体对我有用,到目前为止我检查了它对
BG, ES, CS, DA, DE, ET, EL, EN, FR, IT, LV, LT, HU, MT, NL, PL, PT, RO, SK, SL, FI, SV
and only PDF in Romanian language wasn't created properly...
并且只有罗马尼亚语的 PDF 没有正确创建......
With PDFBoxit's almost the same:
使用PDFBox几乎相同:
private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDFont font = PDTrueTypeFont.loadTTF(document, "fonts/arialuni.ttf");
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("?");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
}
However as Gagravarr says, it doesn't work because of this issue PDFBOX-903. Even with 1.6.0-SNAPSHOT version. Maybe trunk will work. I suggest you to use iText. It works there perfectly.
然而,正如 Gagravarr 所说,由于这个问题PDFBOX-903它不起作用。即使是 1.6.0-SNAPSHOT 版本。也许主干会起作用。我建议你使用 iText。它在那里完美地工作。
回答by Gagravarr
You may find this answerhelpful - it confirms that you can't do what you need with one of the standard type 1 fonts, as they're Latin1 only
您可能会发现此答案很有帮助 - 它确认您无法使用标准类型 1 字体之一执行所需的操作,因为它们仅是 Latin1
In theory, you just need to embed a suitable fontinto the document, which handles all your codepoints, and use that. However, there's at least one open bugwith writing unicode strings, so there's a chance it might not work just yet... Try the latest pdfbox from svn trunk too though to see if it helps!
理论上,您只需要将合适的字体嵌入到文档中,它会处理您的所有代码点,然后使用它。但是,编写 unicode 字符串至少存在一个未解决的错误,因此它可能还无法正常工作......不过也尝试使用 svn trunk 中的最新 pdfbox 看看它是否有帮助!
回答by user1145691
In my project, I just copied the font that supported UTF8 (or whatever language you want) to a directory (or you can used Windows fonts path) and add some code, it looked like this
在我的项目中,我只是将支持 UTF8(或任何你想要的语言)的字体复制到一个目录(或者你可以使用 Windows 字体路径)并添加一些代码,它看起来像这样
BaseFont baseFont = BaseFont.createFont("c:\a.ttf", BaseFont.IDENTITY_H,true);
Font font = new Font(baseFont);
document.add(new Paragraph("Not English Text",font));
Now, you can use this font to show your text in various languages.
现在,您可以使用此字体以各种语言显示您的文本。
回答by Bijin P Thomas
//use this code.Sometimes setfont() willnot work with Paragraph
//使用此代码。有时 setfont() 不适用于段落
try
{
FileOutputStream out=new FileOutputStream(name);
Document doc=new Document();
PdfWriter.getInstance(doc, out);
doc.open();
Font f=new Font(FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
Paragraph p=new Paragraph("New PdF",f);
p.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(p);
doc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}