Add page numbers
Welcome to the Add page numbers to PDF guide. This guide will teach you how add page numbers to PDF files easily.
Remember that all Tool Guides takes into account that you have previous knowledge about the Basics to process a PDF with our REST API but if not, we strongly recommend you to read the related guide before start with this one.
Basic Add page numbers to PDF
You can add page numbers to your PDF files as shown in this code:
By using PagenumberTask as a process tool you will add numbers to your PDF pages with all the default settings. See the full list of settings:
- Page numbering will start from the very first page. Even if it is a cover page
- The starting number will be “1”
- All PDF pages will be numbered.
- Numbers will be placed at bottom center of the page
- The default font used for numbers will be Arial Unicode with a 14p size
- Their color will be #000000, Black.
All this settings and more can be customized with advanced options of addPageNumbers()
Advanced Add page numbers to PDF
Do not number first page
setFirstCover(true) is a useful function for books or magazines and let you avoid numbering the first page of your document. It is recommended to combine this function with setStartNumber() to set from which number you begin your PDF page numbers.
Set starting number
setStartNumber(2) will set from which number will start the page numbering and it is recommended to combine with firstCover() and pages() to set which pages will be numbered.
Pages to number
setPages() has plenty of parameters to set which pages has to be numbered. See them:
- 'all' Means that all pages will be numbered. This is the default value
- '3-end' Means that pages from third to the last will be numbered
- '1,3,4-9' Means that pages 1 and 3 and in range of 4 to 9, will be numbered
- '-1-end' Means that 2 pages less since the last will be numbered
- '3-234' Means that the range of 3 to 234 pages will be numbered
Set numbers font format
setFontFamily(), setFontSize() and setFontColor() are functions to customize page numbers font format. With setFontFamily() can change which font will be used, with setFontSize() change the font size and with setFontColor(), change the color of the numbers.
Format of page number
It is possible to set the page number format and add strings to it concatenated with variables with setText(). In the code example the page number content is: "Page N of P" where N is the number of the actual page and P the number of total pages of the PDF document.
Vertical position of numbers
setVerticalPosition() let you set the vertical position of page numbers to the 'top' or the 'bottom' ends of the page. Note that there will not be any margin, in order to do this, use setVerticalAdjustPosition().
Vertical position adjustment of numbers
setVerticalAdjustPosition() will displace the amount of pixels set in params, from its defined vertical position. This value can be positive or negative so be careful, if you have your number placed to bottom with setVerticalPosition('bottom') and you setVerticalAdjustPosition(-100), you will place your number outside of the page!
Horizontal position of numbers
setHorizontalPosition() let you set the horizontal position of page numbers to the 'left', 'center' or 'right' of the page. Take note that Left and Right positions will be affected by setting facingPages(true) as you will see in its description.
Horizontal position adjustment of numbers
setHorizontalAdjustPosition() will displace the amount of pixels set in params, from its defined horizontal position. This value can be positive or negative so be careful, if you have your number placed to right with setHorizontalPosition('right') and you setHorizontalPositionAdjustment(100), you will place your number outside of the page!
Add number to facing pages
With function setFacingPages() you can set your PDF numbering is in facing mode, like a book or a magazine. This only takes effect combined with setHorizontalPosition('left') or setHorizontalPosition('right') and will place all page numbers mirrored between pages where setHorizontalPosition('left') will place numbers to the interior and setHorizontalPosition('right') to the exterior of the page. Se an example in this picture to better understanding:
Setting setFacingPages(false) will place numbers always at the same position of page.