forked from pipeline-foundation/itext7-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into devsecops
- Loading branch information
Showing
16 changed files
with
657 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
itext.tests/itext.pdfa.tests/itext/pdfa/PdfA4EmbeddedFilesCheckTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
This file is part of the iText (R) project. | ||
Copyright (c) 1998-2023 Apryse Group NV | ||
Authors: Apryse Software. | ||
This program is offered under a commercial and under the AGPL license. | ||
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. | ||
AGPL licensing: | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
using System; | ||
using System.IO; | ||
using iText.Kernel.Pdf; | ||
using iText.Kernel.Pdf.Filespec; | ||
using iText.Pdfa.Exceptions; | ||
using iText.Test; | ||
|
||
namespace iText.Pdfa { | ||
[NUnit.Framework.Category("IntegrationTest")] | ||
public class PdfA4EmbeddedFilesCheckTest : ExtendedITextTest { | ||
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext | ||
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/"; | ||
|
||
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory | ||
+ "/test/itext/pdfa/PdfA4EmbeddedFilesCheckTest/"; | ||
|
||
[NUnit.Framework.OneTimeSetUp] | ||
public static void BeforeClass() { | ||
CreateOrClearDestinationFolder(DESTINATION_FOLDER); | ||
} | ||
|
||
// Test with successful creation PDF/A-4F (the same for PDF/A-4E and PDF/A-4) in | ||
// the embedded files meaning can be found in other tests (e.g. PdfA4CatalogCheckTest). | ||
[NUnit.Framework.Test] | ||
public virtual void PdfA4fWithoutEmbeddedFilesTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4fWithoutEmbeddedFilesTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent()); | ||
doc.AddNewPage(); | ||
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close()); | ||
NUnit.Framework.Assert.AreEqual(PdfaExceptionMessageConstant.NAME_DICTIONARY_SHALL_CONTAIN_EMBEDDED_FILES_KEY | ||
, e.Message); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PdfA4fWithEmbeddedFilesWithoutFTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4fWithEmbeddedFilesWithoutFTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent()); | ||
doc.AddNewPage(); | ||
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null | ||
, null, null); | ||
PdfDictionary fsDict = (PdfDictionary)fs.GetPdfObject(); | ||
fsDict.Remove(PdfName.F); | ||
doc.AddFileAttachment("file.txt", fs); | ||
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close()); | ||
NUnit.Framework.Assert.AreEqual(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY | ||
, e.Message); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PdfA4fWithEmbeddedFilesWithoutUFTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4fWithEmbeddedFilesWithoutUFTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent()); | ||
doc.AddNewPage(); | ||
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null | ||
, null, null); | ||
PdfDictionary fsDict = (PdfDictionary)fs.GetPdfObject(); | ||
fsDict.Remove(PdfName.UF); | ||
doc.AddFileAttachment("file.txt", fs); | ||
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close()); | ||
NUnit.Framework.Assert.AreEqual(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY | ||
, e.Message); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PdfA4fWithEmbeddedFilesWithoutAFRTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4fWithEmbeddedFilesWithoutAFRTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent()); | ||
doc.AddNewPage(); | ||
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null | ||
, null, null); | ||
PdfDictionary fsDict = (PdfDictionary)fs.GetPdfObject(); | ||
fsDict.Remove(PdfName.AFRelationship); | ||
doc.AddFileAttachment("file.txt", fs); | ||
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close()); | ||
NUnit.Framework.Assert.AreEqual(PdfaExceptionMessageConstant.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_AFRELATIONSHIP_KEY | ||
, e.Message); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PdfA4eWithEmbeddedFilesWithoutFTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4eWithEmbeddedFilesWithoutFTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4E, CreateOutputIntent()); | ||
doc.AddNewPage(); | ||
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null | ||
, null, null); | ||
PdfDictionary fsDict = (PdfDictionary)fs.GetPdfObject(); | ||
fsDict.Remove(PdfName.F); | ||
doc.AddFileAttachment("file.txt", fs); | ||
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close()); | ||
NUnit.Framework.Assert.AreEqual(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY | ||
, e.Message); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PdfA4WithEmbeddedFilesWithoutAFRTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4WithEmbeddedFilesWithoutAFRTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, CreateOutputIntent()); | ||
doc.AddNewPage(); | ||
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null | ||
, null, null); | ||
PdfDictionary fsDict = (PdfDictionary)fs.GetPdfObject(); | ||
fsDict.Remove(PdfName.AFRelationship); | ||
doc.AddFileAttachment("file.txt", fs); | ||
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close()); | ||
NUnit.Framework.Assert.AreEqual(PdfaExceptionMessageConstant.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_AFRELATIONSHIP_KEY | ||
, e.Message); | ||
} | ||
|
||
private PdfOutputIntent CreateOutputIntent() { | ||
return new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(SOURCE_FOLDER | ||
+ "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read)); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
itext.tests/itext.pdfa.tests/itext/pdfa/PdfA4MiscCheckTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
This file is part of the iText (R) project. | ||
Copyright (c) 1998-2023 Apryse Group NV | ||
Authors: Apryse Software. | ||
This program is offered under a commercial and under the AGPL license. | ||
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. | ||
AGPL licensing: | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
using System; | ||
using System.IO; | ||
using iText.Kernel.Pdf; | ||
using iText.Kernel.Utils; | ||
using iText.Test; | ||
using iText.Test.Pdfa; | ||
|
||
namespace iText.Pdfa { | ||
[NUnit.Framework.Category("IntegrationTest")] | ||
public class PdfA4MiscCheckTest : ExtendedITextTest { | ||
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext | ||
.CurrentContext.TestDirectory) + "/resources/itext/pdfa/"; | ||
|
||
public static readonly String CMP_FOLDER = SOURCE_FOLDER + "cmp/PdfA4MiscCheckTest/"; | ||
|
||
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory | ||
+ "/test/itext/pdfa/PdfA4MiscCheckTest/"; | ||
|
||
[NUnit.Framework.OneTimeSetUp] | ||
public static void BeforeClass() { | ||
CreateOrClearDestinationFolder(DESTINATION_FOLDER); | ||
} | ||
|
||
[NUnit.Framework.Test] | ||
public virtual void PdfA4CheckThatAsKeyIsAllowedTest() { | ||
String outPdf = DESTINATION_FOLDER + "pdfA4CheckThatAsKeyIsAllowedTest.pdf"; | ||
String cmpPdf = CMP_FOLDER + "cmp_pdfA4CheckThatAsKeyIsAllowedTest.pdf"; | ||
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)); | ||
using (PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, CreateOutputIntent())) { | ||
doc.AddNewPage(); | ||
PdfArray configs = new PdfArray(); | ||
PdfDictionary config = new PdfDictionary(); | ||
config.Put(PdfName.Name, new PdfString("CustomName")); | ||
PdfDictionary usageAppDict = new PdfDictionary(); | ||
usageAppDict.Put(PdfName.Event, PdfName.View); | ||
PdfArray categoryArray = new PdfArray(); | ||
categoryArray.Add(PdfName.Zoom); | ||
usageAppDict.Put(PdfName.Category, categoryArray); | ||
config.Put(PdfName.AS, usageAppDict); | ||
configs.Add(config); | ||
PdfDictionary ocProperties = new PdfDictionary(); | ||
ocProperties.Put(PdfName.Configs, configs); | ||
doc.GetCatalog().Put(PdfName.OCProperties, ocProperties); | ||
} | ||
CompareResult(outPdf, cmpPdf); | ||
} | ||
|
||
private PdfOutputIntent CreateOutputIntent() { | ||
return new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(SOURCE_FOLDER | ||
+ "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read)); | ||
} | ||
|
||
private void CompareResult(String outPdf, String cmpPdf) { | ||
NUnit.Framework.Assert.IsNull(new VeraPdfValidator().Validate(outPdf)); | ||
// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android) | ||
String result = new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"); | ||
if (result != null) { | ||
NUnit.Framework.Assert.Fail(result); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.