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!
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.