Technical Article

PDF-graphics uitgelegd: visuele PDF-elementen begrijpen

· PDF-structuur

Uitleg over PDF-graphics en de visuele elementen waaruit PDF-pagina’s worden opgebouwd.

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
100 100 m    % Move to point (100, 100)
200 200 l    % Draw line to (200, 200)  
300 100 l    % Draw line to (300, 100)
h            % Close the path
S            % Stroke the path
1
2
3
4
200 250 m           % Start at point (200, 250)
300 350 400 450 500 250 c   % Create a Bézier curve
400 250 300 200 y   % Add another curve segment  
h                   % Close the path
1
2
3
4
5
200 250 m           % Start the path
300 350 400 450 500 250 c   % Create curves
400 250 300 200 y   % Complete the shape
h                   % Close the path
f                   % Fill the path
1
2
3
4
5
6
2.0 w              % Set line width to 2 points
200 250 m          % Start the path  
300 350 400 450 500 250 c   % Create the shape
400 250 300 200 y  % Complete curves
h                  % Close path
S                  % Stroke the path
1
2
3
4
5
6
7
8
2.0 w              % Set stroke width
0.8 g              % Set fill to light gray
0 G                % Set stroke to black
200 250 m          % Begin path construction
300 350 400 450 500 250 c
400 250 300 200 y
h                  % Close path
B                  % Fill and stroke
1
2
/DeviceGray cs     % Select DeviceGray color space
0.5 g              % Set gray level to 50%
1
2
3
/DeviceRGB cs      % Select RGB color space  
0.2 0.6 0.8 rg     % Set fill color (blue-green)
0.8 0.2 0.1 RG     % Set stroke color (red)
1
2
/DeviceCMYK cs     % Select CMYK color space
0.2 0.8 0.0 0.1 k  % Set fill color (magenta-heavy)
1
2
3
[a  b  0]
[c  d  0]   where: [x' y' 1] = [x y 1] × CTM
[e  f  1]
1
1 0 0 1 100 50 cm    % Translate by (100, 50)
1
2 0 0 1.5 0 0 cm     % Scale x by 2, y by 1.5
1
0.707 0.707 -0.707 0.707 0 0 cm  % Rotate 45° (π/4 radians)
1
1 0.5 0 1 0 0 cm     % Skew along x-axis
1
2
3
4
5
q                    % Save current graphics state
2 0 0 2 100 100 cm   % Scale by 2 and translate
% ... draw scaled content ...
Q                    % Restore original state
% Continue with original coordinate system
1
2
3
4
5
6
7
8
% Define triangular clipping path
200 100 m
200 500 l  
500 100 l
h              % Close the path
W              % Set as clipping region
S              % Stroke the clipping boundary
% All subsequent drawing is clipped to this triangle
1
2
3
4
5
<< /ExtGState          % Define external graphics state
   << /gs1             % Name for this state collection
      << /ca 0.5 >>    % 50% fill transparency
   >>
>>
1
2
3
/gs1 gs               % Apply transparency state
0.75 g                % Set light gray fill
% Draw semi-transparent content
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/Pattern              % Switch to pattern color space
<<
  /GradientShading    % Pattern name
  <<
    /Type /Pattern
    /PatternType 2    % Shading pattern
    /Shading
    <<
      /ColorSpace /DeviceGray
      /ShadingType 2  % Axial shading
      /Coords [150 200 450 500]  % Start and end points
      /Function << /FunctionType 2 /N 1 /Domain [0 1] >>
      /Extend [true true]
    >>
  >>
>>
1
2
3
/Pattern cs           % Select pattern color space
/GradientShading scn  % Apply our gradient pattern
% Draw shape with gradient fill
1
2
/ShadingType 3        % Radial shading
/Coords [400 400 0 400 400 200]  % Inner and outer circles
1
2
3
4
% Smooth gradient with many color stops
/Function << /FunctionType 3 /Domain [0 1]
              /Functions [func1 func2 func3 func4]
              /Bounds [0.25 0.5 0.75] >>
1
2
% Simple two-color gradient
/Function << /FunctionType 2 /Domain [0 1] /N 1 >>
1
2
3
4
5
6
7
8
9
/XObject <<
  /Logo <<
    /Type /XObject
    /Subtype /Form
    /BBox [0 0 200 100]      % Bounding box
    /Matrix [1 0 0 1 0 0]    % Transformation matrix
    /Length 45
  >>
>>
1
2
3
4
5
6
7
8
stream
q                            % Save graphics state
1 0 0 RG                     % Red stroke color
2 w                          % 2-point line width
10 10 180 80 re              % Rectangle path
S                            % Stroke the rectangle
Q                            % Restore graphics state
endstream
1
2
3
4
q                            % Save current state
2 0 0 2 100 100 cm          % Scale by 2, translate to (100,100)
/Logo Do                     % Invoke the Logo XObject
Q                            % Restore state
1
2
3
4
5
6
7
8
9
10
11
12
/XObject <<
  /Photo <<
    /Type /XObject
    /Subtype /Image
    /Width 640
    /Height 480
    /BitsPerComponent 8
    /ColorSpace /DeviceRGB
    /Filter /DCTDecode        % JPEG compression
    /Length 45670
  >>
>>
1
2
3
4
q
640 0 0 480 50 300 cm        % Scale and position the image
/Photo Do                     % Render the image
Q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
% Master template containing multiple sub-elements
/XObject <<
  /HeaderTemplate << ... >>
  /FooterTemplate << ... >>
  /MainContent <<
    /Type /XObject
    /Subtype /Form
    % References other XObjects
    stream
    /HeaderTemplate Do
    % ... main content ...
    /FooterTemplate Do
    endstream
  >>
>>
1
2
3
4
5
6
7
% Inefficient: Many small line segments
100 100 m
101 101 l 102 102 l 103 103 l ... (hundreds of points)
 
% Efficient: Smooth curve
100 100 m
110 105 120 110 150 120 c        % Single cubic curve
1
2
3
4
5
6
7
8
9
10
% Inefficient: Repeated color changes
1 0 0 rg  100 100 50 50 re f     % Red rectangle
1 0 0 rg  200 100 50 50 re f     % Same red, redundant
1 0 0 rg  300 100 50 50 re f     % Same red, redundant
 
% Efficient: Set color once
1 0 0 rg                         % Set red once
100 100 50 50 re f               % Rectangle 1
200 100 50 50 re f               % Rectangle 2  
300 100 50 50 re f               % Rectangle 3
1
2
3
4
5
6
7
8
9
q                                % Save initial state
% Apply common transformations/settings
2 w                              % Set line width once
0.5 G                            % Set gray level once
 
% Draw multiple elements with shared properties
100 100 m 200 200 l S            % Line 1
150 150 m 250 250 l S            % Line 2
Q                                % Restore state efficiently
1
2
3
4
5
% Problem: Path defined but not painted
100 100 m 200 200 l              % Path exists but invisible
 
% Solution: Add painting operator
100 100 m 200 200 l S            % Now the line will appear

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.