@@ -36,13 +36,18 @@ This file is part of the iText (R) project.
36
36
import com .itextpdf .kernel .pdf .annot .PdfAnnotation ;
37
37
import com .itextpdf .kernel .pdf .annot .PdfLinkAnnotation ;
38
38
import com .itextpdf .kernel .pdf .tagging .StandardRoles ;
39
+ import com .itextpdf .kernel .pdf .tagutils .AccessibilityProperties ;
39
40
import com .itextpdf .layout .tagging .IAccessibleElement ;
40
41
import com .itextpdf .layout .IPropertyContainer ;
41
42
import com .itextpdf .layout .element .ILeafElement ;
42
43
import com .itextpdf .layout .properties .Property ;
43
44
import com .itextpdf .html2pdf .html .AttributeConstants ;
44
45
import com .itextpdf .styledxmlparser .node .IElementNode ;
45
46
import 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 ;
46
51
import org .slf4j .Logger ;
47
52
import org .slf4j .LoggerFactory ;
48
53
@@ -80,7 +85,34 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url)
80
85
* @param url the destination.
81
86
* @param context the processor context.
82
87
*/
88
+ @ Deprecated
83
89
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 ) {
84
116
if (container != null ) {
85
117
PdfLinkAnnotation linkAnnotation ;
86
118
if (url .startsWith ("#" )) {
@@ -94,6 +126,9 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url,
94
126
} else {
95
127
linkAnnotation = (PdfLinkAnnotation ) new PdfLinkAnnotation (new Rectangle (0 , 0 , 0 , 0 )).setAction (PdfAction .createURI (url )).setFlags (PdfAnnotation .PRINT );
96
128
}
129
+ if (container instanceof IAccessibleElement && alternateDescription != null ) {
130
+ ((IAccessibleElement ) container ).getAccessibilityProperties ().setAlternateDescription (alternateDescription );
131
+ }
97
132
linkAnnotation .setBorder (new PdfArray (new float []{0 , 0 , 0 }));
98
133
container .setProperty (Property .LINK_ANNOTATION , linkAnnotation );
99
134
if (container instanceof ILeafElement && container instanceof IAccessibleElement ) {
@@ -102,6 +137,18 @@ public static void applyLinkAnnotation(IPropertyContainer container, String url,
102
137
}
103
138
}
104
139
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
+
105
152
/**
106
153
* Creates a destination
107
154
*
0 commit comments