I am trying to merge different pdf files (all produced with latex - but with different document classes) without breaking the corresponding hyperlinks.

I have tried the terminal using ghostscript, apple automator, the \includepdf option in latex: they all brake the hyperlinks created with hyperref.

Any good suggestion?

EDIT: as this seem to be a common problem, can anyone try to explain/comment on why such apparently simple task is instead rather daunting? What is it that makes it difficult to merge the pdfs and not break the hyperlinks?

  • Not sure whether it can combine PDFs, but qpdf can at least remove encryption from PDFs without breaking hyperlinks, you might give it a try. – Skillmon likes topanswers.xyz Jun 27 '19 at 6:19
  • 1
    @Skillmon: this is quite on topic. There is a package (pax) which tries to solve this, and with luatex it is probably possible to (but needs some coding). – Ulrike Fischer Jun 27 '19 at 6:35
  • @UlrikeFischer ok, if you think so, I'll retract my vote. – Skillmon likes topanswers.xyz Jun 27 '19 at 6:36
  • May be combine. If classes are quite different (e.g., book and beamer) this is not a reasonable option, but otherwise (eg., book and scrbook) probably you can change the class of some document with minor fixings, and then use the combine to merge documents in a convenient way (optinal index of the documents, general list of figures, etc.) – Fran Jun 27 '19 at 6:55
  • 1
    @daleif I am fairly confident, as the hyperlink address is different from the text appearing in the pdf – Bazinga Jun 27 '19 at 17:48

My method uses the pax package and its pdfannotextractor Perl script (by Heiko Oberdiek).

If you want to combine two PDF files (a.pdf and b.pdf) keeping the PDF annotations (internal and external links are kind of PDF annotations), use the manual or automatic method.

(In case of problems, you can use the --debug option of the pdfannotextractor script.)

Manual installation

  1. Install the pax package from CTAN (or via TeXLive or MikTeX).

  2. Download and unzip the PDFBox-0.7.3 Java library (an old version of the Apache PDFBox, a Java PDF library).

  3. Use pdfannotextractor on a.pdf and on b.pdf:

    CLASSPATH='PDFBox-0.7.3/lib/PDFBox-0.7.3.jar:%CLASSPATH%' pdfannotextractor a.pdf
    CLASSPATH='PDFBox-0.7.3/lib/PDFBox-0.7.3.jar:%CLASSPATH%' pdfannotextractor b.pdf

    (You get two new files: a.pax and b.pax)

  4. Compile the following document with pdflatex:


Automatic installation

The pdfannotextractor script can perform the installation itself:

  1. Install the pax package from CTAN (or via TeXLive or MikTeX).

  2. Install automatically the PDFBox-0.7.3 archive:

    pdfannotextractor --install
  3. Use pdfannotextractor on a.pdf and on b.pdf:

    pdfannotextractor a.pdf
    pdfannotextractor b.pdf

    (You get two new files: a.pax and b.pax)

  4. Compile the following document with pdflatex:

| improve this answer | |
  • Having some difficluty getting this to work: 1. It seems as if the pax package is already part of texlive as I have /usr/local/texlive/2019/texmf-dist/tex/latex/pax/pax.sty (dated 4/19/12) and an update to this does not show in the TeXLiveUtility. 2. I downloaded the `PDFBox-0.7.3' library and moved the directory to the directory where I am testing this. – Peter Grill Feb 29 at 7:28
  • Then, executing bash-3.2$ CLASSPATH='PDFBox-0.7.3/lib/PDFBox-0.7.3.jar%CLASSPATH%' pdfannotextractor test-scratch-a.pdf I get PDFAnnotExtractor 0.1l, 2012/04/18 - Copyright (c) 2008, 2011, 2012 by Heiko Oberdiek., warning: kpathsea: configuration file texmf.cnf not found in these directories: /usr/local/texlive/2019/texmf.cnf. and !!! Error: Cannot find pax.jar'`. – Peter Grill Feb 29 at 7:29
  • @PeterGrill What does the command kpsewhich --progname pdfannotextractor --format texmfscripts pax.jar produce? Here, I get /usr/local/texlive/2019/texmf-dist/scripts/pax/pax.jar. – Paul Gaborit Mar 1 at 6:45
  • I get the same result as you do. – Peter Grill Mar 1 at 7:24
  • @PeterGrill Did you try the automatic installation? Did you try to add the --debug option? – Paul Gaborit Mar 1 at 14:10



  • pdftk if want a command-line interface,
  • pdfsam if you want graphical user interface,
  • sejda if you want a web interface.

They are free (as in beer and speech) and available for most operating systems.
I have no idea why most tools do not preserve hyper links.


For a list of possible solutions -- that unfortunately do not discuss whenever links are preserved or not, cf. https://stackoverflow.com/q/2507766/2657549.

I'm testing with two copies of the following file, that I named a.pdf and b.pdf:

  author={Doe, Jack},
  title={An Example}


