我必须在我的 spring mvc 应用程序中生成 pdf。最近我测试了iTextPdf 库 http://itextpdf.com/,但我无法生成 unicode pdf 文档。事实上,我在生成的文档中没有看到非拉丁字符。我决定使用 Apache PDFBox 来实现我的目的,但我不知道它是否支持 unicode 字符?如果有的话,有什么好的学习pdfBox的教程吗?如果没有,我应该使用哪个库?
提前致谢。
1.8.* 版本不支持使用 Unicode 生成 PDF,但 2.0.* 版本支持。这是 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 规范为导向。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)