Home > OS >  How to disable mailto: when clicking an Email like filename?
How to disable mailto: when clicking an Email like filename?

Time:03-09

Consider this simple block of code that reproduces a simple text with a web hyperlink to a file located in the user's file-system.

Document doc = new Document();
Page page1 = doc.Pages.Add();
TextFragment textFragment = new TextFragment();            
TextSegment textSegment = new TextSegment("[email protected]");
textSegment.Hyperlink = new Aspose.Pdf.WebHyperlink("Images/[email protected]");
textFragment.Segments.Add(textSegment);
page1.Paragraphs.Add(textFragment);
doc.Save(dataDir);

The file name is [email protected] and therefor the PDF recognizes it as an Email and automatically adds a mailto: prefix, so instead of opening the file (with some default program) its being opened with Outlook.

This question is followed by my enter image description here

CodePudding user response:

Solved by escaping the text with zero-width-whitespace (definition) like:

internal static string SafeEscapeMailtoPrefix(this string value)
{
    if (string.IsNullOrEmpty(value))
        return value;
    return value.Replace("@", "\u200B@");
}

So the TextSegment will be like:

TextSegment textSegment = newTextSegment("[email protected]".SafeEscapeMailtoPrefix());
  • Related