1. Device Control

Device control in PostScript allows you to interact with output devices, manage page layout, and control rendering behavior. Understanding device control is essential for professional document production.

1.1. Overview

Device control capabilities include:

  • Page management - Page setup and output

  • Device parameters - Resolution, media, etc.

  • Output control - Copying, erasure, and rendering

  • Device-specific features - Printer capabilities

  • Coordinate systems - Device vs. user space

  • Resource management - Fonts, patterns, etc.

These features enable precise control over document output across different devices.

1.2. Page Management

1.2.1. The showpage Operator

Transmits current page to output device:

% Draw content
100 100 moveto
(Hello, World!) show

% Output page
showpage
% Page is cleared, ready for next page

1.2.2. The copypage Operator

Outputs page without clearing:

% Draw content
100 100 moveto
(Page content) show

% Output without clearing
copypage

% Add more content
100 80 moveto
(More content) show

% Final output
showpage

1.2.3. The erasepage Operator

Clears current page without outputting:

% Draw something
100 100 moveto
(Oops, mistake!) show

% Clear it
erasepage

% Start fresh
100 100 moveto
(Correct content) show
showpage

1.2.4. Multi-Page Documents

% Page 1
/Times-Roman findfont 12 scalefont setfont
100 700 moveto
(Page 1) show
showpage

% Page 2
100 700 moveto
(Page 2) show
showpage

% Page 3
100 700 moveto
(Page 3) show
showpage

1.3. Device Parameters

1.3.1. Page Size and Orientation

% US Letter (8.5" x 11")
<< /PageSize [612 792] >> setpagedevice

% A4 (210mm x 297mm)
<< /PageSize [595 842] >> setpagedevice

% Landscape (swap dimensions)
<< /PageSize [792 612] >> setpagedevice

% Legal (8.5" x 14")
<< /PageSize [612 1008] >> setpagedevice

1.3.2. Resolution Control

% Set resolution (if supported)
<< /HWResolution [300 300] >> setpagedevice  % 300 DPI

% High resolution
<< /HWResolution [600 600] >> setpagedevice  % 600 DPI

% Query current resolution
currentpagedevice /HWResolution get
% Returns: [xRes yRes]

1.3.3. Media Selection

% Select media type
<< /MediaType (Plain) >> setpagedevice
<< /MediaType (Transparency) >> setpagedevice
<< /MediaType (Glossy) >> setpagedevice

% Media color
<< /MediaColor (white) >> setpagedevice
<< /MediaColor (blue) >> setpagedevice

% Media weight
<< /MediaWeight 90 >> setpagedevice  % 90 g/m²

1.3.4. Duplex Printing

% Simplex (single-sided)
<< /Duplex false >> setpagedevice

% Duplex (double-sided)
<< /Duplex true >> setpagedevice

% Tumble (short-edge binding)
<< /Duplex true /Tumble true >> setpagedevice

% Long-edge binding
<< /Duplex true /Tumble false >> setpagedevice

1.4. Device Capabilities

1.4.1. Querying Device Parameters

% Get current page device
currentpagedevice

% Query specific parameter
currentpagedevice /PageSize get =

% Check if parameter exists
currentpagedevice /Duplex known {
  (Duplex supported) print
} {
  (Duplex not supported) print
} ifelse

1.4.2. Device Dictionary

% Complete page device dictionary
<<
  /PageSize [612 792]
  /HWResolution [600 600]
  /Duplex true
  /Tumble false
  /MediaType (Plain)
  /MediaColor (white)
  /MediaWeight 90
  /NumCopies 1
  /Collate true
>> setpagedevice

1.4.3. Printer-Specific Features

% Tray selection
<< /MediaPosition 0 >> setpagedevice  % Auto
<< /MediaPosition 1 >> setpagedevice  % Tray 1
<< /MediaPosition 2 >> setpagedevice  % Tray 2

