How to edit PDF files using Incremental Updates

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 HelloWorld PDF in both Notepad++ and Adobe Reader

The HelloWorld PDF in both Notepad++ and Adobe Reader

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.

The object offsets shown in BinaryTool

The object offsets shown in BinaryTool

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.

The updated file in Notepad++ and Adobe Reader

The updated file in Notepad++ and Adobe Reader

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!

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

Related Posts:

The following two tabs change content below.
Sam is a developer at IDRsolutions who mostly specialises in font support and conversion. He's also enjoyed working with Java 3D, Java FX and Swing. His other interests include music and game design.
SamH

About Sam Howard

Sam is a developer at IDRsolutions who mostly specialises in font support and conversion. He’s also enjoyed working with Java 3D, Java FX and Swing. His other interests include music and game design.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>