Add page numbers, headers and footers to PDF in Linux

 Posted by:   Posted on:   Updated on:  2018-02-01T14:59:43Z

How you can add dynamic header and/or footer to a PDF document using Linux and free software. Works for page numbers, chapters and section markers. Or anything else you want, no matter if it's identical on all pages or different on each page.

If you need to add page numbers over each page of a PDF document in Linux keep reading. Actually using this method you will be able to add any kind of content that changes sequentially or by page to a PDF document. By content I mean header, footer and/or watermark. It can be text and/or graphics. I will use cross-platform and free software so you may want to try this on Windows too. I will be using only GUI software: ScribusPDF Chain (the GUI to pdftk) and GIMP. You can install these on Ubuntu with:
sudo apt-get install scribus pdftk pdfchain gimp
First of all you need to gather some information about the document you want to add content to. Using your favorite PDF reader or any other PDF information software find out the number of pages and their size. In order to know where to place headers/footers pick a content representative page from the PDF document that you will later use as a template to avoid overlaying header/footer with document contents.

Add page numbers, headers and footers to PDF in Linux

PDF Information - Evince screenshot
Document information needed here
GIMP is what I will use for that and it is as simple as opening the PDF document with it. When prompted to choose a page you must choose one that represents the contents of the document the best. Resolution can be a small because we are not interested in details. Then export the image to PNG format. The steps are:
  1. Open GIMP. Go to File - Open. Choose the PDF document.
  2. Choose the page that best represents the contents layout and click Import.
  3. Go to File and choose Export As. Change file extension from pdf to png and click Export (twice).
  4. Close GIMP. Choose not to save if asked so.
GIMP import PDF - screenshot
Import PDF document page in GIMP
Now we have enough information to launch Scribus. Here we will create a document with the same page number and size as the source PDF. It may also be a good idea to set margins to 0 so you won't be bothered by those guides.

Scribus New document - screenshot
Create new document in Scribus
Now go to Edit menu and choose Master Pages. A small window appears and whenever it shows, whatever you add to the page displayed will be added to all document pages that the master page applies to. For the rest of this tutorial I will use a single master page that will apply to all pages of the resulting document. However if you want different left/right layout you must use two master pages. How to do that is described in Scribus tutorials. Here is one: Adding Master Pages to Scribus.

I will start by adding a image to the master page, yes that very image I extracted from PDF. Make sure the Edit Master Pages window is visible and the right page is selected (Normal if there is only one). These are the steps:
  1. Insert an image frame from the Insert menu or main toolbar.
  2. Double click on it and choose that image exported from GIMP.
  3. Bring up the Properties window from Windows menu.
  4. Make sure the image frame is selected.
  5. Set X-Pos and Y-Pos to 0.
  6. Set Width to page width and Height to page height (yes, the same dimensions as the source PDF document and the Scribus document). After you did this, the image frame should extend to the entire page and the contained image should fit perfectly. If not, right-click the image frame and choose Adjust Image to Frame.
  7. It's a good idea to lock the image frame too.
Image frame properties - Scribus screenshot
Image frame properties
Now, it's the time to add whatever you wanted to add. Header and/or footer, page number (in a text frame - Insert - Character - Page Number). Align them in order not to overlap the contents of the image. Use the Properties dialog to tweak what you add. When you're done, delete the image frame with the page image (after you unlock it). This is how my header and footer looks like:

Master page
Content master page
All you have to do is close the Edit Master Pages window. You should now be able to browse through your header/footer only document in Scribus. If it is OK, go to File - Export and choose Save as PDF. Do not make any change in the Pre press tab of the export dialog because you risk modifying the output document page size. Close Scribus.

You should have two PDF documents: the initial document you want header/footer added to and the Scribus generated PDF that contains only the header/footer. Both should have the same page number and size. We'll stamp the first with the second using PDF Chain. Make sure the input document is not encrypted! It is very important that Use multiple pages is checked.

PDF Chain stamp screenshot
Stamp with PDF Chain
That is all. Here is a page from my document.

Document page with header/footer
Document with page number and header
The overall size of the PDF increased in my case by about 0.5 MB. 

5 comments :

  1. Thank you! I've searched all morning on this subject. Did it success on Debian 10 Kde.

    ReplyDelete
  2. Can it right off crop the pages on through similar way?

    ReplyDelete
  3. Thank you for sharing your knowledge. Works perfectly on Ubuntu 20.04.

    ReplyDelete
  4. Thanks for sharing!

    ReplyDelete

Please read the comments policy before publishing your comment.