% Output bin
<< /OutputType (Stacker) >> setpagedevice

% Finishing options
<<
  /Staple 1        % Staple top-left
  /Punch true      % Hole punch
  /Fold 1          % Fold in half
>> setpagedevice

1.5. Output Control

1.5.1. Number of Copies

% Print 5 copies
<< /NumCopies 5 >> setpagedevice

% With collation
<<
  /NumCopies 5
  /Collate true
>> setpagedevice

% Draw page
100 100 moveto
(This will print 5 times) show
showpage

1.5.2. Manual Feed

% Request manual feed
<< /ManualFeed true >> setpagedevice

% Draw page
100 100 moveto
(Insert paper manually) show
showpage

% Restore automatic feed
<< /ManualFeed false >> setpagedevice

1.5.3. Job Control

% Start of job
<<
  /BeginPage {
    % Execute before each page
    /Times-Roman findfont 8 scalefont setfont
    550 20 moveto
    (Page ) show
    PageCount =string cvs show
  }
  /EndPage {
    % Execute after each page
    pop  % Discard reason
    0    % Return 0 to output page
  }
>> setpagedevice

1.6. Coordinate System Management

1.6.1. Default vs. Device Matrix

% Get default matrix (initial CTM)
matrix defaultmatrix
% Returns standard page matrix

% Get current matrix
matrix currentmatrix

% Reset to default
initmatrix

1.6.2. Device Space Transformations

% Transform point to device space
100 200  % User space coordinates
matrix currentmatrix
dtransform
% Returns device space coordinates

% Inverse transform
500 600  % Device space coordinates
matrix currentmatrix
idtransform
% Returns user space coordinates

1.6.3. Resolution-Independent Drawing

% Always work in points (1/72 inch)
% Let device handle resolution

% Good: resolution-independent
/inch { 72 mul } def
1 inch 2 inch moveto
(Text at 1", 2") show

% Bad: device-specific
300 600 moveto  % Assumes 300 DPI
(Position varies by device) show

1.7. Device-Independent Output

1.7.1. Standard Page Sizes

/PageSizes <<
  /Letter [612 792]     % 8.5 x 11 in
  /Legal [612 1008]     % 8.5 x 14 in
  /Ledger [1224 792]    % 17 x 11 in
  /Tabloid [792 1224]   % 11 x 17 in
  /A0 [2384 3370]       % 841 x 1189 mm
  /A1 [1684 2384]       % 594 x 841 mm
  /A2 [1191 1684]       % 420 x 594 mm
  /A3 [842 1191]        % 297 x 420 mm
  /A4 [595 842]         % 210 x 297 mm
  /A5 [420 595]         % 148 x 210 mm
  /B4 [729 1032]        % 257 x 364 mm
  /B5 [516 729]         % 182 x 257 mm
>> def

/setPageSize {  % /sizeName -> -
  PageSizes exch get
  dup 0 get exch 1 get
  << /PageSize [4 -1 roll 3 -1 roll] >> setpagedevice
} def

% Usage
/A4 setPageSize

1.7.2. Margins and Safe Area

/PageMargins {
  % Define standard margins
  <<
    /ImagingBBox [36 36 576 756]  % 0.5" margins on Letter
    % [llx lly urx ury] in points
  >> setpagedevice
} def

% Calculate safe area
/SafeArea {  % -> llx lly urx ury
  currentpagedevice /PageSize get
  aload pop                    % width height
  4 dict begin
    /h exch def
    /w exch def
    /margin 72 def  % 1 inch

    margin                      % llx
    margin                      % lly
    w margin sub               % urx
    h margin sub               % ury
  end
} def

% Draw in safe area
SafeArea
4 dict begin
  /ury exch def /urx exch def
  /lly exch def /llx exch def

  % Draw content within safe area
  llx lly moveto
  (Safe content) show
end

1.8. Rendering Control

1.8.1. Screen Frequency

