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
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 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". -ois the file to create. Without-othe result goes toout.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. 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
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, useclean(Reorder pages in a PDF). -Osets output options, e.g.-O compress,garbage. See sumatrapdf-tool merge for the list.
Tips #
- Check the messages after merging: if a file is missing or isn’t a PDF,
mergeskips it withCannot 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 if the merged file is large.
See also #
- Split a PDF — the reverse: one PDF into several
- Reorder pages in a PDF — reverse, move and repeat pages
- Delete pages from PDF
- Batch process PDFs — run tools over many files
- sumatrapdf-tool merge — all options
- All cmd-line tools