|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2023 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 | +package com.itextpdf.pdfa; |
| 24 | + |
| 25 | +import com.itextpdf.kernel.pdf.PdfAConformanceLevel; |
| 26 | +import com.itextpdf.kernel.pdf.PdfDictionary; |
| 27 | +import com.itextpdf.kernel.pdf.PdfName; |
| 28 | +import com.itextpdf.kernel.pdf.PdfOutputIntent; |
| 29 | +import com.itextpdf.kernel.pdf.PdfVersion; |
| 30 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 31 | +import com.itextpdf.kernel.pdf.WriterProperties; |
| 32 | +import com.itextpdf.kernel.pdf.filespec.PdfFileSpec; |
| 33 | +import com.itextpdf.pdfa.exceptions.PdfAConformanceException; |
| 34 | +import com.itextpdf.pdfa.exceptions.PdfaExceptionMessageConstant; |
| 35 | +import com.itextpdf.test.ExtendedITextTest; |
| 36 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 37 | + |
| 38 | +import java.io.FileInputStream; |
| 39 | +import java.io.FileNotFoundException; |
| 40 | +import java.io.IOException; |
| 41 | +import org.junit.Assert; |
| 42 | +import org.junit.BeforeClass; |
| 43 | +import org.junit.Test; |
| 44 | +import org.junit.experimental.categories.Category; |
| 45 | + |
| 46 | +@Category(IntegrationTest.class) |
| 47 | +public class PdfA4EmbeddedFilesCheckTest extends ExtendedITextTest { |
| 48 | + public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/pdfa/"; |
| 49 | + public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/pdfa/PdfA4EmbeddedFilesCheckTest/"; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void beforeClass() { |
| 53 | + createOrClearDestinationFolder(DESTINATION_FOLDER); |
| 54 | + } |
| 55 | + |
| 56 | + // Test with successful creation PDF/A-4F (the same for PDF/A-4E and PDF/A-4) in |
| 57 | + // the embedded files meaning can be found in other tests (e.g. PdfA4CatalogCheckTest). |
| 58 | + |
| 59 | + @Test |
| 60 | + public void pdfA4fWithoutEmbeddedFilesTest() throws IOException { |
| 61 | + String outPdf = DESTINATION_FOLDER + "pdfA4fWithoutEmbeddedFilesTest.pdf"; |
| 62 | + |
| 63 | + PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)); |
| 64 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, createOutputIntent()); |
| 65 | + doc.addNewPage(); |
| 66 | + |
| 67 | + Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> doc.close()); |
| 68 | + Assert.assertEquals(PdfaExceptionMessageConstant.NAME_DICTIONARY_SHALL_CONTAIN_EMBEDDED_FILES_KEY, e.getMessage()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void pdfA4fWithEmbeddedFilesWithoutFTest() throws IOException { |
| 73 | + String outPdf = DESTINATION_FOLDER + "pdfA4fWithEmbeddedFilesWithoutFTest.pdf"; |
| 74 | + |
| 75 | + PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)); |
| 76 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, createOutputIntent()); |
| 77 | + doc.addNewPage(); |
| 78 | + |
| 79 | + PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec( |
| 80 | + doc, "file".getBytes(), "description", "file.txt", null, null, null); |
| 81 | + PdfDictionary fsDict = (PdfDictionary) fs.getPdfObject(); |
| 82 | + fsDict.remove(PdfName.F); |
| 83 | + doc.addFileAttachment("file.txt", fs); |
| 84 | + |
| 85 | + Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> doc.close()); |
| 86 | + Assert.assertEquals(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY, e.getMessage()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void pdfA4fWithEmbeddedFilesWithoutUFTest() throws IOException { |
| 91 | + String outPdf = DESTINATION_FOLDER + "pdfA4fWithEmbeddedFilesWithoutUFTest.pdf"; |
| 92 | + |
| 93 | + PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)); |
| 94 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, createOutputIntent()); |
| 95 | + doc.addNewPage(); |
| 96 | + |
| 97 | + PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec( |
| 98 | + doc, "file".getBytes(), "description", "file.txt", null, null, null); |
| 99 | + PdfDictionary fsDict = (PdfDictionary) fs.getPdfObject(); |
| 100 | + fsDict.remove(PdfName.UF); |
| 101 | + doc.addFileAttachment("file.txt", fs); |
| 102 | + |
| 103 | + Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> doc.close()); |
| 104 | + Assert.assertEquals(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY, e.getMessage()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void pdfA4fWithEmbeddedFilesWithoutAFRTest() throws IOException { |
| 109 | + String outPdf = DESTINATION_FOLDER + "pdfA4fWithEmbeddedFilesWithoutAFRTest.pdf"; |
| 110 | + |
| 111 | + PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)); |
| 112 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, createOutputIntent()); |
| 113 | + doc.addNewPage(); |
| 114 | + |
| 115 | + PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec( |
| 116 | + doc, "file".getBytes(), "description", "file.txt", null, null, null); |
| 117 | + PdfDictionary fsDict = (PdfDictionary) fs.getPdfObject(); |
| 118 | + fsDict.remove(PdfName.AFRelationship); |
| 119 | + doc.addFileAttachment("file.txt", fs); |
| 120 | + |
| 121 | + Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> doc.close()); |
| 122 | + Assert.assertEquals(PdfaExceptionMessageConstant.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_AFRELATIONSHIP_KEY, e.getMessage()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void pdfA4eWithEmbeddedFilesWithoutFTest() throws IOException { |
| 127 | + String outPdf = DESTINATION_FOLDER + "pdfA4eWithEmbeddedFilesWithoutFTest.pdf"; |
| 128 | + |
| 129 | + PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)); |
| 130 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4E, createOutputIntent()); |
| 131 | + doc.addNewPage(); |
| 132 | + |
| 133 | + PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec( |
| 134 | + doc, "file".getBytes(), "description", "file.txt", null, null, null); |
| 135 | + PdfDictionary fsDict = (PdfDictionary) fs.getPdfObject(); |
| 136 | + fsDict.remove(PdfName.F); |
| 137 | + doc.addFileAttachment("file.txt", fs); |
| 138 | + |
| 139 | + Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> doc.close()); |
| 140 | + Assert.assertEquals(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY, e.getMessage()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void pdfA4WithEmbeddedFilesWithoutAFRTest() throws IOException { |
| 145 | + String outPdf = DESTINATION_FOLDER + "pdfA4WithEmbeddedFilesWithoutAFRTest.pdf"; |
| 146 | + |
| 147 | + PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)); |
| 148 | + PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, createOutputIntent()); |
| 149 | + doc.addNewPage(); |
| 150 | + |
| 151 | + PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec( |
| 152 | + doc, "file".getBytes(), "description", "file.txt", null, null, null); |
| 153 | + PdfDictionary fsDict = (PdfDictionary) fs.getPdfObject(); |
| 154 | + fsDict.remove(PdfName.AFRelationship); |
| 155 | + doc.addFileAttachment("file.txt", fs); |
| 156 | + |
| 157 | + Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> doc.close()); |
| 158 | + Assert.assertEquals(PdfaExceptionMessageConstant.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_AFRELATIONSHIP_KEY, e.getMessage()); |
| 159 | + } |
| 160 | + |
| 161 | + private PdfOutputIntent createOutputIntent() throws FileNotFoundException { |
| 162 | + return new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", |
| 163 | + new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm")); |
| 164 | + } |
| 165 | +} |
0 commit comments