-7

I have a multi-page PDF document, and I want a version of the document with one of the pages in the middle with a scanned-in copy, as I needed a physical signature in the document. How can I do this with ImageMagick? I'm aware that ImageMagick may not necessarily be the best tool for the job. However, the resulting PDF does not need to be high quality or a high fidelity copy, so it should be sufficient for my needs.

As a specific example, I have a 9 page my-file.pdf, and I want to create a copy of the PDF with the 8th page replaced with page-8.png. It looks like I should be able to achieve this goal with the convert tool, though it's not immediately obvious what the syntax would be. How can I achieve this goal?

If I merely wanted to append the new page to the end of the file, I know I can do the following:

convert my-file.pdf page-8.png output-file.pdf

However, this end up with the original pages 1-9, then the new page 8. What I actually want is to replace the original page 8 with the new page 8. My desired output is:

[original pages 1 - 7],[new page 8],[original page 9]
M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • [Close discussion](https://meta.stackoverflow.com/questions/402759/why-was-this-question-closed-as-needing-details-or-clarity-when-it-didnt-fit-th?noredirect=1#comment807193_402759) – M. Justin Nov 09 '20 at 18:16

2 Answers2

3

A specific page or range of pages can be specified using the bracket syntax with zero-based indexing. For instance, [8] will refer to the ninth page, and [0-6] to the first seven pages. Using this, a duplicate of the PDF with the 8th page replaced can be achieved as follows:

convert my-file.pdf[0-6] page-8.png my-file.pdf[8] output-file.pdf
M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • 4
    Imagemagick does not preserve vector data. The output will be all rasterized. It will not be all vector pages, except for the one replaced one. All pages in the output will be rasterized. If that does not matter, then this solution is fine. – fmw42 Nov 07 '20 at 03:48
0

As indicated in the question, as well as a comment, Imagemagick is not a great tool for this task, as it rasterizes the output of the PDF. An alternate solution that avoids this would be to use pdftk to replace the page. While the inserted page will still be an image, the unreplaced pages will not be rasterized.

First, save the scanned-in page as a PDF using an application capable of this. Then, use the pdftk cat operation to combine the PDFs:

pdftk A=my-file.pdf B=page-8.pdf cat A1-7 B A9 output output-file.pdf
M. Justin
  • 14,487
  • 7
  • 91
  • 130