Make Your Own PDF – Part 2b: Create your own non-working PDF

This article is part of a 7 part series to create a hello world PDF. Click here to visit the series index.

In this part we going to use what we learnt in Part 2: Structure of a PDF file to create a PDF file using a text editor.  The only problem with the PDF we are going to make is that it is not going to work.  It will however give us a error message we can understand in Acrobat PDF viewer.  This is going to form the basis for creating a working PDF file in the posts that follow.  The ingredients you require are: a text editor, a hex editor (I’m going to use HxD) and a at least partially functioning human brain.  Preferably your own.

We are going to create all the parts I mentioned in the last article in a text editor and figure out the address of the things we put in our file using the HxD.  We can also see what error messages we can produce from Acrobat.

Firstly I’m gonna make a new blank file called myPdf.pdf.  Just because I can I’m gonna load it in Acrobat to see what it says:

“Adobe Reader could not open ‘myPdf.pdf’ because it is either not a supported file type or because the file has been damaged.”

Hardly suprising, but if you get this message from a supposedly working PDF in the future you can be sure its a bit knackered.

Now I’m adding the header part, which only requires a version number in the form: %PDF-1.3.  Next we have the body sections where all the objects go.  For this section we’re just have one object:  Object number 1 and its going to be a dictionary object (that we are not going to put anything in…yet!).

%PDF-1.3
1 0 obj << >>
endobj

Next we want the Cross Reference Table section.  First we need the xref keyword.  Then the number of the first object in our list and the amount of objects in our file.  So far we have two objects:  1 0 obj that is in our body section and object 0 which is the head of the linked list that I described in Part 2.  So we end up with a line with 0 2 on it.  The entries that follow have the information about our objects.  They all have the same format which is 10 characters, a space, 5 characters, a space and then a letter describing what kind of object it is.

xref
0 2
0000000000 65535 f
0000000010 00000 n

Notice I’ve put 10 as the address of object 1.  As each letter is a byte its pretty easy to count up %PDF-1.3 plus return characters, but if you want to check you can open your file in HxD (set the width box to 10 and the number system to decimal to make life easier) and click on the 1 of 1 0 obj to get the starting address of 1 0 obj.

Next you need the final part which is the trailer section.  You need a startxref then a trailer dictionary with the size in objects of the file and a reference to the root object:

trailer <</Size 2/Root 1 0 R>>
startxref

Then you need the address from the Cross Reference Table (where the xref keyword starts in bytes) which is 32 on mine.  Finish of the file with %%EOF.  So you end up with:

%PDF-1.3
1 0 obj << >>
endobj
xref
0 2
0000000000 65535 f
0000000010 00000 n
trailer <</Size 2/Root 1 0 R>>
startxref
33
%%EOF

If you open this in Acrobat you’ll get a different kind of error.  If you hold down Ctrl while clicking OK you see another part of the error message: “Expected a dict object.”  Which is fair enough as we havent put any values in it.

Next time: DIY Blank Page!

This article is part of a 7 part series to create a hello world PDF. Click here to visit the series index.

Related Posts:

The following two tabs change content below.

Daniel

Developer at IDR Solutions
When not delving into obscure PDF or Java bugs, Daniel is exploring the new features in JavaFX.
Daniel

About Daniel

When not delving into obscure PDF or Java bugs, Daniel is exploring the new features in JavaFX.

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>