% Set halftone screen (printing)
<<
  /ScreenFrequency 85
  /ScreenAngle 45
>> setpagedevice

% High-quality screen
<< /ScreenFrequency 150 >> setpagedevice

1.8.2. Transfer Functions

% Adjust tone reproduction
<<
  /TransferFunction { 0.5 mul }  % Lighter
>> setpagedevice

% Gamma correction
<<
  /TransferFunction { 0.45 exp }
>> setpagedevice

1.8.3. Overprint Control

% Enable overprint
<< /Overprint true >> setpagedevice

% Disable overprint
<< /Overprint false >> setpagedevice

1.9. Practical Device Control

1.9.1. Page Setup Utility

/setupPage {  % size orientation -> -
  2 dict begin
    /orient exch def  % 0=portrait, 1=landscape
    /size exch def

    % Get page dimensions
    PageSizes size get

    % Apply orientation
    orient 1 eq {
      aload pop exch  % Swap width/height
    } {
      aload pop
    } ifelse

    % Set page size
    << /PageSize [4 -1 roll 3 -1 roll] >> setpagedevice
  end
} def

% Usage
/Letter 0 setupPage  % Portrait Letter
/A4 1 setupPage      % Landscape A4

1.9.2. Multi-Page Template

/PageTemplate {
  10 dict begin
    /pageNum 1 def
    /totalPages exch def

    /header {
      gsave
        /Helvetica findfont 10 scalefont setfont
        72 720 moveto
        (Document Title) show
      grestore
    } def

    /footer {
      gsave
        /Helvetica findfont 9 scalefont setfont
        540 36 moveto
        (Page ) show
        pageNum =string cvs show
        ( of ) show
        totalPages =string cvs show
      grestore
    } def

    /nextPage {
      header exec
      % Page content here
      footer exec
      showpage
      /pageNum pageNum 1 add def
    } def

    currentdict
  end
} def

% Usage
10 PageTemplate /doc exch def
doc /nextPage get exec  % Page 1
doc /nextPage get exec  % Page 2
doc /nextPage get exec  % Page 3
/PrintJob {
  20 dict begin
    /title exch def
    /copies 1 def
    /duplex false def
    /collate true def

    /setup {
      <<
        /NumCopies copies
        /Duplex duplex
        /Collate collate
      >> setpagedevice
    } def

    /setCopies { /copies exch def } def
    /setDuplex { /duplex exch def } def
    /setCollate { /collate exch def } def

    /print {
      setup
      % Print pages
    } def

    currentdict
  end
} def

% Usage
(My Document) PrintJob /job exch def
job /setCopies 5 exec
job /setDuplex true exec
job /print exec

1.10. Device Testing

1.10.1. Capability Detection

/testDeviceCapability {  % key -> boolean
  currentpagedevice exch known
} def

% Test capabilities
/Duplex testDeviceCapability {
  (Duplex printing available) print
} {
  (Duplex not supported) print
} ifelse

/Color testDeviceCapability {
  (Color device) print
} {
  (Monochrome device) print
} ifelse

1.10.2. Resolution Test Page

/resolutionTest {
  % Display device information
  /Courier findfont 10 scalefont setfont

  72 700 moveto
  (Device Information) show

  72 680 moveto
  (Resolution: ) show
  currentpagedevice /HWResolution get
  0 get =string cvs show
  ( DPI) show

  72 660 moveto
  (Page Size: ) show
  currentpagedevice /PageSize get
  aload pop
  exch =string cvs show
  ( x ) show
  =string cvs show
  ( points) show

  % Resolution test pattern
  100 500 moveto
  0 1 99 {
    /i exch def
    0 2 rlineto
    0 -2 rmoveto
    1 0 rmoveto
  } for
  0.1 setlinewidth
  stroke
} def

resolutionTest
showpage

1.10.3. Color Test Page

