本文转载自:https://blog.csdn.net/u012632138/article/details/80622280
引言
在“PDF学习二 PDF物理结构”的说明里用了较大的篇幅说明了交叉引用表,但是有人表示说,用记事本打开一个PDF文件,并没有看到所谓的交叉引用表。
精华提炼出来的文档的描述有错误么?非也,只是交叉引用表现在使用的比较少了,大部分的pdf文件都在使用交叉引用流
无奈只能开了一个杂谈说明下PDF对象流,对PDF物理结构的一个补充。
正文
先来了解下什么是对象流 Object Streams
来个定义: 从PDF1.5开始新增object stream,包含一系列的PDF对象,类似文件体。
优点:
- 压缩PDF对象
- 减小PDF文件大小
大部分对象都可出现在对象流中,除了以下情况:
- 流对象 Stream Objects。
- 对象的产生号不是0。
- 文档的加密字典。
- 一个对象的Length条目出现在对象流字典中。
以下是解码后的Object Stream例子:
15 0 obj % 对象流 object stream
<< /Type /ObjStm % 类型必需是ObjStm
/Length 1856
/N 3 % 对象流中对象数
/First 24 % 第一个对象的偏移值
>>
stream
% 对象号和相对于第一个对象的偏移值
11 0 12 547 13 665 % 11、12和13是对象号,0、547和665是偏移值
<< /Type /Font % 略去obj和endobj,以<<与>>分隔。
/Subtype /TrueType
...other keys...
/FontDescriptor 12 0 R
>>
接着重点,什么是交叉引用流 Cross-Reference Streams
再来个定义: 从PDF1.5开始新增Cross-Reference Streams,包含一个字典类似物理结构中trailer字典和一个数据流存储交叉引用表的数据。
优点:
- 交叉引用信息更紧凑。
- 能够访问存储在Object Stream中的被压缩的对象,方便增加新的交叉引用信息。
12 0 obj % Cross-reference stream
<< /Type /XRef % 交叉引用流字典替代trailer字典
/Size ... % 类似trailer中的Size有多少对象
/Root ...
/W [1 2 1] % 交叉引用流中数据分布由1字节、2字节、1字节表示
>>
stream
... % 流数据包含交叉引用信息
endstream
endobj
... more objects ...
startxref % startxref 替代xref
byte_offset_of_cross-reference_stream % Points to object 12
%%EOF
交叉引用流中数据含义:

例子:
|
对象与交叉引用表
|
对象流
|
交叉应用流
|
|
1 0 obj % The document root, at offset 23.
<< /Type /Catalog
/StructTreeRoot 3 0 R
…
>>
endobj
12 0 obj
…
endobj
…
99 0 obj
…
endobj
xref % The main xref section, at offset 2664
0 100 % This subsection has entries for objects 0 - 99.
0000000002 65535 f % Entry for object 0
0000000023 00000 n % Entry for object 1, the root
0000000003 65535 f % Entry for object 2 (object stream), marked free in this table
0000000004 65535 f % Entry for object 3, marked free in this table
0000000005 65535 f % …
0000000006 65535 f
0000000007 65535 f
0000000008 65535 f
0000000009 65535 f
0000000010 65535 f
0000000011 65535 f
0000000000 65535 f % Entry for object 11 (xref stream), marked free in this table.
0000000045 00000 n % Entry for object 12, in use.
0000000179 00000 n % Entry for object 13, in use.
…
0000002201 00000 n % Entry for object 99, in use.
trailer
<< /Size 100
/Root 1 0 RSECTION 3.4 File Structure
113
/ID …
>>
startxref
2664 % Offset of the main xref section
%%EOF
|
2 0 obj % The object stream, at offset 3722
<< /Length ...
/N 8 % This stream contains 8 objects.
/First 47 % The stream-offset of the first object
>>
stream
3 0 4 50 5 72 … % The numbers and stream-offsets of the 8 objects
<< /Type /StructTreeRoot % This is object 3.
/K 4 0 R
/RoleMap 5 0 R
/ClassMap 6 0 R
/ParentTree 7 0 R
/ParentTreeNextKey 8
>>
<< /S /Workbook % This is object 4 (K value from StructTreeRoot).
/P 8 0 R
/K 9 0 R
>>
<< /Workbook /Div % This is object 5 (RoleMap).
/Worksheet /Sect
/TextBox /Figure
/Shape /Figure
>>
… % Objects 6 through 10 are defined here.
endstream
endobj
|
11 0 obj % The cross-reference stream, at offset 4899
<< /Type /XRef
/Index [2 10] % This stream contains entries for objects 2 through 11
/Size 100
/W [1 2 1] % The byte-widths of each field
/Filter /ASCIIHexDecode % For readability only (not supported by Acrobat 6)
…
>>
stream
01 0E8A 0 % Entry for object 2 (0x0E8A = 3722)
02 0002 00 % Entry for object 3 (in object stream 2, index 0)
02 0002 01 % Entry for object 4 (in object stream 2, index 1)
02 0002 02 % …
02 0002 03
02 0002 04
02 0002 05
02 0002 06
02 0002 07 % Entry for object 10 (in object stream 2, index 7)
01 1323 0 % Entry for object 11 (0x1323 = 4899)
endstream
endobj
xref % The update xref section, at offset 5640
0 0 % There are no entries in this section.
trailer
<< /Size 100
/Prev 2664 % Offset of previous xref section
/XRefStm 4899
/Root 1 0 R
/ID …
>>
startxref
5640
%%EOF
|
尾声
恶补了下PDF Reference,找到以上说明,不足之处请指出。