Chapter 5. Graphics

In this chapter, we’ll run through the main ways to build graphics in the content stream of a PDF page. All of the examples are based on the same PDF we created manually in Chapter 2 and processed into valid PDF documents with pdftk in the same fashion. All the examples are included in the online resources.

Looking at Content Streams

A PDF page is made up of one or more content streams, defined by the /Contents entry in the page object, together with a shared set of resources, defined by the /Resources entry. In all our examples, there will only be a single content stream. Multiple content streams are equivalent to a single stream containing their concatenated content.

Here’s an example page, with no resources and a single content stream:

3 0 obj
<<
  /Type /Page
  /Parent 1 0 R
  /Resources << >>
  /MediaBox [ 0 0 792 612 ]
  /Rotate 0
  /Contents [ 2 0 R ]
>>
endobj

Here’s the associated content stream, consisting of the stream dictionary and the stream data.

2 0 obj
<< /Length 18 >> Stream dictionary
stream
200 150 m 600 450 l S Stream data
endstream
endobj

We’ll discover what the m, l and S operators do in a moment. The numbers are measurements in points—a point (or pt) is 1/72 inch. The result of loading this document into a PDF viewer (after processing with pdftk as per Chapter 2) is shown in Figure 5-1.

The full manually created file (before processing with pdftk) is shown in Example 5-1. We’re going to be using variations on this file for the rest of this chapter. For the ...

Get PDF Explained now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.