82

I want to find out the easiest way to replace a single page in a multiple page PDF with another single page PDF in Linux. What tool is best for this?

Currently I do it like this:

  1. split pdf to single pages using pdfsam
  2. replace the pages that I want to replace
  3. merge them using pdfsam
  4. use ghostscript to force the page size as same

Any better way?

5 Answers 5

128

You could use the PDF Toolkit PDFtk:

Example:

pdftk A=inA.pdf B=inB.pdf cat A1-12 B3 A14-end output out1.pdf

The output consists of the first 12 pages of inA.pdf, followed by page 3 of inB.pdf and then pages 14 until end of inA.pdf.

Many Linux distributions provide a PDFtk package you can download and install using their package manager.

5
  • 7
    Thanks! Note, A= and B= must be uppercase. When I tried lowercase pdftk though a= was part of the filename.
    – biscuit314
    Oct 9, 2017 at 13:23
  • Didn't work for me: ` pdftk A=./inA.pdf B=./inB.pdf A1-4 B1 A6-end output out.pdf Error: Unable to find file. Error: Failed to open PDF file: A1-4 Error: Unable to find file. Error: Failed to open PDF file: B1 Error: Unable to find file. Error: Failed to open PDF file: A6-end Errors encountered. No output created. Done. Input errors, so no output created. ` - I used qpdf and worked.
    – azbarcea
    May 22, 2020 at 19:18
  • 3
    @azbarcea: the cat is missing. The complete command: pdftk A=./inA.pdf B=./inB.pdf cat A1-4 B1 A6-end output out.pdf. While qpdf certainly has its merits, pdftk allows additional rotation parameters for the cat operation. Try pdftk --help May 22, 2020 at 19:40
  • Note that pdftk is hasn't been updated since 2013
    – Charles L.
    Nov 11, 2021 at 18:57
  • This will not preserve bookmarks. As a workaround, you can use dump_data (check the docs) to dump bookmarks into a text file, and update_info to push those bookmarks to your output pdf file. The pdftk data dump only preserves page numbers, not offsets within the page. So a bookmark links to something at the bottom of the page after going through dump data and update info, the link will go to the top of the page.
    – Charles L.
    Nov 11, 2021 at 19:02
37

You could also use qpdf which is open source:

Same example as the one in the other response:

qpdf --empty --pages inA.pdf 1-12 inB.pdf 3 inA.pdf 14-z -- out1.pdf
2
  • 1
    You probably mean inB.pdf instead of B.pdf. This should probably be the accepted answer as pdftk is discontinued as far as I know. Apr 9, 2019 at 10:40
  • 1
    I don't know if you can change the accepted answer after it is already set. However, I agree this is the better option as it is still very actively maintained. I also really like the fact that it offers an AppImage so you can use it without installing. :D May 13, 2019 at 15:42
3

Another very nice GUI alternative is PDF Arranger.

Just load both PDF files, move the page where you want it, delete the page you don't want, and save.

1

If you just wanted to replace the last page of a two-page pdf with another pdf (one-page), like me, but not trying to insert a page in between pages, it can be done like so:

sudo pdftk A=Doc1.pdf B=Doc2.pdf cat A1 B output DocEdited.pdf

Note:

  • This will replace the last page in Doc1 with the page from Doc2;
  • B is like B1-end; since Doc2 is a PDF of one page so B1 or B is the same;
  • cat A1: Taking the first page from A (or Doc1).
1
  • 7
    There is no need to run pdftk as root. Please edit your answer to drop "sudo" from the command.
    – Ruben
    Dec 7, 2020 at 15:27
0

You could use PDF Chain.

PDF Chain is a graphical user interface for the PDF Toolkit (PDFtk). The GUI supports all common features of the command line tool in a comfortable way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .