无法在 PDBox PDF 文档中插入制表符和空格 [英] Can't insert Tabs and Spaces into PDBox PDF document

查看:49
本文介绍了无法在 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() + "	"
            + "Grade:" + getStudentGradeInSchool(student.getYearGraduate()) + "	"
            + "Year Joined" + student.getYearJoined() + "
" 
            + "Name:" + student.getFirstName() + " " + student.getLastName() + "
"
            + "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 表示跳到下一个 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 位置.使用 courier 字体(固定),您还可以通过计算字符串的长度并添加适当的空格数来实现.

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天全站免登陆