--- /dev/null
+%%% Page size and margins %%%
+
+\newdimen\pagewidth
+\newdimen\pageheight
+\newdimen\leftmargin
+\newdimen\rightmargin
+\newdimen\topmargin
+\newdimen\bottommargin
+
+\def\setuppage{%
+ \hsize=\pagewidth
+ \advance\hsize by -\leftmargin
+ \advance\hsize by -\rightmargin
+ \vsize=\pageheight
+ \advance\vsize by -\topmargin
+ \advance\vsize by -\bottommargin
+ \hoffset=\leftmargin
+ \advance\hoffset by -1truein
+ \voffset=\topmargin
+ \advance\voffset by -1truein
+ \ifx\pdfpagewidth\undefined\else
+ \pdfpagewidth=\pagewidth
+ \pdfpageheight=\pageheight
+ \fi
+}
+
+\def\sethmargins#1{\leftmargin=#1\relax\rightmargin=#1\relax}
+\def\setvmargins#1{\topmargin=#1\relax\bottommargin=#1\relax}
+\def\setmargins#1{\sethmargins{#1}\setvmargins{#1}}
+
+\def\defpaper#1#2#3{\expandafter\def\csname paper-#1\endcsname{\pagewidth=#2\pageheight=#3\setuppage}}
+\def\setpaper#1{%
+ \expandafter\let\expandafter\currentpaper\csname paper-#1\endcsname
+ \ifx\currentpaper\relax
+ \errmessage{Undefined paper format #1}
+ \fi
+ \currentpaper
+}
+
+\def\landscape{%
+ \dimen0=\pageheight
+ \pageheight=\pagewidth
+ \pagewidth=\dimen0
+ \setuppage
+}
+
+\defpaper{a3}{297truemm}{420truemm}
+\defpaper{a4}{210truemm}{297truemm}
+\defpaper{a5}{148truemm}{210truemm}
+\defpaper{a6}{105truemm}{148truemm}
+\defpaper{letter}{8.5truein}{11truein}
+\defpaper{legal}{8.5truein}{14truein}
+
+\setmargins{1truein}
+\setpaper{a4}
+
+\bye