Renderizar hiperenlaces con estilo personalizado
Contents
[
Hide
]
Puede haber momentos en los que necesite generar hiperenlaces con un estilo específico basado en los requisitos de su aplicación. Para ello, Aspose.Email proporciona HyperlinkRenderingCallback. Puede pasar el HyperlinkRenderingCallback como un parámetro de MailMessage.GetHtmlBodyText.
El siguiente fragmento de código le muestra cómo usar HyperlinkRenderingCallback para generar hiperenlaces utilizando su propio estilo personalizado.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
public static void main(String[] args) { | |
String dataDir = Utils.getSharedDataDir(CustomHyperlinkRendering.class) + "email/"; | |
String fileName = dataDir + "LinksSample.eml"; | |
MailMessage msg = MailMessage.load(fileName); | |
String htmlTextHref = msg.getHtmlBodyText(new HyperlinkRenderingCallback() { | |
@Override | |
public String invoke(String source) { | |
return RenderHyperlinkWithHref(source); | |
} | |
}); | |
System.out.println(htmlTextHref); | |
String htmlTextHrefLess = msg.getHtmlBodyText(new HyperlinkRenderingCallback() { | |
@Override | |
public String invoke(String source) { | |
return RenderHyperlinkWithoutHref(source); | |
} | |
}); | |
System.out.println(htmlTextHrefLess); | |
} | |
private static String RenderHyperlinkWithHref(String source){ | |
int start = source.indexOf("href=\"") + "href=\"".length(); | |
int end = source.indexOf("\"", start + "href=\"".length()); | |
String href = source.substring(start, end); | |
start = source.indexOf(">") + 1; | |
end = source.indexOf("<", start); | |
String text = source.substring(start, end); | |
String link = text + "<" + href + ">"; | |
return link; | |
} | |
private static String RenderHyperlinkWithoutHref(String source){ | |
int start = source.indexOf(">") + 1; | |
int end = source.indexOf("<", start); | |
String text = source.substring(start, end); | |
return text; | |
} |
Los métodos RenderHyperlinkWithHref y RenderHyperlinkWithoutHref están destinados a demostrar la representación de hiperenlaces y no están diseñados para su uso en producción.