java-使用Apache PDFBox生成unicode pdf [英] java - generate unicode pdf with Apache PDFBox

查看:483
本文介绍了java-使用Apache PDFBox生成unicode pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的spring mvc应用程序中生成pdf.最近,我测试了 iTextPdf库,但无法生成unicode pdf文档.实际上,在生成的文档中我没有看到非拉丁字符.我决定将Apache PDFBox用于我的目的,但是我不知道它是否支持Unicode字符?如果有的话,有什么好的学习pdfBox的教程吗?如果没有,我应该使用哪个库? 预先感谢.

I have to generate pdf in my spring mvc application. recently I tested iTextPdf library, but i could not generate unicode pdf document. in fact I didn't see non-latin characters in the generated document. I decided to use Apache PDFBox for my purpose, but I don't know has it support unicode characters? If has, is there any good tutorial for learning pdfBox? And If not, which library should I use? Thanks in advance.

推荐答案

1.8.*版本不支持使用Unicode生成PDF,但2.0.*版本支持.这是示例EmbeddedFonts.java:

The 1.8.* versions don't support PDF generation with Unicode, but the 2.0.* versions do. This is the example EmbeddedFonts.java:

public class EmbeddedFonts
{
    public static void main(String[] args) throws IOException
    {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage(page);

        String dir = "../pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/";
        PDType0Font font = PDType0Font.load(document, new File(dir + "LiberationSans-Regular.ttf"));

        PDPageContentStream stream = new PDPageContentStream(document, page);

        stream.beginText();
        stream.setFont(font, 12);
        stream.setLeading(12 * 1.2);

        stream.newLineAtOffset(50, 600);
        stream.showText("PDFBox Unicode with Embedded TrueType Font");
        stream.newLine();

        stream.showText("Supports full Unicode text ?");
        stream.newLine();

        stream.showText("English русский язык Tiếng Việt");

        stream.endText();
        stream.close();

        document.save("example.pdf");
        document.close();
    }
}

请注意,与iText不同,PDFBox对PDF创建的支持非常低级,即我们不支持现成的段落或表格.没有教程,但是有很多示例.该API面向PDF规范.

Note that unlike iText, PDFBox support for PDF creation is very low level, i.e. we don't support paragraphs or tables out of the box. There is no tutorial, but a lot of examples. The API orients itself on the PDF specification.

这篇关于java-使用Apache PDFBox生成unicode pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