@@ -23,13 +23,17 @@ This file is part of the iText (R) project.
23
23
package com .itextpdf .svg .converter ;
24
24
25
25
import com .itextpdf .commons .utils .FileUtil ;
26
+ import com .itextpdf .commons .utils .IOThrowingAction ;
27
+ import com .itextpdf .kernel .exceptions .PdfException ;
26
28
import com .itextpdf .kernel .geom .PageSize ;
27
29
import com .itextpdf .kernel .geom .Rectangle ;
28
30
import com .itextpdf .kernel .pdf .PdfDocument ;
29
31
import com .itextpdf .kernel .pdf .PdfPage ;
30
32
import com .itextpdf .kernel .pdf .PdfWriter ;
31
33
import com .itextpdf .kernel .pdf .WriterProperties ;
32
34
import com .itextpdf .kernel .pdf .canvas .PdfCanvas ;
35
+ import com .itextpdf .kernel .pdf .tagging .StandardRoles ;
36
+ import com .itextpdf .kernel .pdf .tagutils .TagTreePointer ;
33
37
import com .itextpdf .kernel .pdf .xobject .PdfFormXObject ;
34
38
import com .itextpdf .layout .element .Image ;
35
39
import com .itextpdf .styledxmlparser .IXmlParser ;
@@ -48,13 +52,13 @@ This file is part of the iText (R) project.
48
52
import com .itextpdf .svg .renderers .SvgDrawContext ;
49
53
import com .itextpdf .svg .renderers .impl .PdfRootSvgNodeRenderer ;
50
54
import com .itextpdf .svg .utils .SvgCssUtils ;
55
+ import org .slf4j .Logger ;
56
+ import org .slf4j .LoggerFactory ;
51
57
52
58
import java .io .File ;
53
59
import java .io .IOException ;
54
60
import java .io .InputStream ;
55
61
import java .io .OutputStream ;
56
- import org .slf4j .Logger ;
57
- import org .slf4j .LoggerFactory ;
58
62
59
63
/**
60
64
* This is the main container class for static methods that do high-level
@@ -64,11 +68,12 @@ This file is part of the iText (R) project.
64
68
*/
65
69
public final class SvgConverter {
66
70
67
- private SvgConverter () {
68
- }
69
-
71
+ public static final String SVG_DEFAULT_ROLE = StandardRoles .FIGURE ;
70
72
private static final Logger LOGGER = LoggerFactory .getLogger (SvgConverter .class );
71
73
74
+ private SvgConverter () {
75
+ //empty constructor
76
+ }
72
77
73
78
private static void checkNull (Object o ) {
74
79
if (o == null ) {
@@ -212,7 +217,13 @@ public static void drawOnPage(String content, PdfPage page) {
212
217
*/
213
218
public static void drawOnPage (String content , PdfPage page , float x , float y ) {
214
219
checkNull (page );
215
- drawOnCanvas (content , new PdfCanvas (page ), x , y );
220
+ PdfCanvas canvas = new PdfCanvas (page );
221
+ try {
222
+ withTaggingIfNeeded (page .getDocument (), canvas , page , null , () -> drawOnCanvas (content , canvas , x , y ));
223
+ } catch (IOException e ) {
224
+ // This can't happen in normal circumstances.
225
+ throw new PdfException (e );
226
+ }
216
227
}
217
228
218
229
@@ -238,7 +249,14 @@ public static void drawOnPage(String content, PdfPage page, ISvgConverterPropert
238
249
*/
239
250
public static void drawOnPage (String content , PdfPage page , float x , float y , ISvgConverterProperties props ) {
240
251
checkNull (page );
241
- drawOnCanvas (content , new PdfCanvas (page ), x , y , props );
252
+ final PdfCanvas canvas = new PdfCanvas (page );
253
+ try {
254
+ withTaggingIfNeeded (page .getDocument (), canvas , page , props ,
255
+ () -> drawOnCanvas (content , canvas , x , y , props ));
256
+ } catch (IOException e ) {
257
+ // This can't happen in normal circumstances.
258
+ throw new PdfException (e );
259
+ }
242
260
}
243
261
244
262
/**
@@ -263,7 +281,8 @@ public static void drawOnPage(InputStream stream, PdfPage page) throws IOExcepti
263
281
*/
264
282
public static void drawOnPage (InputStream stream , PdfPage page , float x , float y ) throws IOException {
265
283
checkNull (page );
266
- drawOnCanvas (stream , new PdfCanvas (page ), x , y );
284
+ PdfCanvas canvas = new PdfCanvas (page );
285
+ withTaggingIfNeeded (page .getDocument (), canvas , page , null , () -> drawOnCanvas (stream , canvas , x , y ));
267
286
}
268
287
269
288
/**
@@ -288,12 +307,14 @@ public static void drawOnPage(InputStream stream, PdfPage page, ISvgConverterPro
288
307
* @param props a container for extra properties that customize the behavior
289
308
* @throws IOException when the Stream cannot be read correctly
290
309
*/
291
- public static void drawOnPage (InputStream stream , PdfPage page , float x , float y , ISvgConverterProperties props ) throws IOException {
310
+ public static void drawOnPage (InputStream stream , PdfPage page , float x , float y , ISvgConverterProperties props )
311
+ throws IOException {
292
312
checkNull (page );
293
313
if (props instanceof SvgConverterProperties && ((SvgConverterProperties ) props ).getCustomViewport () == null ) {
294
314
((SvgConverterProperties ) props ).setCustomViewport (page .getMediaBox ());
295
315
}
296
- drawOnCanvas (stream , new PdfCanvas (page ), x , y , props );
316
+ PdfCanvas canvas = new PdfCanvas (page );
317
+ withTaggingIfNeeded (page .getDocument (), canvas , page , props , () -> drawOnCanvas (stream , canvas , x , y , props ));
297
318
}
298
319
299
320
/**
@@ -723,8 +744,18 @@ public static Image convertToImage(InputStream stream, PdfDocument document) thr
723
744
* @return a {@link Image Image} containing the PDF instructions corresponding to the passed SVG content
724
745
* @throws IOException when the Stream cannot be read correctly
725
746
*/
726
- public static Image convertToImage (InputStream stream , PdfDocument document , ISvgConverterProperties props ) throws IOException {
727
- return new Image (convertToXObject (stream , document , props ));
747
+ public static Image convertToImage (InputStream stream , PdfDocument document , ISvgConverterProperties props )
748
+ throws IOException {
749
+ Image image = new Image (convertToXObject (stream , document , props ));
750
+ if (props instanceof SvgConverterProperties ) {
751
+ SvgConverterProperties properties = (SvgConverterProperties ) props ;
752
+ if (properties .getAccessibilityProperties ().getAlternateDescription () != null ) {
753
+ image .getAccessibilityProperties ()
754
+ .setAlternateDescription (properties .getAccessibilityProperties ().getAlternateDescription ());
755
+ }
756
+ image .getAccessibilityProperties ().setRole (properties .getAccessibilityProperties ().getRole ());
757
+ }
758
+ return image ;
728
759
}
729
760
730
761
/*
@@ -937,4 +968,35 @@ private static ResourceResolver createResourceResolver(final ISvgConverterProper
937
968
}
938
969
return new ResourceResolver (props .getBaseUri (), props .getResourceRetriever ());
939
970
}
971
+
972
+
973
+ private static void withTaggingIfNeeded (PdfDocument document , PdfCanvas canvas , PdfPage page ,
974
+ ISvgConverterProperties props , IOThrowingAction function )
975
+ throws IOException {
976
+ final boolean isTagged = document .isTagged ();
977
+ if (isTagged ) {
978
+ SvgConverterProperties properties = null ;
979
+ if (props instanceof SvgConverterProperties ) {
980
+ properties = (SvgConverterProperties ) props ;
981
+ }
982
+ String role = SVG_DEFAULT_ROLE ;
983
+ if (properties != null ) {
984
+ role = properties .getAccessibilityProperties ().getRole ();
985
+ }
986
+
987
+ TagTreePointer tagTreePointer = new TagTreePointer (document );
988
+ tagTreePointer .addTag (role );
989
+ tagTreePointer .setPageForTagging (page );
990
+ if (properties != null && properties .getAccessibilityProperties ().getAlternateDescription () != null ) {
991
+ tagTreePointer .getProperties ()
992
+ .setAlternateDescription (properties .getAccessibilityProperties ().getAlternateDescription ());
993
+ }
994
+ canvas .openTag (tagTreePointer .getTagReference ());
995
+ }
996
+ function .execute ();
997
+ if (isTagged ) {
998
+ canvas .closeTag ();
999
+ }
1000
+ }
1001
+
940
1002
}
0 commit comments