کد قبلی فقط یرای دو ستون کار میکرد. در کد زیر مهم نیست که تعداد ستونها چند تا باشند چون ضریب \columnwidth و \columnsep همیشه تعداد ستونها منهای یک است:
کد: [انتخاب]
\documentclass[a4paper]{book}
\usepackage{ptext,graphicx}
\usepackage{picture}
\usepackage{multicol}
\usepackage{bidi-atbegshi}
\usepackage{xepersian}
\settextfont{XB Niloofar}
\setlength{\pdfpagewidth}{\paperwidth}
\setlength{\pdfpageheight}{\paperheight}
\makeatletter
\AtBeginShipout{%
\ifnum\col@number=\@ne
\AtBeginShipoutUpperLeft{%
\setlength{\unitlength}{1mm}
\put(-1,-5){\line(-1,0){10}}
\put(-1,-5){\line(0,-1){10}}
}
\AtBeginShipoutUpperRight{%
\setlength{\unitlength}{1mm}
\put(1,-5){\line(1,0){10}}
\put(1,-5){\line(0,-1){10}}
}
\AtBeginShipoutLowerLeft{%
\setlength{\unitlength}{1mm}
\put(-1,5){\line(-1,0){10}}
\put(-1,5){\line(0,1){10}}
}
\AtBeginShipoutLowerRight{%
\setlength{\unitlength}{1mm}
\put(1,5){\line(1,0){10}}
\put(1,5){\line(0,1){10}}
}
\else
\AtBeginShipoutUpperLeft{%
\setlength{\unitlength}{1mm}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep+1mm\relax,-5){\line(-1,0){10}}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep+1mm\relax,-5){\line(0,-1){10}}
}
\AtBeginShipoutUpperRight{%
\setlength{\unitlength}{1mm}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep-1mm\relax,-5){\line(1,0){10}}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep-1mm\relax,-5){\line(0,-1){10}}
}
\AtBeginShipoutLowerLeft{%
\setlength{\unitlength}{1mm}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep+1mm\relax,5){\line(-1,0){10}}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep+1mm\relax,5){\line(0,1){10}}
}
\AtBeginShipoutLowerRight{%
\setlength{\unitlength}{1mm}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep-1mm\relax,5){\line(1,0){10}}
\put(-\dimexpr\numexpr\col@number-1\relax\columnwidth+\numexpr\col@number-1\relax\columnsep-1mm\relax,5){\line(0,1){10}}
}
\fi
}
\makeatother
\begin{document}
\chapter{مقداری متن}
\ptext[1-100]
\begin{multicols*}{4}
\ptext[1-100]
\end{multicols*}
\ptext[1-100]
\end{document}