\section{A section}

url: \href{https://tex.stackexchange.com/a/531215/34551}{this post}

reference: \cite{ex}

internal link: \ref{section}

file link: \href{run:a.pdf}{file}


I combined them into a tot.pdf file using the following tools, and tested whenever the url, the reference and the internal links were preserved in both documents or not.

|  Software |                                   Command                                  |  url |  ref. |  link | file |
|  convert  | convert a.pdf b.pdf tot.pdf                                                | ✗    | ✗     | ✗     | ✗   |
|  pdfjam   | pdfjam a.pdf b.pdf -o tot.pdf                                              | ✗    | ✗     | ✗     | ✗   |
|  gs       | gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=tot.pdf a.pdf b.pdf | ✗    | ✗     | ✗     | ✗   |
|  pdfunite | pdfunite a.pdf b.pdf tot.pdf                                               | ✓    | ✗     | ✗     | ✓   |
|  pdftk    | pdftk a.pdf b.pdf cat output tot.pdf                                       | ✓    | ✓     | ✓     | ✓   |
|  pdfsam   | (it's a gui)                                                               | ✓    | ✓     | ✓     | ✓   |
| sejda.com | (it's a website)                                                           | ✓    | ✓     | ✓     | ✓   |

(Note that the links to files are not converted to internal anchor if the document that was referenced is being merged. I don't think that this is even feasible without re-writting the LaTeX source code.)


  • convert: ImageMagick 6.9.10-23 Q16 x86_64 20190101
  • pdfjam: This is pdfjam version 3.03.
  • gs: GPL Ghostscript 9.27 (2019-04-04)
  • pdfunite: version 0.71.0
  • pdftk: port to java 3.0.2 a Handy Tool for Manipulating PDF Documents
  • pdfsam: PDF Split And Merge Basic Edition 4.0.1
  • https://www.sejda.com/merge-pdf does not mention any version, but was tested on 2020/03/04.

Licences and Pricing

Other options

Not tested:

| improve this answer | |
  • pdftk seemed to be the one, but unfortuately does not run on the latest MacOS. Have emailed the author. – Peter Grill Mar 5 at 6:09
  • Looks like sejda was able to maintain the run: type links that I need (ie, \href{run:foo.pdf}{My PDF} to open another .pdf file). They have a desktop version so I am trying that. – Peter Grill Mar 5 at 7:04
  • @PeterGrill Have you tried building it from source? Some indications are at pdflabs.com/tools/pdftk-server I've added a test case for your run: type. I'm assuming they will be dealt the same way as href links are. – Clément Mar 5 at 19:01
  • 1
    @PeterGrill Oh, and if you have control over the source file, then you may want to have a look at the documentation of hyperref, the paragraph that starts with "If the final PDF file is going to be merged with another file, …". – Clément Mar 5 at 19:23
  • My links are to external files, so I don't have that specfic issue. I think that would be useful information to include in the answer as I was not aware of that. – Peter Grill Mar 5 at 20:57

There are still a few tools to allow merging pdf files whilst retaining hyperlinks. I retested a few of the more established ones.

Ghostscript (free)

The commands I tested all broke some links (however I may have missed a command that works for you).

PDFSAM / Sejda (freemium) these come in many flavours from the same source.

I had best success using Sejda online 4 files will have internal / external links that work as expected. However note in my limited test the last document say pages 1-5 for arguments sake would now be for example physical 15-19 of the total However they are kept as logical 1-5 so I can jump internally from page3(18) to page2(17) or index 5(19) - contents 1(15) etc I do see the old numbers 1-5 in use which helps.

Pdftk (freemium) also merged well in the free version.
However pages as built similar to above only show the new physical page numbers so it is odd to see a link for page 1 that jumps me to page 15.

In summary are you sure this is the route to go down? since numbering of internal links and page numbers will be all over the place. It makes more sense to include all the files in a main.tex where numbering of pages and links can be controlled.

| improve this answer | |

PDF-XChange Editor seems to do this without any problems. However, it is a "premium feature" and the editor will stamp a notice all over your document when you do it using the free version.

| improve this answer | |
  • Do you know if it can be used via command line or som sort of batch processing? Lauching a UI everytime it is not an option. – Peter Grill Mar 1 at 22:58
  • @PeterGrill Hmm, couldn't find any in the manual, so probably not. One could of course mail and ask the developers to be sure. – schtandard Mar 2 at 8:15
  • Sadly, this only seems to only be available on Windows so I can't test it. – Peter Grill Mar 5 at 7:13

Take a look at pdfjam, most of it's options are just passed along to pdfpages, and only documented there. Lots of tweaks available, also keeping hyperlinks (not done by default, though). It is part of TeXlive.

| improve this answer | |
  • I am unable to locate documentation that says that links can be kept. Do you know where that is mentioned? – Peter Grill Mar 4 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.