Yesterday, Mark wrote about how wrong references tables can be. He’s also previously written about how to edit PDF files by adding a second reference table, and Daniel made a whole series on making your own PDF file.
I thought it might be fun to put the theory into practise and edit Daniel’s Hello World PDF by adding some new objects and a new reference table and trailer. Let’s jump in!
Here’s the file at the end of Daniel’s Hello World article:
The idea of additional trailers is that you don’t have the edit the original file – you simply include all of the new and changed objects after the original trailer, and then include a new trailer at the end which also points to the original trailer. So lets add an annotation object and update the page object to include it:
7 0 obj<</Type /Annot /Subtype /Text /Rect [175 720 175 720] /Contents (Hello Annotation!) /Open true>> endobj 3 0 obj<</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents 6 0 R /Annots [7 0 R]>> endobj
I’ve put each object on multiple lines here for readability’s sake, but kept them on a single line in the actual file.
Now we just need a new trailer. The first step for this is an additional xref table. This will list the new or updated objects along with their byte offset (which I’ll find using our in-house BinaryTool, although a hex editor would work too), their revision number (which will in this case be 0) and whether they are free or used (in this case ‘n’ for used). The reference table must also store a linked list of free object numbers, which means we must also include an entry for object 0.
xref 0 1 0000000000 65535 f 3 1 0000000744 00000 n 7 1 0000000631 00000 n
Next up is the trailer. This will be much like the last, except for the fact the size key should now have the value of 8, since we’ve added an object, and we need to add a /Prev key to the original xref table.
trailer <</Size 8 /Root 1 0 R /Prev 406>>
Now all we need to is add in a pointer to our new xref table and mark the new end of the file.
startxref 863 %%EOF
Load it up in Reader, and voilà! One annotation.
Of course, annotations are just one example of ways you can use additional trailers – others include adding or removing page elements, or saving the data in forms. Keep reading for some news on that front in the coming months!
Are you a Developer working with PDF files?
Our developers guide contains a large number of technical posts to help you understand the PDF file Format.
Do you need to solve any of these problems?
|Display PDF documents in a Web app|
|Use PDF Forms in a web browser|
|Convert PDF Documents to an image|
|Work with PDF Documents in Java|