@@ -36,13 +36,18 @@ This file is part of the iText (R) project.
3636import com .itextpdf .kernel .pdf .annot .PdfAnnotation ;
3737import com .itextpdf .kernel .pdf .annot .PdfLinkAnnotation ;
3838import com .itextpdf .kernel .pdf .tagging .StandardRoles ;
39+ import com .itextpdf .kernel .pdf .tagutils .AccessibilityProperties ;
3940import com .itextpdf .layout .tagging .IAccessibleElement ;
4041import com .itextpdf .layout .IPropertyContainer ;
4142import com .itextpdf .layout .element .ILeafElement ;
4243import com .itextpdf .layout .properties .Property ;
4344import com .itextpdf .html2pdf .html .AttributeConstants ;
4445import com .itextpdf .styledxmlparser .node .IElementNode ;
4546import java .util .List ;
47+
48+ import com .itextpdf .styledxmlparser .node .INode ;
49+ import com .itextpdf .styledxmlparser .node .impl .jsoup .node .JsoupElementNode ;
50+ import com .itextpdf .styledxmlparser .node .impl .jsoup .node .JsoupNode ;
4651import org .slf4j .Logger ;
4752import org .slf4j .LoggerFactory ;
4853
@@ -80,7 +85,34 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url)
8085 * @param url the destination.
8186 * @param context the processor context.
8287 */
88+ @ Deprecated
8389 public static void applyLinkAnnotation (IPropertyContainer container , String url , ProcessorContext context ) {
90+ applyLinkAnnotation (container , url , context , "" );
91+ }
92+
93+ private static String retrieveAlternativeDescription (IElementNode element ) {
94+ List <INode > children = element .childNodes ();
95+ //if there is an img tag under the link then prefer the alt attribute as a link description
96+ if (children .size () == 1
97+ && children .get (0 ).childNodes ().isEmpty ()
98+ && children .get (0 ) instanceof JsoupElementNode
99+ && ((JsoupElementNode )children .get (0 )).getAttribute (AttributeConstants .ALT ) != null ) {
100+ return ((JsoupElementNode )children .get (0 )).getAttribute (AttributeConstants .ALT );
101+ }
102+ //return title attribute value in case of regular link
103+ return element .getAttribute (AttributeConstants .TITLE );
104+ }
105+
106+ /**
107+ * Applies a link annotation.
108+ *
109+ * @param container the containing object.
110+ * @param url the destination.
111+ * @param context the processor context.
112+ * @param alternateDescription description for a link.
113+ */
114+ @ Deprecated
115+ public static void applyLinkAnnotation (IPropertyContainer container , String url , ProcessorContext context , String alternateDescription ) {
84116 if (container != null ) {
85117 PdfLinkAnnotation linkAnnotation ;
86118 if (url .startsWith ("#" )) {
@@ -94,6 +126,9 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url,
94126 } else {
95127 linkAnnotation = (PdfLinkAnnotation ) new PdfLinkAnnotation (new Rectangle (0 , 0 , 0 , 0 )).setAction (PdfAction .createURI (url )).setFlags (PdfAnnotation .PRINT );
96128 }
129+ if (container instanceof IAccessibleElement && alternateDescription != null ) {
130+ ((IAccessibleElement ) container ).getAccessibilityProperties ().setAlternateDescription (alternateDescription );
131+ }
97132 linkAnnotation .setBorder (new PdfArray (new float []{0 , 0 , 0 }));
98133 container .setProperty (Property .LINK_ANNOTATION , linkAnnotation );
99134 if (container instanceof ILeafElement && container instanceof IAccessibleElement ) {
@@ -102,6 +137,18 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url,
102137 }
103138 }
104139
140+ /**
141+ * Applies a link annotation.
142+ *
143+ * @param container the containing object.
144+ * @param url the destination.
145+ * @param context the processor context.
146+ * @param element the element node.
147+ */
148+ public static void applyLinkAnnotation (IPropertyContainer container , String url , ProcessorContext context , IElementNode element ) {
149+ LinkHelper .applyLinkAnnotation (container , url , context , retrieveAlternativeDescription (element ));
150+ }
151+
105152 /**
106153 * Creates a destination
107154 *
0 commit comments