Adding a LinkProvider

It makes perfect sense to create a PDF with http links that open an URL in a browser window. It's more delicate to add a relative link to a PDF document. If the document is downloaded and consulted off line, the relative link requires that the document that is referred to is present on your system at the correct location. Looking at thoreau1.pdf, we see that there's a link to the walden.html file that is in the same folder as thoreau.html file. However, the PDF we're creating is written to a different directory. If we want the link to work, we need to change the base URL. This can be done by implementing LinkProvider, an interface with a single method: getLinkRoot(), and adding it to the HtmlPipelineContext using the setLinkProvider() method.

htmlContext.setLinkProvider(new LinkProvider() {
    public String getLinkRoot() {
        return "http://tutorial.itextpdf.com/src/main/resources/html/";
    }
});

In this case, the relative link <a href="walden.html">Walden</a> is changed into an absolute link to http://tutorial.itextpdf.com/src/main/resources/html/walden.html.