Part 3: Create a Non-Working PDF (click to expand)
In the previous article of this series, we learn to use a text editor to structure and create a PDF file. The only problem with the PDF we are going to make is that it is not going to work. It will however give us an 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 surprising, 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-2.0. 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-2.0
1 0 obj << >>
endobjNext 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.
xref
0 2
0000000000 65535 f
0000000010 00000 nNext you need the final part which is the trailer section:
trailer << /Size 2 /Root 1 0 R >>
startxref
33
%%EOFIf 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.”
Our software libraries allow you to
| Convert PDF files to HTML |
| Use PDF Forms in a web browser |
| Convert PDF Documents to an image |
| Work with PDF Documents in Java |
| Read and write HEIC and other Image formats in Java |