Can't insert Tabs and Spaces into PDBox PDF document

杀马特。学长 韩版系。学妹 提交于 2019-12-01 11:37:06

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".

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.

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.

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.

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.

The cause is that PDPageContentStream.showText(String) can only show text, it cannot do any additional layout'ing like interpreting horizontal tabs, line breaks, or other control characters; Tilman explained this in detail in his answer.

You might achieve your goal with something like this:

contentStream.newLineAtOffset(xPosition, yPosition);
contentStream.showText("Member #: "+ student.getMembershipNumber());
contentStream.newLineAtOffset(200, 0);
contentStream.showText("Grade: " + getStudentGradeInSchool(student.getYearGraduate()));
contentStream.newLineAtOffset(200, 0);
contentStream.showText("Year Joined: " + student.getYearJoined());
contentStream.newLineAtOffset(-400, -12);
contentStream.showText("Name: " + student.getFirstName() + " " + student.getLastName());
contentStream.newLineAtOffset(0, -12);
contentStream.showText("Amount Owed: $" + student.getAmountOwed());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!