Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

What are PDF Xref tables?

1 min read

What are 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. It is found in the trailer. A PDF file can contain multiple linked xref tables which all need to be read. The pointer to the first xref table is usually found at the end of the PDF files in the last 1024 bytes. Linearized PDF files includes some data at the start of the file so you can display the file before it has been fully read.

If you open a PDF file in a text editor and search for the word ‘xref’ you will find something like this

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.5 and above, you may not find an xref entry because they introduced an alternative way to store the objects inside Compressed streams.

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.

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.

Find out more about our software for Developers

Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

5 Replies to “What are PDF Xref tables?”

  1. As part of our process where I work, we split incoming PDFs into individual pages before archiving them. We came across a few that broke the library we used to split them. Looking closer at the xref table I noticed these weird offsets:

    >grep -Eabo "[0-9]{10} [0-9]{5} (n|f)" "brokenpdf.pdf"
    337232:0000225190 00000 n
    337252:0032768500 00000 n
    337272:0000225389 00000 n
    337292:0000225565 00000 n
    337312:0000225763 00000 n
    337332:0000235424 00000 n
    337352:0000235612 00000 n
    337372:0000235815 00000 n
    337392:0000245138 00000 n
    337412:0000245318 00000 n
    337432:0032768500 00000 n
    337452:0000245520 00000 n

    This offset in hex looks like 0x01f401f4, which is suspicious, plus the fact that the PDF itself was only 200K. Then I searched for all the objects in the document:

    >grep -Eabo "[0-9]+ [0-9]+ obj" "brokenpdf.pdf"
    225190:28 0 obj
    225389:30 0 obj
    225565:31 0 obj
    225763:32 0 obj
    235424:33 0 obj
    235612:34 0 obj
    235815:35 0 obj
    245138:36 0 obj
    245318:37 0 obj
    245520:39 0 obj

    Objects 29 and 38 were missing, and where their offsets would have been recorded in the xref table, there was a rediculously large number. Changing the “n” to “f” for these xref entries seemed to fix the issue well enough to split the document.

  2. If I understand correctly on how to update an existing PDF to add a signature:

    1. Get the root from trailer
    2. Get the /Pages from root object
    3. Get the /Kids from the pages object
    4. Recreate the first kid reference, including an /Annots key to point to your objects.

    Is that the proper procedure?

Leave a Reply

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

IDRsolutions Ltd 2022. All rights reserved.