# 1. Basic Struture of PDF

Portable Document Format (PDF) là tiêu chuẩn hướng tới khi bạn muốn đảm bảo your document trông giống nhau ở mọi nơi, bất kể thiết bị nào đã mở tệp.

PDF had been standardized as ISO-32000 in 2008.

Trước khi bắt đầu tìm hiểu bài này thì ta sẽ làm quen một số thuật ngữ cho bài này nhé:

* `Objects` - nó không phải là các đối tượng theo nghĩa OOP mà là kiểu dữ liệu tiêu chuẩn của PDF. There are 9 types of objects (null, boolean, integer, real, name, string, array, dictionary and stream)
* `Dictionary`  -  một cặp đối tượng key-value không có thứ tự. Chúng được ký hiệu là << và >> ở đầu và cuối.
* `Indirect Objects` - là đối tượng được đề cập bằng cách tham chiếu (thông qua các khác, gián tiếp)
* `Direct Objects` - là đối tượng appear inline and được lấy trực tiếp
* `Conforming Reader` - Application phân tích tệp PDF theo Tiêu chuẩn PDF.

## Structure of PDF file

Để đơn giản thì 1 file PDF được tạo thành từ 4 phần:

<figure><img src="/files/BbErPM3Ev8UFE2aHdVL7" alt=""><figcaption></figcaption></figure>

### Header

The first line of the PDF file is a `Header` - điều này biểu thị version of the PDF file with the following format:

* `%PDF-` followed by the version number in the form of `1.N`

<figure><img src="/files/BmW1Nuabmg2PbTUFigZK" alt=""><figcaption></figcaption></figure>

### Body

Phần này bao gồm các `Indirect Objects` (đối tượng gián tiếp) thể hiện nội dung of a document.

`Indirect Objects` bắt đầu bằng một mã định dang duy nhất cho phép các đối tượng khác tham chiếu đến nó. Mã định danh này được tạo thành từ:

* `Object Number` (số nguyên dương, có thể theo thứ tự tùy ý)
* `Generation Number` (số nguyên không âm)

Indirect Objects có thể được tham chiếu từ nơi khác bằng Indirect Reference bao gồm: `Object Number`, `Generation Number` and keyword `R` (for example `4 0 R`)

Sau mã định danh sẽ là keyword `obj` (start of the object) and `endobj` (end of the object), mọi thứ ở giữa sẽ là cặp `key-value` mô tả đối tượng

```
1 0 obj % Object Number 1, Generation Number 0
<<
/Type /Pages % Describe type of object
/Kids [ 4 0 R ] $ Kids Entry referring to an indirect reference (Object number 4, Generation number 0)
/Count 1
>>
endobj

2 0 obj % Object Number 2, Generation Number 0
<<
/Type /Catalog % Describe type of object
/Pages 1 0 R % Referring another object via unique object identifier
>>
endobj
```

Nói một cách đơn giản, `Body` of a PDF file là một cây gồm các đối tượng được liên kết với nhau, cuối cùng đi xuống `Root Object` (được xác định bởi `Root` lối vào `Trailer`, is a `catalog` dictionary)

### Cross-Reference Table

Về cơ bản, là một bảng chứa danh sách byte offset trỏ đến `indirect objects`. Trình đọc tuân thủ sử dụng bảng này như  một bảng tra cứu để truy cập nhanh chóng các đối tượng nhấn định khi cần.

Format:

* In the following format `nnnnnnnnnn ggggg n eol` , a total of 20 bytes
* `nnnnnnnnnn` is a 10-digit byte offset in the decođe stream
* `ggggg` is 5-digit generation number
* `n` keyword for in-use entry or `f` keyword for free entry
* `eol` 2 character end-of-line sequence (like `CR LF`)

`Cross-Reference Table` always start with a special entry `0000000000 65535 f` that never changes

```
xref
0 7 % total of 7 entries in table, starting with object number 0
0000000000 65535 f % special entry, f denoting it is a free entry
0000000016 00000 n % offset of object 0 is 16 byte, in-use entry
0000000076 00000 n % offset of object 1 is 76 byte, in-use entry
0000000126 00000 n % offset of object 2 is 126 byte, in-use entry
0000000596 00000 n 
0000000791 00000 n 
0000000889 00000 n 
```

### Trailer

`Trailer` shows the location of `Cross-Reference Table` and một số đối tượng đặc biệt nhất định (certain special objects). Một trình đọc phù hợp luôn read a PDF file from its end, do đó nó có thể truy cập `Cross-Reference Table` và `Indirect Objects` khác một cách nhanh chóng mà không cần phân tích toàn bộ tệp.

The `Trailer` is basically another key-value pair `Dictionary` with the following format:

```
 trailer
 <<
   key1 value1
   key2 value2
   ...
   keyn valuen
 >>
 startxref
 Byte_offset_of_last_cross-reference_section
 %%EOF
```

Following is an example of a `Trailer` dictionary:

```
trailer
<<
/Size 7 % Number of entries in Cross-Reference Table
/Root 2 0 R % Showing the location of Catalog Dictionary
/Info 3 0 R % Showing the location of the Information Dictionary
>>

startxref
1088 % Byte offset to the Cross-Reference Table
%%EOF
```

The Trailer dictionary has the following keys:

* `/Size` - Total number of entries in `Cross-Reference Table`
* `/Prev` - chỉ được sử dụng khi có nhiều hơn `Cross-Reference Table`. Đó là byte offset trong luồng được giải mã from the beginning of the file to the beginning of the previous `Cross-Reference Section`
* `/Root` - `Catalog` dictionary for the PDF (Can be a `dictionary` or `indirect reference`)
* `/Encrypt` - `Encryption` dictionary, if exists, the PDF file is encrypted (Can be a `dictionary` or `indirect reference`)
* `/Info` - `Information` dictionary containing general information on the file (Must be `indirect reference`)
* `/ID` - Array of two byte-strings constituting a file identifier. (If Encrypt entry exists, then this entry is required and shall be direct objects)

***

Nguồn: <https://medium.com/aia-sg-techblog/basic-structure-of-portable-document-format-pdf-79db682579c9>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viettaliii.gitbook.io/home/education/other/1.-basic-struture-of-pdf.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
