Search code examples
referenceitextanchorhrefmailto

How can I linkify an email address with the iTextSharp Anchor class?


I've got this code:

Font LinkFont = FontFactory.GetFont(FontFactory.COURIER, 9, iTextSharp.text.Font.UNDERLINE, BaseColor.BLUE);
Anchor anchor = new Anchor("Adobe Reader", LinkFont);
anchor.Reference = "http://www.adobe.com";

...which works perfectly to link to a website; but to invoke the email client, this:

Anchor anchorEmail = new Anchor("Email the Boss", LinkFont);
anchorEmail.Reference = "mailto://[email protected]";

...does not. Am I missing something, using the wrong class, or what?

UPDATED

This works:

Anchor anchorFinPol = new Anchor("[email protected]", LinkFont);
anchorFinPol.Reference = "mailto://[email protected]";

...but this does not:

Anchor anchorCCO = new Anchor("Danish Danishes", LinkFont);
anchor.Reference = "mailto://[email protected]"; 

Solution

  • The // in a URI is used for protocols with an authority such as http and ftp. For URIs representing email addresses you should omit them:

    var anchor = new Anchor("Example", LinkFont);
    anchor.Reference = "mailto:[email protected]";
    

    Also, sometimes PDF renderers will automatically make things that look like links into actual links automatically. Your example that you said worked when the actual text was the email address was probably such as case. This is convenient for consumers but also dangerous for authors because you can't usually guarantee what renderer everybody will be using.