GhostScript is incredibly powerful tool for manipulating many different document formats, among others also PDF.
Yet, its cryptic command format makes it sometimes hard to get something done; but most of the operations can be simply done by template.