|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2025 Apryse Group NV |
| 4 | +Authors: Apryse Software. |
| 5 | +
|
| 6 | +This program is offered under a commercial and under the AGPL license. |
| 7 | +For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | +AGPL licensing: |
| 10 | +This program is free software: you can redistribute it and/or modify |
| 11 | +it under the terms of the GNU Affero General Public License as published by |
| 12 | +the Free Software Foundation, either version 3 of the License, or |
| 13 | +(at your option) any later version. |
| 14 | +
|
| 15 | +This program is distributed in the hope that it will be useful, |
| 16 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | +You should have received a copy of the GNU Affero General Public License |
| 21 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +*/ |
| 23 | +using System; |
| 24 | +using iText.Commons.Utils; |
| 25 | +using iText.Kernel.Pdf; |
| 26 | +using iText.Kernel.Pdf.Tagging; |
| 27 | +using iText.Kernel.Utils; |
| 28 | +using iText.Layout; |
| 29 | +using iText.Layout.Element; |
| 30 | +using iText.Pdfua; |
| 31 | +using iText.Pdfua.Exceptions; |
| 32 | +using iText.Svg.Processors.Impl; |
| 33 | +using iText.Test; |
| 34 | +using iText.Test.Pdfa; |
| 35 | + |
| 36 | +namespace iText.Svg.Converter { |
| 37 | + // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf/ua validation on Android) |
| 38 | + [NUnit.Framework.Category("IntegrationTest")] |
| 39 | + public class SvgTaggedConverterTest : ExtendedITextTest { |
| 40 | + public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 41 | + .CurrentContext.TestDirectory) + "/resources/itext/svg/converter/SvgTaggedConverterTest/"; |
| 42 | + |
| 43 | + public static readonly String DEST_FOLDER = TestUtil.GetOutputPath() + "/svg/converter/SvgTaggedConverterTest/"; |
| 44 | + |
| 45 | + [NUnit.Framework.OneTimeSetUp] |
| 46 | + public static void BeforeClass() { |
| 47 | + CreateOrClearDestinationFolder(DEST_FOLDER); |
| 48 | + } |
| 49 | + |
| 50 | + [NUnit.Framework.Test] |
| 51 | + public virtual void SimpleSvgTagged() { |
| 52 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 53 | + String destination = DEST_FOLDER + "simple.pdf"; |
| 54 | + String cmpFile = SOURCE_FOLDER + "cmp_simple.pdf"; |
| 55 | + WriterProperties writerProperties = new WriterProperties(); |
| 56 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 57 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destination, writerProperties)); |
| 58 | + pdfDocument.AddNewPage(); |
| 59 | + SvgConverter.DrawOnDocument(FileUtil.GetInputStreamForFile(source), pdfDocument, 1); |
| 60 | + pdfDocument.Close(); |
| 61 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destination, cmpFile, DEST_FOLDER, "diff_" |
| 62 | + )); |
| 63 | + } |
| 64 | + |
| 65 | + [NUnit.Framework.Test] |
| 66 | + public virtual void SimpleUACompliantSvgTagged() { |
| 67 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 68 | + String destination = DEST_FOLDER + "simpleUACompliantSvgTagged.pdf"; |
| 69 | + WriterProperties writerProperties = new WriterProperties(); |
| 70 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 71 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 72 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 73 | + pdfDocument.AddNewPage(); |
| 74 | + SvgConverter.DrawOnDocument(FileUtil.GetInputStreamForFile(source), pdfDocument, 1); |
| 75 | + Exception e = NUnit.Framework.Assert.Catch(typeof(PdfUAConformanceException), () => pdfDocument.Close()); |
| 76 | + NUnit.Framework.Assert.AreEqual(PdfUAExceptionMessageConstants.IMAGE_SHALL_HAVE_ALT, e.Message); |
| 77 | + } |
| 78 | + |
| 79 | + [NUnit.Framework.Test] |
| 80 | + public virtual void SimpleSvgTaggedWithConverterProperties() { |
| 81 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 82 | + String destination = DEST_FOLDER + "simpleSvgTaggedWithConverterProperties.pdf"; |
| 83 | + WriterProperties writerProperties = new WriterProperties(); |
| 84 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 85 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 86 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 87 | + pdfDocument.AddNewPage(); |
| 88 | + SvgConverterProperties properties = new SvgConverterProperties(); |
| 89 | + SvgConverter.DrawOnDocument(FileUtil.GetInputStreamForFile(source), pdfDocument, 1, properties); |
| 90 | + Exception e = NUnit.Framework.Assert.Catch(typeof(PdfUAConformanceException), () => pdfDocument.Close()); |
| 91 | + NUnit.Framework.Assert.AreEqual(PdfUAExceptionMessageConstants.IMAGE_SHALL_HAVE_ALT, e.Message); |
| 92 | + } |
| 93 | + |
| 94 | + [NUnit.Framework.Test] |
| 95 | + public virtual void SimpleSvgTaggedWithConverterPropertiesEmptyAlternateDescriptionEmpty() { |
| 96 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 97 | + String destination = DEST_FOLDER + "simpleSvgTaggedWithConverterPropertiesEmptyAlternateDescriptionEmpty.pdf"; |
| 98 | + WriterProperties writerProperties = new WriterProperties(); |
| 99 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 100 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 101 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 102 | + pdfDocument.AddNewPage(); |
| 103 | + SvgConverterProperties properties = new SvgConverterProperties(); |
| 104 | + properties.GetAccessibilityProperties().SetAlternateDescription(""); |
| 105 | + SvgConverter.DrawOnDocument(FileUtil.GetInputStreamForFile(source), pdfDocument, 1, properties); |
| 106 | + Exception e = NUnit.Framework.Assert.Catch(typeof(PdfUAConformanceException), () => pdfDocument.Close()); |
| 107 | + NUnit.Framework.Assert.AreEqual(PdfUAExceptionMessageConstants.IMAGE_SHALL_HAVE_ALT, e.Message); |
| 108 | + } |
| 109 | + |
| 110 | + [NUnit.Framework.Test] |
| 111 | + public virtual void SimpleSvgTaggedWithConverterPropertiesEmptyAlternateDescriptionSomeContent() { |
| 112 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 113 | + String destination = DEST_FOLDER + "simpleSvgTaggedWithConverterPropertiesEmptyAlternateDescriptionSomeContent.pdf"; |
| 114 | + WriterProperties writerProperties = new WriterProperties(); |
| 115 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 116 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 117 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 118 | + pdfDocument.AddNewPage(); |
| 119 | + SvgConverterProperties properties = new SvgConverterProperties(); |
| 120 | + properties.GetAccessibilityProperties().SetAlternateDescription("Hello there, "); |
| 121 | + SvgConverter.DrawOnDocument(FileUtil.GetInputStreamForFile(source), pdfDocument, 1, properties); |
| 122 | + pdfDocument.Close(); |
| 123 | + NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(destination)); |
| 124 | + } |
| 125 | + |
| 126 | + // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf/ua validation on Android) |
| 127 | + [NUnit.Framework.Test] |
| 128 | + public virtual void SimpleSvgTaggedWithConverterPropertiesTaggedAsArtifact() { |
| 129 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 130 | + String destination = DEST_FOLDER + "simpleSvgTaggedWithConverterPropertiesTaggedAsArtifact.pdf"; |
| 131 | + WriterProperties writerProperties = new WriterProperties(); |
| 132 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 133 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 134 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 135 | + pdfDocument.AddNewPage(); |
| 136 | + SvgConverterProperties properties = new SvgConverterProperties(); |
| 137 | + properties.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT); |
| 138 | + SvgConverter.DrawOnDocument(FileUtil.GetInputStreamForFile(source), pdfDocument, 1, properties); |
| 139 | + pdfDocument.Close(); |
| 140 | + NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(destination)); |
| 141 | + } |
| 142 | + |
| 143 | + // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf/ua validation on Android) |
| 144 | + [NUnit.Framework.Test] |
| 145 | + public virtual void ConvertToImage() { |
| 146 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 147 | + String destination = DEST_FOLDER + "convertToImage.pdf"; |
| 148 | + WriterProperties writerProperties = new WriterProperties(); |
| 149 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 150 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 151 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 152 | + Image image = SvgConverter.ConvertToImage(FileUtil.GetInputStreamForFile(source), pdfDocument); |
| 153 | + image.GetAccessibilityProperties().SetAlternateDescription("Hello!"); |
| 154 | + Document document = new Document(pdfDocument); |
| 155 | + document.Add(image); |
| 156 | + pdfDocument.Close(); |
| 157 | + NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(destination)); |
| 158 | + } |
| 159 | + |
| 160 | + // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf/ua validation on Android) |
| 161 | + [NUnit.Framework.Test] |
| 162 | + public virtual void ConvertToImageWithProps() { |
| 163 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 164 | + String destination = DEST_FOLDER + "convertToImageWithProps.pdf"; |
| 165 | + WriterProperties writerProperties = new WriterProperties(); |
| 166 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 167 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 168 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 169 | + SvgConverterProperties props = new SvgConverterProperties(); |
| 170 | + props.GetAccessibilityProperties().SetAlternateDescription("Bing bong"); |
| 171 | + Image image = SvgConverter.ConvertToImage(FileUtil.GetInputStreamForFile(source), pdfDocument, props); |
| 172 | + NUnit.Framework.Assert.AreEqual("Bing bong", image.GetAccessibilityProperties().GetAlternateDescription()); |
| 173 | + NUnit.Framework.Assert.AreEqual(StandardRoles.FIGURE, image.GetAccessibilityProperties().GetRole()); |
| 174 | + pdfDocument.Close(); |
| 175 | + } |
| 176 | + |
| 177 | + [NUnit.Framework.Test] |
| 178 | + public virtual void ConvertToImageWithPropsArtifacts() { |
| 179 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 180 | + String destination = DEST_FOLDER + "convertToImageWithPropsArtifacts.pdf"; |
| 181 | + WriterProperties writerProperties = new WriterProperties(); |
| 182 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 183 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 184 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 185 | + SvgConverterProperties props = new SvgConverterProperties(); |
| 186 | + props.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT); |
| 187 | + Image image = SvgConverter.ConvertToImage(FileUtil.GetInputStreamForFile(source), pdfDocument, props); |
| 188 | + NUnit.Framework.Assert.AreEqual(StandardRoles.ARTIFACT, image.GetAccessibilityProperties().GetRole()); |
| 189 | + pdfDocument.Close(); |
| 190 | + } |
| 191 | + |
| 192 | + [NUnit.Framework.Test] |
| 193 | + public virtual void DrawOnPage01() { |
| 194 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 195 | + String destination = DEST_FOLDER + "drawOnPage01.pdf"; |
| 196 | + String cmpFile = SOURCE_FOLDER + "cmp_drawOnPage01.pdf"; |
| 197 | + WriterProperties writerProperties = new WriterProperties(); |
| 198 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 199 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destination, writerProperties)); |
| 200 | + PdfPage page = pdfDocument.AddNewPage(); |
| 201 | + SvgConverter.DrawOnPage(FileUtil.GetInputStreamForFile(source), page); |
| 202 | + pdfDocument.Close(); |
| 203 | + NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destination, cmpFile, DEST_FOLDER, "diff_" |
| 204 | + )); |
| 205 | + } |
| 206 | + |
| 207 | + [NUnit.Framework.Test] |
| 208 | + public virtual void DrawOnPageWithUaCompliant() { |
| 209 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 210 | + String destination = DEST_FOLDER + "drawOnPage01.pdf"; |
| 211 | + WriterProperties writerProperties = new WriterProperties(); |
| 212 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 213 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 214 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 215 | + PdfPage page = pdfDocument.AddNewPage(); |
| 216 | + SvgConverter.DrawOnPage(FileUtil.GetInputStreamForFile(source), page); |
| 217 | + Exception e = NUnit.Framework.Assert.Catch(typeof(PdfUAConformanceException), () => pdfDocument.Close()); |
| 218 | + NUnit.Framework.Assert.AreEqual(PdfUAExceptionMessageConstants.IMAGE_SHALL_HAVE_ALT, e.Message); |
| 219 | + } |
| 220 | + |
| 221 | + [NUnit.Framework.Test] |
| 222 | + public virtual void DrawOnPage02() { |
| 223 | + String source = SOURCE_FOLDER + "simple.svg"; |
| 224 | + String destination = DEST_FOLDER + "drawOnPage02.pdf"; |
| 225 | + WriterProperties writerProperties = new WriterProperties(); |
| 226 | + writerProperties.SetPdfVersion(PdfVersion.PDF_2_0); |
| 227 | + PdfUADocument pdfDocument = new PdfUADocument(new PdfWriter(destination, writerProperties), new PdfUAConfig |
| 228 | + (PdfUAConformance.PDF_UA_2, "ua title", "en-US")); |
| 229 | + SvgConverterProperties converterProperties = new SvgConverterProperties(); |
| 230 | + converterProperties.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT); |
| 231 | + PdfPage page = pdfDocument.AddNewPage(); |
| 232 | + SvgConverter.DrawOnPage(FileUtil.GetInputStreamForFile(source), page, converterProperties); |
| 233 | + pdfDocument.Close(); |
| 234 | + NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(destination)); |
| 235 | + } |
| 236 | + // Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf/ua validation on Android) |
| 237 | + } |
| 238 | +} |
0 commit comments