Technical Article

PDF-tekst en lettertypen verwerken met codevoorbeelden en best practices

· PDF-structuur

Een ontwikkelaarsgids voor PDF-tekst, lettertypen, codering en praktische verwerkingspatronen.

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
7
8
9
10
11
BT
/F0 24 Tf
1 0 0 1 50 700 Tm
(Normal text spacing) Tj
0 -30 Td
3 Tc
(Character spacing = 3 points) Tj
0 -30 Td
-1 Tc
(Tight character spacing = -1 point) Tj
ET
1
2
3
4
5
6
7
8
9
10
11
BT
/F0 24 Tf
1 0 0 1 50 600 Tm
(Normal word spacing) Tj
0 -30 Td
10 Tw
(Extended word spacing improves readability) Tj
0 -30 Td
-2 Tw
(Compressed word spacing saves space) Tj
ET
1
2
3
4
5
6
7
8
9
10
11
12
BT
/F0 24 Tf
1 0 0 1 50 500 Tm
100 Tz
(Normal horizontal scaling - 100%) Tj
0 -30 Td
80 Tz
(Condensed text - 80% scaling) Tj
0 -30 Td
120 Tz
(Extended text - 120% scaling) Tj
ET
1
2
3
4
5
6
7
8
9
10
BT
/F0 18 Tf
18 TL
1 0 0 1 50 400 Tm
(This text uses 18pt leading) Tj T*
(which matches the font size) Tj T*
24 TL
(This text uses 24pt leading) Tj T*
(providing more generous spacing) Tj T*
ET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BT
/F0 36 Tf
1 0 0 1 140 290 Tm
(H) Tj
-8 Ts
/F0 24 Tf
(2) Tj
0 Ts
/F0 36 Tf
(O represents water with O) Tj
8 Ts
/F0 24 Tf
(2) Tj
0 Ts
/F0 36 Tf
( as oxygen) Tj
ET
1
2
3
4
5
6
7
8
9
10
11
% Set up rotation transformation
0.96 0.25 -0.25 0.96 0 0 cm
BT
/F0 48 Tf
48 TL
% Set text matrix for positioning
1 0 0 1 270 240 Tm
(Text and graphics) Tj T*
(transforms combined) Tj T*
(with proper newlines) Tj
ET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
BT
% Select font and set initial size
/F0 12 Tf
1 0 0 1 50 750 Tm
(12-point font example) Tj
% Change to larger size, same font
/F0 18 Tf
0 -25 Td
(18-point font example) Tj
% Even larger size
/F0 24 Tf
0 -35 Td
(24-point font example) Tj
% Largest size
/F0 36 Tf
0 -50 Td
(36-point font example) Tj
ET
1
2
3
4
5
6
7
8
9
BT
/F0 48 Tf
1 0 0 1 100 400 Tm
% Standard text rendering
(WAVE Type) Tj
0 -60 Td
% Kerned text with precise adjustments
[(W) -120 (A) -80 (V) -100 (E) 50 (T) -20 (y) -10 (p) -5 (e)] TJ
ET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BT
/F0 36 Tf
1 0 0 1 100 500 Tm
% Standard filled text
0 Tr
(Filled Text) Tj
0 -50 Td
% Stroked text only
1 Tr
2 w
(Stroked Text) Tj
0 -50 Td
% Both filled and stroked
2 Tr
(Filled and Stroked) Tj
ET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
% Example ToUnicode CMap structure
23 0 obj
<< /Length 317 >>
stream
/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
/CMapName /Adobe-Identity-UCS def
1 begincodespacerange
<0001>
endcodespacerange
2 beginbfchar
<0001> <0041>  % Map glyph 1 to Unicode U+0041 (A)
<0002> <0042>  % Map glyph 2 to Unicode U+0042 (B)
endbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end end
endstream
endobj
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
% Complex text positioning that challenges extraction
BT
/F0 12 Tf
1 0 0 1 72 720 Tm
(This text appears) Tj
150 0 Td
(out of order) Tj
-150 -15 Td
(in the content stream) Tj
200 0 Td
(but should be) Tj
-200 -15 Td
(reconstructed properly) Tj
100 0 Td
(by extraction algorithms.) Tj
ET
 
% Graphics elements that interrupt text flow
q
1 0 0 1 100 650 cm
0.5 g
0 0 200 50 re f
Q
 
% Continuation of text after graphics
BT
/F0 12 Tf
1 0 0 1 72 630 Tm
(Text continues after graphics elements) Tj
ET

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.