技術記事

PDF ドキュメント構造の詳細な調査

· PDF構造

PDF ドキュメント構造の詳細

PDF の内部には、カタログ、ページツリー、リソース、メタデータ、トレーラー辞書など、文書全体を制御する複数の構造があります。これらを理解すると、ページ欠落、順序の不一致、メタデータの不整合を調査しやすくなります。

PDF ドキュメント構造の概要図
ヘッダー、本文、相互参照、トレーラーの関係。

トレーラー辞書とカタログ

トレーラー辞書は PDF の入口です。Root からドキュメント カタログへ進み、そこからページツリー、アウトライン、メタデータ、名前辞書などへ到達します。

トレーラーとカタログ 1

文書の入口となる辞書構造の例です。

1
2
3
4
5
6
<<
    /Size 421
    /Root 377 0 R
    /Info 375 0 R
    /ID [<5sazn0fs3tamppia2izf569h281104ae> <6cig0wa61ti593bzuwy41905tr6s5c5a>]
>>

トレーラーとカタログ 2

文書の入口となる辞書構造の例です。

1
2
3
4
5
6
7
8
9
10
<<
    /ModDate (D:20060926213913+02'00')
    /CreationDate (D:20060926213913+02'00')
    /Title (Product Catalog - UK Edition)
    /Creator (QuarkXPress: pictwpstops filter 1.0)
    /Producer (Acrobat Distiller 6.0 for Macintosh)
    /Author (James Smith)
    /Subject (Quarterly Product Showcase)
    /Keywords (products, catalog, prices, specifications)
>>

トレーラーとカタログ 3

文書の入口となる辞書構造の例です。

1
2
/MediaBox [0 0 595 842]    # A4 size in points (8.27" × 11.69")
/CropBox [50 50 545 792]   # A4 with 50-point margins on all sides

ページツリーと座標系

ページは単純な配列ではなく、ツリー構造で管理されます。各ページは MediaBox、CropBox、Resources、Contents などを持ち、親ノードから値を継承する場合もあります。

ページ構造 1

ページ辞書とページツリーの関係を示す例です。

1
2
3
4
5
6
7
8
1 0 obj  % Root node
<< /Type /Pages /Kids [2 0 R 3 0 R 4 0 R] /Count 7 >> endobj
 
2 0 obj  % Intermediate node
<< /Type /Pages /Kids [5 0 R 6 0 R 7 0 R] /Parent 1 0 R /Count 3 >> endobj
 
5 0 obj  % Actual page
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << >> >> endobj

ページ構造 2

ページ辞書とページツリーの関係を示す例です。

1
2
3
4
If bytes[0] == 254 AND bytes[1] == 255:
    encoding = "UTF-16BE"  # Unicode byte-order marker U+FEFF
else:
    encoding = "PDFDocEncoding"  # Default PDF encoding

ページ構造 3

ページ辞書とページツリーの関係を示す例です。

1
(D:YYYYMMDDHHmmSSOHH'mm')

文字列、日付、メタデータ

PDF では PDFDocEncoding、UTF-16BE、日付文字列など、独自の表現が使われます。文字化けや日付解釈の誤りを避けるには、形式ごとの扱いを分ける必要があります。

補足構造 1

文字列、日付、ページ構造を扱う補足例です。

1
2
3
(D:20250625132712+08'00')  # June 25, 2024, 1:27:12 PM, UTC+8 (Beijing)
(D:20250625132712-05'00')  # Same moment in Eastern Standard Time
(D:20250625132712Z)        # Same moment in UTC (Zulu time)

補足構造 2

文字列、日付、ページ構造を扱う補足例です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
%PDF-1.0  % Header
 
1 0 obj  % Document catalog
<< /PageLayout /TwoColumnLeft /Pages 2 0 R /Type /Catalog >> endobj
 
2 0 obj  % Root of page tree
<< /Kids [3 0 R 4 0 R] /Type /Pages /Count 3 >> endobj
 
3 0 obj  % Page one
<<
    /Type /Page
    /Parent 2 0 R
    /MediaBox [0 0 612 792]  % US Letter size
    /Resources << /Font << /F0 << /BaseFont /Times-Roman /Subtype /Type1 /Type /Font >> >> >>
    /Contents [5 0 R]
>> endobj
 
4 0 obj  % Intermediate page tree node
<< /Parent 2 0 R /Kids [6 0 R 7 0 R] /Count 2 /Type /Pages >> endobj
 
5 0 obj  % Content stream for page one
<< /Length 58 >>
stream
BT /F0 24 Tf 50 750 Td (Hello, PDF World!) Tj ET
endstream endobj
 
6 0 obj  % Page two
<<
    /Type /Page
    /Parent 4 0 R
    /MediaBox [0 0 612 792]
    /Rotate 90  % Landscape orientation
    /Resources << /Font << /F0 << /BaseFont /Times-Roman /Subtype /Type1 /Type /Font >> >> >>
    /Contents [8 0 R]
>> endobj
 
7 0 obj  % Page three
<<
    /Type /Page
    /Parent 4 0 R
    /MediaBox [0 0 612 792]
    /Resources << /Font << /F0 << /BaseFont /Times-Roman /Subtype /Type1 /Type /Font >> >> >>
    /Contents [9 0 R]
>> endobj
 
8 0 obj  % Content stream for page two
<< /Length 72 >>
stream
BT /F0 18 Tf 50 700 Td (This page is rotated 90 degrees) Tj ET
endstream endobj
 
9 0 obj  % Content stream for page three
<< /Length 45 >>
stream
BT /F0 24 Tf 50 750 Td (Final page) Tj ET
endstream endobj
 
10 0 obj  % Document information dictionary
<<
    /Title (PDF Structure Example)
    /Author (PDF Guide Author)
    /Producer (Manual Creation)
    /CreationDate (D:20240625132712+08'00')
    /ModDate (D:20240625133045+08'00')
    /Subject (Demonstrating PDF internal structure)
    /Keywords (PDF, structure, example, tutorial)
>> endobj
 
xref  % Cross-reference table
0 11
0000000000 65535 f
0000000015 00000 n
0000000074 00000 n
0000000120 00000 n
0000000355 00000 n
0000000415 00000 n
0000000522 00000 n
0000000747 00000 n
0000000958 00000 n
0000001079 00000 n
0000001173 00000 n
 
trailer  % Trailer dictionary
<<
    /Size 11
    /Root 1 0 R
    /Info 10 0 R
    /ID [<A1B2C3D4E5F6789012345678901234AB> <A1B2C3D4E5F6789012345678901234AB>]
>>
startxref
1456
%%EOF

実装上の注意

  • ページ数は Count だけでなく実際の Kids 構造も確認する。
  • リソース継承を考慮してフォントや画像を解決する。
  • アウトラインや注釈はページ参照と整合しているか検証する。
  • メタデータの文字エンコーディングを明示的に扱う。

PDF 構造を階層として読み解くことで、表示結果だけでは見えない問題を正確に診断できます。