Understanding the PDF file Format – Space is a special character

The PDF text stream contains the instructions to draw the actual contents of the PDF. One of these instructions is the Tj command, which takes a text string. So (Hello)Tj will write the word Hello onto the PDF page at the current co-ordinate location.

Things get more interesting if you were to use (Hello World)Tj because space is a special value. When a space character is detected, some additional commands come into play. The Tw command specifies a space to be added when there is a gap between words. This allows you to have extra spacing between words for fine control over word position.

In theory you could add the Tw value when you find the value 32 (standard space value). The problem is that the character values can be mapped onto any character. So what happens if space is not a space?

In reality, you need to check that this is a genuine space by looking at the what it maps onto as a character value. If it is not a real space you need to ignore it (and of course you need to keep an eye at what the real space character is). The PDF file format can be complex at times!

This post is part of our “Understanding the PDF File Format” series. In each article, we discuss a PDF feature, bug, gotcha or tip. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.
He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX.

He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.