Renderizar hipervínculos con estilo personalizado
Contents
[
Hide
]
Puede haber ocasiones en que necesites generar hipervínculos con un estilo específico según los requisitos de tu aplicación. Para eso, Aspose.Email ofrece HyperlinkRenderingCallback. Puedes pasar HyperlinkRenderingCallback como parámetro de MailMessage.GetHtmlBodyText.
El siguiente fragmento de código muestra cómo usar HyperlinkRenderingCallback para generar hipervínculos con tu propio estilo personalizado.
// 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 = "data/";
MailMessage msg = MailMessage.load(dataDir + "LinksSample.eml");
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);
}
// Renders the link as "text <href>"
private static String renderHyperlinkWithHref(String source) {
int start = source.indexOf("href=\"") + "href=\"".length();
int end = source.indexOf("\"", start);
String href = source.substring(start, end);
start = source.indexOf(">") + 1;
end = source.indexOf("<", start);
String text = source.substring(start, end);
return text + " <" + href + ">";
}
// Renders the link text only, dropping the target
private static String renderHyperlinkWithoutHref(String source) {
int start = source.indexOf(">") + 1;
int end = source.indexOf("<", start);
return source.substring(start, end);
}
Los métodos RenderHyperlinkWithHref y RenderHyperlinkWithoutHref están destinados a demostrar la renderización de hipervínculos y no se recomiendan para uso en producción.