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!
Latest posts by Mark Stephens (see all)
- My experience of a Turkish bath (visiting Istanbul for DevFest) - November 24, 2017
- My 5 key takeaways from JavaOne 2017 - October 6, 2017
- My notes and pictures from thursday JavaOne 2017 - October 5, 2017
- My notes and pictures from Wednesday JavaOne 2017 - October 5, 2017
- My notes and pictures from Tuesday JavaOne 2017 - October 4, 2017