无法将选项卡和空格插入PDBox PDF文档 [英] Can't insert Tabs and Spaces into PDBox PDF document

查看:168
本文介绍了无法将选项卡和空格插入PDBox PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PDFBOX创建的pdf中打印它。它不会让我插入标签和空格,因为字体不支持它们。为什么这是一个问题,更重要的是,我该如何解决?

I want to print this in a pdf created by PDFBOX. It wont let me insert tabs and spaces because the font does not support them. Why is this a problem, and more importantly, how can I fix it?

StudentData student = listOfDebtors.get(j);

contentStream.beginText();
contentStream.setFont(font, 8);
contentStream.newLineAtOffset(xPosition, yPosition);
contentStream.showText("Member #:"+ student.getMembershipNumber() + "\t"
            + "Grade:" + getStudentGradeInSchool(student.getYearGraduate()) + "\t"
            + "Year Joined" + student.getYearJoined() + "\n" 
            + "Name:" + student.getFirstName() + " " + student.getLastName() + "\n"
            + "Amount Owed : $" + student.getAmountOwed()); 

显示错误:

Caused by: java.lang.IllegalArgumentException: No glyph for U+0009 in font Courier
    at org.apache.pdfbox.pdmodel.font.PDType1Font.encode(PDType1Font.java:353)
    at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:283)
    at org.apache.pdfbox.pdmodel.PDPageContentStream.showText(PDPageContentStream.java:341)
    at fbla.rahulshah.database.dataManipulation.PDFCreator.createDebtorPDF(PDFCreator.java:61)
    at fbla.rahulshah.database.view.MainScreenController.generateDebtReport(MainScreenController.java:114)
    ... 62 more


推荐答案

字体是一组字形。没有TAB字形这样的东西。想象一下,自己在100年前用金属字形进行排版,有些人(拥有打字机)问你标签字形。

A font is a set of glyphs. There is no such thing as "a TAB glyph". Just imagine yourself typesetting with metal glyphs 100 years ago and some guy (who owns a typewriter) asks you about "the tab glyph".

在打字机中,点击TAB意味着跳到下一个标签位置。字体不知道自己的位置,它只知道其字形的外观和大小。 PDF或PDFBox也没有标签位置的概念。 PDF或PDFBox不是文本编辑器。

In a typewriter, hitting TAB means "jump to the next tab position". A font does not know its own position, it only knows the look and the size of its glyphs. Nor does PDF or PDFBox have a concept of "tab positions". PDF or PDFBox aren't text editors.

即使使用编辑器,盲目点击TAB也不会总是让你开心,这取决于你只需要文本的长度中写道。您必须首先检查自己的位置,然后考虑点击TAB,或者两次点击它。

And even with an editor, blindly hitting TAB won't always making you happy, depending of the length of the text you just wrote. You would have to check your own position first, then think about hitting TAB, or maybe hitting it twice.

您应该做的是在写完数据列之后,您将自己定位到下一列的相应X位置。使用快递字体(固定),您还可以通过计算字符串的长度并添加适当的空格数来完成此操作。

What you should do instead is that after writing a data column, you position yourself to the appropriate X position of the next column. With a courier font (fixed with), you can also do this by calculating the length of a string and adding an appropriate count of spaces.

这将我们带到下一个部分,缺少的空间。好吧,使用一个有空格的不同字体,因为有一个空格字形:它看起来不可见,但它有一个固定的大小。

Which brings us to the next part, the missing space. Well, use a different font that has spaces, because there is a space glyph: it looks invisible, but it has a fixed size.

最后,也没有作为换行符号的东西。换行是一个命令。你已经使用了newLineAtOffset,它可以很好地定位你自己。请参阅mkl关于如何操作的答案。

And finally, there's also no such thing as a "newline glyph". Newline is a command. You already use "newLineAtOffset" which should work fine to position yourself. See the answer by mkl on how to do it.

这篇关于无法将选项卡和空格插入PDBox PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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