# Merge PDFs

Combine several PDF files, or pages picked from each of them, into one PDF with the `merge` command-line tool.

**Available in [pre-release 3.7](https://www.sumatrapdfreader.org/prerelease)**

**Run `sumatrapdf-tool merge -o all.pdf a.pdf b.pdf`.** At a glance:

- **Whole files:** `sumatrapdf-tool merge -o all.pdf a.pdf b.pdf c.pdf`
- **Some pages of each file:** `sumatrapdf-tool merge -o out.pdf a.pdf 1-3 b.pdf 5`
- **Insert a file in the middle of another:** `sumatrapdf-tool merge -o out.pdf a.pdf 1-3 b.pdf a.pdf 4-N`
- **Every PDF in a folder:** a PowerShell one-liner, see below.

`sumatrapdf-tool` is installed with SumatraPDF and added to PATH. [SumatraPDF.exe merge](Tools.md) takes the same arguments. There is no merge command in the SumatraPDF window.

## Merge whole files

```
sumatrapdf-tool merge -o all.pdf a.pdf b.pdf c.pdf
```

- Files are added in the order you list them.
- Put file names with spaces in quotes: `"01 intro.pdf"`.
- `-o` is the file to create. Without `-o` the result goes to `out.pdf`.

## Pick pages from each file

Put a page list after a file name to take only those pages. A file without a page list is added whole.

```
sumatrapdf-tool merge -o out.pdf a.pdf 1-3 b.pdf 5 c.pdf
```

This takes pages 1, 2 and 3 of `a.pdf`, page 5 of `b.pdf` and all of `c.pdf`.

The page list is comma-separated, with no spaces:

| Page list | Pages                                                     |
| --------- | --------------------------------------------------------- |
| `5`       | page 5                                                    |
| `1-3,7`   | pages 1, 2, 3 and 7                                       |
| `8-N`     | page 8 to the last page (`N` is the last page)            |
| `-1`      | the last page; `-2` is the second-to-last                 |
| `1--2`    | every page except the last                                |
| `N-1`     | all pages in reverse order                                |

Pages are taken in the order you list them, so the same syntax reorders and repeats pages: see [Reorder pages in a PDF](Reorder-PDF-pages.md). Note: `1-N-1` is not "all but the last page"; it repeats the last page. Use `1--2`.

You can list the same file more than once. To insert all of `b.pdf` after page 3 of `a.pdf`:

```
sumatrapdf-tool merge -o out.pdf a.pdf 1-3 b.pdf a.pdf 4-N
```

## Merge every PDF in a folder

In PowerShell, in the folder with the PDFs:

```
$files = (Get-ChildItem *.pdf | Sort-Object Name).FullName
sumatrapdf-tool merge -o ..\all.pdf $files
```

Files are merged in name order. The result is written to the parent folder so running the command again doesn't merge the previous result too.

## Merge images or other documents

`merge` reads only PDF files. For anything else it prints `Cannot merge document 'photo.png'.` and leaves that file out. Convert it to PDF first, then merge:

```
sumatrapdf-tool convert -O compress -o photo.pdf photo.png
sumatrapdf-tool merge -o out.pdf report.pdf photo.pdf
```

See [Convert images to PDF](Convert-images-to-PDF.md), [Convert EPUB to PDF](Convert-EPUB-to-PDF.md) and [Convert comics to PDF](Convert-comics-to-PDF.md).

## What the merged PDF keeps

- Bookmarks (table of contents) of each file are copied and point to the new page numbers. Bookmarks to pages you didn't take are dropped.
- Note: with a range written backwards (`N-1`, `6-4`) bookmarks keep their old page numbers. To reorder one file, use `clean` ([Reorder pages in a PDF](Reorder-PDF-pages.md)).
- `-O` sets output options, e.g. `-O compress,garbage`. See [sumatrapdf-tool merge](Tool-merge.md) for the list.

## Tips

- Check the messages after merging: if a file is missing or isn't a PDF, `merge` skips it with `Cannot merge document '...'` and still writes the output (and still exits with code 0).
- Name files `01 ...`, `02 ...` so a folder merges in the right order.
- Write the output to a new file, not over one of the inputs.
- Use [Compress a PDF](Tool-x-compress-pdf.md) if the merged file is large.

## See also

- [Split a PDF](Split-a-PDF.md) — the reverse: one PDF into several
- [Reorder pages in a PDF](Reorder-PDF-pages.md) — reverse, move and repeat pages
- [Delete pages from PDF](Tool-x-delete-pages-from-pdf.md)
- [Batch process PDFs](Batch-process-PDFs.md) — run tools over many files
- [sumatrapdf-tool merge](Tool-merge.md) — all options
- [All cmd-line tools](Tools.md)