/colorTest {
  % Primary colors
  50 dict begin
    /x 50 def
    /y 650 def
    /size 100 def

    % Red
    1 0 0 setrgbcolor
    x y size size rectfill

    % Green
    0 1 0 setrgbcolor
    x size add 20 add y size size rectfill

    % Blue
    0 0 1 setrgbcolor
    x size 2 mul add 40 add y size size rectfill

    % Grayscale ramp
    /y y size sub 50 sub def
    0 0.1 1 {
      /gray exch def
      gray setgray
      x gray 500 mul add y 10 50 rectfill
    } for
  end
} def

colorTest
showpage

1.11. Error Handling for Devices

1.11.1. Safe Device Settings

/safeSetPageDevice {  % dict -> boolean
  {
    setpagedevice
    true
  } stopped {
    pop  % Discard error
    (Warning: Could not set page device) print
    false
  } ifelse
} def

% Usage
<< /PageSize [595 842] >> safeSetPageDevice {
  (Page size set successfully) print
} {
  (Using default page size) print
} ifelse

1.11.2. Fallback Strategy

/setPageSizeSafe {  % size -> -
  dup PageSizes exch known {
    PageSizes exch get
    << /PageSize 3 -1 roll >> setpagedevice
  } {
    pop
    (Unknown page size, using default) print
  } ifelse
} def

% Usage
/A4 setPageSizeSafe        % Sets A4
/CustomSize setPageSizeSafe  % Uses default

1.12. Performance Optimization

1.12.1. Batch Page Setup

% Good: set once for all pages
<<
  /PageSize [612 792]
  /NumCopies 1
  /Duplex false
>> setpagedevice

% Draw all pages with same settings
1 1 10 {
  pop
  % Page content
  showpage
} for

% Bad: repeated setup
1 1 10 {
  pop
  << /PageSize [612 792] >> setpagedevice
  % Page content
  showpage
} for

1.12.2. Minimize Device Changes

% Good: group by settings
<< /Duplex false >> setpagedevice
% Pages 1-5 simplex
1 1 5 { pop showpage } for

<< /Duplex true >> setpagedevice
% Pages 6-10 duplex
6 1 10 { pop showpage } for

% Bad: alternating
1 1 10 {
  dup 2 mod 0 eq {
    << /Duplex true >> setpagedevice
  } {
    << /Duplex false >> setpagedevice
  } ifelse
  showpage
} for

1.13. Best Practices

1.13.1. Device-Independent Code

% Good: works on any device
/inch { 72 mul } def
1 inch 2 inch moveto
(Text) show

% Bad: device-specific
300 600 moveto  % Assumes specific resolution
(Text) show

1.13.2. Query Before Setting

% Good: check capability first
currentpagedevice /Duplex known {
  << /Duplex true >> setpagedevice
} {
  (Duplex not available) print
} ifelse

% Bad: assume capability
<< /Duplex true >> setpagedevice  % May error

1.13.3. Preserve Device State

% Good: save and restore
/savedDevice currentpagedevice def
<< /NumCopies 5 >> setpagedevice
% ... print job
savedDevice setpagedevice

% Bad: don't restore
<< /NumCopies 5 >> setpagedevice
% Settings persist!

1.14. Common Pitfalls

1.14.1. Forgetting showpage

% Wrong: page not output
100 100 moveto
(Text) show
% No showpage - page stays in memory!

% Correct
100 100 moveto
(Text) show
showpage

1.14.2. Unsupported Parameters

% Wrong: may cause error
<< /UnsupportedFeature true >> setpagedevice

% Correct: check first
currentpagedevice /UnsupportedFeature known {
  << /UnsupportedFeature true >> setpagedevice
} {
  (Feature not supported) print
} ifelse

1.14.3. Mixing User and Device Space

% Wrong: confused coordinate systems
matrix currentmatrix dtransform moveto  % Wrong!

% Correct: stay in user space
100 200 moveto  % User space coordinates

1.15. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.