Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()); 

Error shown:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...