Understanding PDF Xref Tables:
Xref tables are part of the original PDF file specification and one of the features which gives the PDF file format its flexibility. If you open a PDF file in a text editor and search for the word ‘xref’ you will find something like this
xref 0 271 0000000000 65535 f 0000000015 00000 n 0000000102 00000 n
This is the xref table. A PDF consists of lots of COS objects and this tells you where they are located in the file. This is actually very useful. A PDF Reader just has to read these values and then it loads the objects only when they are needed. It does not need to parse or load the whole file.
The first line tells you about the table entries. In this case the xref table has 271 entries and the object numbers start at zero. The following lines give the object offset from the start of the file, then the generation number (you can have several revisions of an object) and a flag to say whether the object is in use (n) or not (f). If the PDF file has been edited and objects changed, the changed version is often tagged onto the PDF with an updated xref table showing the new location. So it is possible for a PDF file to contain several xref tables and the later values are used.
If you look at byte offset 15 in the PDF file I took the xref table from you will find the start of object 1
1 0 obj<</Type/Font ...
If you are looking at PDF file created with version 1.6, you may not find an xref entry because they introduced an alternative way to store the objects locations – but that is for another article.
Xref tables also explain why if you alter a byte or add a byte to a PDF file it will become corrupted – all the pointers are now wrong.
This post is part of our “Understanding the PDF File Format” series. In each article, we aim to take a specific PDF feature and explain it in simple terms. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.