Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's write a PDF file

Let's write a PDF file

A simple walk-through to learn the basics of the PDF format (at your rhythm)

2017/07/23 : first release
2015/07/28 : r2 - typos, improvements, stream filters

Ange Albertini

July 23, 2015
Tweet

More Decks by Ange Albertini

Other Decks in Technology

Transcript

  1. Let’s write a PDF file A simple walk-through to learn

    the basics of the PDF format (at your rhythm) PDF = Portable Document Format r2
  2. PDF is text-based, with some binary in specific cases. But

    not in this example, so just open a text editor.
  3. Statements are separated by white space. (any extra white space

    is ignored) Any of these: 0x00 Null 0x0C Form Feed 0x09 Tab 0x0D Carriage Return 0x0A Line feed 0x20 Space (yes, you can mix EOL style :( )
  4. %PDF-_ A PDF starts with a %PDF-? signature followed by

    a version number. 1.0 <= version number <= 1.7 (it doesn’t really matter here)
  5. %PDF-1.3 %file body xref _ After the file body, comes

    the cross reference table. It starts with the xref keyword, on a separated line.
  6. %PDF-1.3 %file body xref %xref table here _ After the

    xref keyword, comes the actual table. (we’ll see about it later)
  7. %PDF-1.3 %file body xref %xref table here trailer_ After the

    table, comes the trailer... It starts with a trailer keyword.
  8. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    _ (we’ll see that later too…) ...and its contents.
  9. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    startxref _ (with startxref) Then, a pointer to the xref table...
  10. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    startxref %xref pointer %%EOF_ ...an %%EOF marker. Lastly, to mark the end of the file...
  11. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    startxref %xref pointer %%EOF Easy ;) That’s the overall layout of a PDF document!
  12. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    startxref %xref pointer %%EOF Now, we just need to fill in the rest :)
  13. Def: dictionary object Sequence of keys and values (no delimiter

    in between) enclosed in << and >> sets each key to value
  14. Keys are always name objects << /Index 1>> sets /Index

    to 1 << Index 1 >> is invalid (the key is not a name)
  15. Dictionaries can have any length << /Index 1 /Count /Whatever

    >> sets /Index to 1 and /Count to /Whatever
  16. Extra white space is ignored (as usual) << /Index 1

    /Count /Whatever >> is equivalent to << /Index 1 /Count /Whatever >>
  17. Dictionaries can be nested. << /MyDict << >> >> sets

    /MyDict to << >> (empty dictionary)
  18. White space before delimiters is not required. << /Index 1

    /MyDict << >> >> equivalent to <</Index 1/MyDict<<>>>>
  19. Def: indirect object an object number (>0), a generation number

    (0*) the obj keyword the object content the endobj keyword * 99% of the time
  20. Object reference Refers to an indirect object as a value

    ex: << /Root 1 0 R >> refers to object number 1 generation 0 as the /Root
  21. Used only as values in a dictionary << /Root 1

    0 R >> is OK. << 1 0 R /Catalog>> isn’t.
  22. Be careful with the syntax! “1 0 3” is a

    sequence of 3 numbers 1 0 3 “1 0 R” is a single reference to an object number 1 generation 0
  23. Example 1 0 obj 3 endobj 2 0 obj <<

    /Index 1 >> endobj defines 2 objects with different contents
  24. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    startxref %xref pointer %%EOF Remember this?
  25. %PDF-1.3 %file body xref %xref table here trailer %trailer contents

    startxref %xref pointer %%EOF Now, let’s start!
  26. %PDF-1.3 %file body xref %xref table here trailer << _

    >> startxref %xref pointer %%EOF The trailer is a dictionary.
  27. %PDF-1.3 %file body xref %xref table here trailer << /Root_

    >> startxref %xref pointer %%EOF It defines a /Root name...
  28. %PDF-1.3 %file body xref %xref table here trailer << /Root

    1 0 R_>> startxref %xref pointer %%EOF ...that refers to an object...
  29. %PDF-1.3 %file body xref %xref table here trailer << /Root

    1 0 R >> startxref %xref pointer %%EOF (like all the the other objects) ...that will be in the file body.
  30. %PDF-1.3 _ xref %xref table here trailer << /Root 1

    0 R >> startxref %xref pointer %%EOF Let’s create our first object...
  31. %PDF-1.3 1 0 obj _ endobj xref %xref table here

    trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …(with the standard object declaration)...
  32. %PDF-1.3 1 0 obj << _ >> endobj xref %xref

    table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF (like most objects) ...that contains a dictionary.
  33. %PDF-1.3 1 0 obj << /Type_ >> endobj xref %xref

    table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...and its /Type is...
  34. %PDF-1.3 1 0 obj << /Type /Catalog_ >> endobj xref

    %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...defined as /Catalog...
  35. %PDF-1.3 1 0 obj << /Type /Catalog _ >> endobj

    xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF the /Root object also refers to the page tree...
  36. %PDF-1.3 1 0 obj << /Type /Catalog /Pages_ >> endobj

    xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...via a /Pages name...
  37. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R_>> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...that refers to another object...
  38. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj _ xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...which we’ll create.
  39. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj _ xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Let’s create object 2.
  40. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj _ endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF The usual declaration.
  41. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << _ >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF It’s a dictionary too.
  42. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages_ >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF The pages’ object /Type has to be defined as … /Pages ☺
  43. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids_ >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF This object defines its children via /Kids...
  44. Def: array enclosed in [ ] values separated by whitespace

    ex: [1 2 3 4] is an array of 4 integers 1 2 3 4
  45. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ _ ] >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...which is an array...
  46. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R_] >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF … of references to each page object.
  47. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] _ >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF One last step...
  48. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1_>> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...the number of kids has to be set in /Count...
  49. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...and now object 2 is complete!
  50. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj _ We can add our only Kid...
  51. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj _ endobj …(a single page)...
  52. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << _ >> endobj … a dictionary...
  53. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type_ >> endobj … defining a /Type...
  54. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page_ >> endobj … as /Page.
  55. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent_ >> endobj This grateful kid properly recognizes its own parent...
  56. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R_>> endobj … as you would expect ☺
  57. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R _ >> endobj Our page requires resources.
  58. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources_ >> endobj Let’s add them...
  59. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << _ >> >> endobj ...as a dictionary:
  60. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font_ >> >> endobj In this case, fonts...
  61. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << _ >> >> >> endobj ...as a dictionary.
  62. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << _ >> >> >> endobj We define one font...
  63. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1_ >> >> >> endobj ...by giving it a name...
  64. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << _ >> >> >> >> endobj ...and setting its parameters:
  65. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type_ >> >> >> >> endobj its type is ...
  66. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font_ >> >> >> >> endobj … font ☺
  67. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype_ >> >> >> >> endobj Its font type is...
  68. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1_ >> >> >> >> endobj …(Adobe) Type1...
  69. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont_>> >> >> >> endobj ...and its name is...
  70. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial_>> >> >> >> endobj .../Arial.
  71. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> _ >> endobj One thing is missing in our page...
  72. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents_ >> endobj The actual page contents...
  73. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R_ >> endobj … as a reference to another object.
  74. xref %xref table here trailer << /Root 1 0 R

    >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj That’s all for our page object.
  75. Recap: object 3 defines a /Page, its /Parent, /Resources (fonts)

    and its /Contents is in another object. (thank you Mario!)
  76. Def: stream objects So far, everything is text. How do

    you store binary data (images,...) ?
  77. 1 0 obj … endobj Stream objects are objects. They

    start and they end like any other object: Ex: .
  78. Stream objects contain a stream. between stream and endstream keywords

    1 0 obj stream <stream content> endstream endobj
  79. Streams can contain anything Yes, really! Even binary, other file

    formats... (except the endstream keyword)
  80. Stream parameters are stored before the stream. a dictionary after

    obj, before stream required: stream length optional: compression algorithm, etc…
  81. _ %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2

    0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  82. 4 0 obj _ endobj %PDF-1.3 1 0 obj <<

    /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj We create a /Content object... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  83. 4 0 obj stream _ endstream endobj %PDF-1.3 1 0

    obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj ...that is a stream object... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  84. 4 0 obj stream _ endstream endobj %PDF-1.3 1 0

    obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj Text objects are delimited by BT and ET... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  85. 4 0 obj stream BT _ ET endstream endobj %PDF-1.3

    1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj ...(BeginText & EndText). xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  86. 4 0 obj stream BT Tf_ ET endstream endobj %PDF-1.3

    1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj We need to set a font, with Tf. xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  87. 4 0 obj stream BT _ Tf ET endstream endobj

    %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj It takes 2 parameters: a font name... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  88. 4 0 obj stream BT /F1_ Tf ET endstream endobj

    %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj ...(from the page’s resources)... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  89. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj stream BT /F1 100_Tf ET endstream endobj ...and a font size. xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  90. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj stream BT /F1 100 Tf _ ET endstream endobj We move the cursor... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  91. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj stream BT /F1 100 Tf Td_ ET endstream endobj ...with the Td operator... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  92. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj stream BT /F1 100 Tf _ Td ET endstream endobj ...that takes 2 parameters... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  93. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj stream BT /F1 100 Tf 10 400_Td ET endstream endobj ...x and y coordinates. (default page size: 612x792) xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  94. 4 0 obj stream BT /F1 100 Tf 10 400

    Td _ ET endstream endobj Showing a text string... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  95. 4 0 obj stream BT /F1 100 Tf 10 400

    Td Tj_ ET endstream endobj ...is done with the Tj operator... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  96. 4 0 obj stream BT /F1 100 Tf 10 400

    Td _ Tj ET endstream endobj ...that takes a single parameter... xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  97. 4 0 obj stream BT /F1 100 Tf 10 400

    Td (_) Tj ET endstream endobj ...a literal string. xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  98. 4 0 obj stream BT /F1 100 Tf 10 400

    Td (Hello World_) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  99. 4 0 obj stream BT /F1 100 Tf 10 400

    Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Our contents stream is complete... %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  100. 4 0 obj stream BT /F1 100 Tf 10 400

    Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  101. 4 0 obj _ stream BT /F1 100 Tf 10

    400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF One last thing... %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  102. 4 0 obj << _ >> stream BT /F1 100

    Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...we need to set its parameters... %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  103. 4 0 obj << /Length_ >> stream BT /F1 100

    Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF … the stream length... %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  104. 4 0 obj << /Length 44_>> stream BT /F1 100

    Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …including white space (new lines characters…). %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  105. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Our stream parameters are finished... %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  106. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...so our page contents object is finished. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj
  107. Recap: obj 4 is a stream object with a set

    length, defining the page’s contents: declare text, set a font and size, move cursor, display text.
  108. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj << /Length 44 >> stream BT /F1 100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Our PDF defines 4 objects, starting at index 1...
  109. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...but PDFs always have an object 0, that is null...
  110. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref %xref table here trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...so 5 objects, starting at 0.
  111. Warning: offsets & EOLs We have to define offsets, which

    are affected by the EOL conventions: 1 char under Linux/Mac, 2 under Windows. (I use 1 char newlines character here)
  112. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Let’s edit the XREF table!
  113. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF The next line defines the starting index...
  114. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...and the number of objects.
  115. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Then, one line per object...
  116. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...following the xxxxxxxxxx yyyyy a format (10 digits, 5 digits, 1 letter).
  117. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF The first parameter is the offset (in decimal) of the object...
  118. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...(for the null object, it’s 0).
  119. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Then, the generation number (that is almost always 0)...
  120. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...but for object 0, it’s 65535.
  121. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Then, a letter, to tell if this entry is free (f) or in use (n).
  122. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Lastly, each line should take 20 bytes, including EOL...
  123. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...so add a trailing space.
  124. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Next line (the first real object)...
  125. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …object offset, in decimal...
  126. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …generation number...
  127. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n_ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …and declare the object index in use (n)...
  128. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …and the trailing space
  129. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF Do the same with the other objects...
  130. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 00000 n 00000 n 00000 n _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF …knowing that all lines will end with “ 00000 n ”,...
  131. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n _ trailer << /Root 1 0 R >> startxref %xref pointer %%EOF ...set all offsets.
  132. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R >> startxref %xref pointer %%EOF The cross-reference table is finished.
  133. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  134. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R >> startxref %xref pointer %%EOF
  135. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R >> startxref _ %%EOF We set the startxref pointer...
  136. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R >> startxref 364_ %%EOF ...as xref’s offset, in decimal (no prepending 0s).
  137. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R >> startxref 364 %%EOF
  138. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R _ >> startxref 364 %%EOF We also need to update the trailer dictionary...
  139. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R /Size_ >> startxref 364 %%EOF ...with the number of objects...
  140. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R /Size 5_>> startxref 364 %%EOF … in the PDF (including object 0).
  141. 4 0 obj << /Length 44 >> stream BT /F1

    100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R /Size 5 >> startxref 364 %%EOF Our PDF is now complete.
  142. %PDF-1.3 1 0 obj << /Type /Catalog /Pages 2 0

    R >> endobj 2 0 obj << /Type /Pages /Kids [ 3 0 R ] /Count 1 >> endobj 3 0 obj << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Arial >> >> >> /Contents 4 0 R >> endobj 4 0 obj << /Length 44 >> stream BT /F1 100 Tf 10 400 Td (Hello World!) Tj ET endstream endobj xref 0 5 0000000000 65535 f 0000000010 00000 n 0000000060 00000 n 0000000120 00000 n 0000000269 00000 n trailer << /Root 1 0 R /Size 5 >> startxref 364 %%EOF
  143. Disclaimer: this is a minimal PDF. Most PDF documents are

    much bigger, and contain many more elements. Our PDF: 528 bytes 4 objects text only A standard generated “Hello World”: 15 kiloBytes 20 objects text and binary (embedded fonts…)
  144. No need to type them yourself! Hint: use “mutool clean”

    to fix offsets and lengths. http://www.mupdf.com/
  145. ⇒ mutool version Slightly different content, but same rendering. %PDF-1.3

    %%μῦ 1 0 obj <</Type/Catalog/Pages 2 0 R>> endobj 2 0 obj <</Type/Pages/Kids[3 0 R]/Count 1>> endobj 3 0 obj <</Type/Page/Parent 2 0 R/Resources 5 0 R/Contents 4 0 R>> endobj 4 0 obj <</Length 49>> stream q BT /F1 100 Tf 10 400 Td (Hello World!) Tj ET Q endstream endobj 5 0 obj <</Font<</F1<</Type/Font/Subtype/Type1/BaseFont/Arial>>>>>> endobj xref 0 6 0000000000 65536 f 0000000018 00000 n 0000000064 00000 n 0000000116 00000 n 0000000191 00000 n 0000000288 00000 n trailer <</Size 6/Root 1 0 R>> startxref 364 %%EOF
  146. Hint: you can directly extract the PDF sources. use “pdftotext

    --layout” on the slide deck http://www.foolabs.com/xpdf/home.html
  147. Def: stream filters streams can be encoded and/or compressed algorithms

    can be cascaded ex: compression, then ASCII encoding
  148. New stream parameter: /Filter ex: encode the stream in ASCII

    1 0 obj << /Length 12 >> stream Hello World! endstream endobj 1 0 obj << /Length 24 /Filter /ASCIIHexDecode>> stream 48656C6C6F20576F726C6421 endstream endobj ⇔
  149. Ex: compression (deflate = ZIP compression) 1 0 obj <<

    /Length 12 >> stream Hello World! endstream endobj 1 0 obj << /Length 20 /Filter /FlateDecode>> stream x£¾H═╔╔¤/╩IQ♦ ∟I♦> endstream endobj ⇔
  150. Filters can be cascaded. Ex: compressed, then encoded in ASCII

    1 0 obj << /Length 12 >> stream Hello World! endstream endobj 1 0 obj << /Length 40 /Filter [/ASCIIHexDecode /FlateDecode] >> stream 789CF348CDC9C95708CF2FCA495104001C49043E endstream endobj ⇔
  151. Hint: “mutool clean -d” to remove any stream filter. (if

    you want to explore PDFs by yourself) http://www.mupdf.com/