Technical Article

Diepgaande verkenning van de PDF-documentstructuur

· PDF-structuur

Een diepgaande gids voor de interne structuur van PDF-documenten, inclusief objecten, pagina’s en verwijzingen.

Dit artikel is bedoeld voor ontwikkelaars die met pdf-structuur werken. Productnamen, API-namen, bestandsnamen en codefragmenten zijn bewust ongewijzigd gehouden, zodat de voorbeelden direct met de oorspronkelijke documentatie en broncode te vergelijken zijn.

Overzicht

De pagina beschrijft het probleemgebied, de relevante implementatiekeuzes en de controles die belangrijk zijn voordat de oplossing in een echte toepassing wordt gebruikt.

Codevoorbeeld

Het onderstaande codefragment is ongewijzigd uit de Engelse bron overgenomen om identifiers, API-aanroepen en syntaxis exact te behouden.

1
2
3
4
5
6
<<
    /Size 421
    /Root 377 0 R
    /Info 375 0 R
    /ID [<5sazn0fs3tamppia2izf569h281104ae> <6cig0wa61ti593bzuwy41905tr6s5c5a>]
>>
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)
>>
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
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
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
1
(D:YYYYMMDDHHmmSSOHH'mm')
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)
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

Praktische aandachtspunten

  • Controleer invoerbestanden en foutpaden expliciet voordat u de routine in productie gebruikt.
  • Houd paginalay-out, lettertypen en coördinaten reproduceerbaar, vooral bij server-side verwerking.
  • Test het resultaat in meer dan één PDF-viewer wanneer rendering, annotaties of interactieve elementen belangrijk zijn.
  • Laat componentlevensduur en bestandshandles altijd via try/finally-achtige patronen opruimen.