Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Sep 21, 2023
2 parents ebb0c9a + f9110cf commit aa259a7
Show file tree
Hide file tree
Showing 16 changed files with 657 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public virtual void PdfA4fForbiddenAnnotations1Test() {
.PDF_2_0));
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent());
PdfPage page = doc.AddNewPage();
AddSimpleEmbeddedFile(doc);
PdfAnnotation annot = new PdfSoundAnnotation(new Rectangle(100, 100, 100, 100), new PdfStream());
page.AddAnnotation(annot);
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
Expand All @@ -193,6 +194,7 @@ public virtual void PdfA4fForbiddenAnnotations2Test() {
.PDF_2_0));
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent());
PdfPage page = doc.AddNewPage();
AddSimpleEmbeddedFile(doc);
PdfAnnotation annot = new Pdf3DAnnotation(new Rectangle(100, 100, 100, 100), new PdfArray());
page.AddAnnotation(annot);
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfAConformanceException), () => doc.Close());
Expand All @@ -207,9 +209,7 @@ public virtual void PdfA4fAllowedAnnotations1Test() {
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
using (PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent())) {
PdfPage page = doc.AddNewPage();
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null
, null, null);
doc.AddFileAttachment("file.txt", fs);
AddSimpleEmbeddedFile(doc);
PdfAnnotation annot = new PdfFileAttachmentAnnotation(new Rectangle(100, 100, 100, 100));
annot.SetFlag(PdfAnnotation.PRINT);
annot.SetContents("Hello world");
Expand All @@ -226,9 +226,7 @@ public virtual void PdfA4fAllowedAnnotations2Test() {
PdfWriter writer = new PdfWriter(outPdf, new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0));
using (PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent())) {
PdfPage page = doc.AddNewPage();
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null
, null, null);
doc.AddFileAttachment("file.txt", fs);
AddSimpleEmbeddedFile(doc);
PdfAnnotation annot = new PdfLinkAnnotation(new Rectangle(100, 100, 100, 100));
annot.SetFlag(PdfAnnotation.PRINT);
annot.SetContents("Hello world");
Expand Down Expand Up @@ -259,6 +257,7 @@ public virtual void PdfA4ForbiddenAKeyWidgetAnnotationTest() {
.PDF_2_0));
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent());
PdfPage page = doc.AddNewPage();
AddSimpleEmbeddedFile(doc);
PdfAnnotation annot = new PdfWidgetAnnotation(new Rectangle(100, 100, 100, 100));
annot.GetPdfObject().Put(PdfName.A, (new PdfAction()).GetPdfObject());
annot.SetFlag(PdfAnnotation.PRINT);
Expand Down Expand Up @@ -290,6 +289,7 @@ public virtual void PdfA4ForbiddenAAKeyAnnotationTest() {
.PDF_2_0));
PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4F, CreateOutputIntent());
PdfPage page = doc.AddNewPage();
AddSimpleEmbeddedFile(doc);
PdfAnnotation annot = new PdfLinkAnnotation(new Rectangle(100, 100, 100, 100));
annot.GetPdfObject().Put(PdfName.AA, (new PdfAction()).GetPdfObject());
annot.SetFlag(PdfAnnotation.PRINT);
Expand All @@ -308,6 +308,12 @@ private void CompareResult(String outPdf, String cmpPdf) {
}
}

private void AddSimpleEmbeddedFile(PdfDocument doc) {
PdfFileSpec fs = PdfFileSpec.CreateEmbeddedFileSpec(doc, "file".GetBytes(), "description", "file.txt", null
, null, null);
doc.AddFileAttachment("file.txt", fs);
}

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));
Expand Down
142 changes: 142 additions & 0 deletions itext.tests/itext.pdfa.tests/itext/pdfa/PdfA4EmbeddedFilesCheckTest.cs
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 itext.tests/itext.pdfa.tests/itext/pdfa/PdfA4MiscCheckTest.cs
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);
}
}
}
}
Loading

0 comments on commit aa259a7

Please sign in to comment.