Cấu Trúc Bên Trong File PDF: Header, Body, Bảng xref Và Trailer
Khám phá cấu trúc nhị phân của tệp PDF theo tiêu chuẩn ISO 32000: các đối tượng gián tiếp, bảng xref và cơ chế Fast Web View.
Portable Document Format (PDF), created by Adobe in 1993 and later standardized as ISO 32000, was designed to represent documents independently of application software, hardware, and operating systems. Understanding its internal object structure is critical for developers building custom PDF parsers, generators, and forensics tools.
The 4 Physical Sections of a PDF File
Every valid PDF file consists of four major physical regions:
+-------------------------------+
| Header (%PDF-1.4) |
+-------------------------------+
| Body (Indirect Objects) |
| - Pages, Fonts, Streams |
+-------------------------------+
| Cross-Reference Table (xref) |
| - Byte offsets of objects |
+-------------------------------+
| Trailer |
| - Root pointer & %%EOF |
+-------------------------------+
- Header: The very first line identifies the PDF specification version (e.g.
%PDF-1.4). - Body: A collection of indirect objects (dictionaries, arrays, numbers, and compressed binary streams).
- Cross-Reference Table (xref): A table mapping each object number to its exact byte offset from the start of the file. This enables PDF readers to perform random-access seeking without parsing the entire file sequentially.
- Trailer: Points to the catalog dictionary (
Root) and specifies the byte offset of thexreftable, ending with the magic marker%%EOF.
Download our structurally pure 1MB PDF file to inspect xref tables and object streams in a hex editor.
Tải File Mẫu 1MB PDF →Inspecting a PDF via Hex / Plaintext
You can view the uncompressed ASCII components of a PDF with any text or hex viewer:
%PDF-1.4
%âãÏÓ
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<<
/Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
>>
endobj
Linearized ("Fast Web View") PDFs
Standard PDFs must be downloaded completely before a reader can locate the trailer at the end. Linearized PDFs restructure internal objects so the first page and an initial hint stream appear at the start of the file, allowing web browsers to render Page 1 before the remaining megabytes have finished downloading.
Nguyễn Đại Long
Tác GiảBackend Lead • Chuyên gia Kiến trúc Hệ thống Phân tán & Lưu trữ Đám mây
Hơn 4 năm kinh nghiệm thiết kế các hệ thống xử lý tệp tải lên thông lượng lớn, tối ưu hóa cơ sở dữ liệu và hạ tầng phân tán Cloudflare R2 / AWS S3. Người sáng lập FileDummy và Mạng lưới Hệ sinh thái NDL.
Bài viết này có hữu ích không?
Bấm Thích để ủng hộ tác giả và giúp bài viết lan tỏa tới cộng đồng lập trình viên.
Thảo Luận Kỹ Thuật & Đóng Góp Ý Kiến (0)
Chia sẻ kết quả benchmark, phản hồi các trường hợp biên hoặc đặt câu hỏi chuyên môn.
Chưa có bình luận nào. Hãy là lập trình viên đầu tiên bắt đầu cuộc thảo luận!
Câu Hỏi Thường Gặp (FAQ)
What is a PDF cross-reference (xref) table?▾
The xref table is an index at the end of a PDF mapping each object number to its byte offset. PDF readers jump to the xref to find any object without scanning the entire file — enabling fast random access to any page of a 1000-page document.
Why do PDF files start with %PDF-1.x?▾
The %PDF-1.x header is the PDF magic bytes signature identifying the format and PDF specification version (1.4, 1.7, 2.0). The second line with high-byte characters signals that the file contains binary data, preventing text-mode processing by tools.
What is a content stream in a PDF?▾
A content stream is a sequence of PDF operators that describe the visual appearance of a page — text positions, font selections, image placements, and vector drawing commands. It is stored as a compressed (usually zlib/deflate) binary stream within the PDF object structure.