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 Feb 13, 2025
2 parents 29461b3 + 92dde6b commit cbd38c8
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2025 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 iText.Kernel.XMP;
using iText.Kernel.XMP.Impl;
using iText.Test;

namespace iText.Kernel.Pdf {
[NUnit.Framework.Category("UnitTest")]
public class PdfUAConformanceTest : ExtendedITextTest {
[NUnit.Framework.Test]
public virtual void GetUA1ConformanceTest() {
XMPMeta meta = new XMPMetaImpl();
meta.SetProperty(XMPConst.NS_PDFUA_ID, XMPConst.PART, "1");
PdfConformance level = PdfConformance.GetConformance(meta);
NUnit.Framework.Assert.AreEqual(PdfUAConformance.PDF_UA_1, level.GetUAConformance());
}

[NUnit.Framework.Test]
public virtual void GetUA2ConformanceTest() {
XMPMeta meta = new XMPMetaImpl();
meta.SetProperty(XMPConst.NS_PDFUA_ID, XMPConst.PART, "2");
PdfConformance level = PdfConformance.GetConformance(meta);
NUnit.Framework.Assert.AreEqual(PdfUAConformance.PDF_UA_2, level.GetUAConformance());
}

[NUnit.Framework.Test]
public virtual void GetUAConformanceNullTest() {
XMPMeta meta = new XMPMetaImpl();
meta.SetProperty(XMPConst.NS_PDFUA_ID, XMPConst.PART, "3");
PdfConformance level = PdfConformance.GetConformance(meta);
NUnit.Framework.Assert.IsNull(level.GetUAConformance());
}
}
}
10 changes: 9 additions & 1 deletion itext.tests/itext.pdfua.tests/itext/pdfua/PdfUADocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ public class PdfUADocumentTest : ExtendedITextTest {
[NUnit.Framework.Test]
[LogMessage(PdfUALogMessageConstants.PDF_TO_PDF_UA_CONVERSION_IS_NOT_SUPPORTED, LogLevel = LogLevelConstants
.WARN)]
public virtual void OpenNotUaDocumentTest() {
public virtual void OpenNotUa1DocumentTest() {
NUnit.Framework.Assert.DoesNotThrow(() => new PdfUADocument(new PdfReader(SOURCE_FOLDER + "usualPdf.pdf"),
new PdfWriter(new MemoryStream()), new PdfUAConfig(PdfUAConformance.PDF_UA_1, "simple doc", "eng")));
}

[NUnit.Framework.Test]
[LogMessage(PdfUALogMessageConstants.PDF_TO_PDF_UA_CONVERSION_IS_NOT_SUPPORTED, LogLevel = LogLevelConstants
.WARN)]
public virtual void OpenNotUa2DocumentTest() {
NUnit.Framework.Assert.DoesNotThrow(() => new PdfUADocument(new PdfReader(SOURCE_FOLDER + "usualPdf.pdf"),
new PdfWriter(new MemoryStream()), new PdfUAConfig(PdfUAConformance.PDF_UA_2, "simple doc", "eng")));
}

[NUnit.Framework.Test]
[LogMessage(PdfUALogMessageConstants.WRITER_PROPERTIES_PDF_VERSION_WAS_OVERRIDDEN, LogLevel = LogLevelConstants
.WARN)]
Expand Down
6 changes: 6 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/PdfConformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class PdfConformance {
public static readonly iText.Kernel.Pdf.PdfConformance PDF_UA_1 = new iText.Kernel.Pdf.PdfConformance(PdfUAConformance
.PDF_UA_1);

public static readonly iText.Kernel.Pdf.PdfConformance PDF_UA_2 = new iText.Kernel.Pdf.PdfConformance(PdfUAConformance
.PDF_UA_2);

public static readonly iText.Kernel.Pdf.PdfConformance PDF_NONE_CONFORMANCE = new iText.Kernel.Pdf.PdfConformance
();

Expand Down Expand Up @@ -349,6 +352,9 @@ private static PdfUAConformance GetUAConformance(String part) {
if ("1".Equals(part)) {
return PdfUAConformance.PDF_UA_1;
}
if ("2".Equals(part)) {
return PdfUAConformance.PDF_UA_2;
}
return null;
}

Expand Down
4 changes: 4 additions & 0 deletions itext/itext.kernel/itext/kernel/pdf/PdfUAConformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public sealed class PdfUAConformance {
public static readonly iText.Kernel.Pdf.PdfUAConformance PDF_UA_1 = new iText.Kernel.Pdf.PdfUAConformance(
"1");

/// <summary>PDF/UA-2 conformance</summary>
public static readonly iText.Kernel.Pdf.PdfUAConformance PDF_UA_2 = new iText.Kernel.Pdf.PdfUAConformance(
"2");

private readonly String part;

//\cond DO_NOT_DOCUMENT
Expand Down
42 changes: 36 additions & 6 deletions itext/itext.pdfua/itext/pdfua/PdfUADocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 Microsoft.Extensions.Logging;
using iText.Commons;
using iText.Commons.Utils;
using iText.Kernel.Pdf;
using iText.Kernel.Validation;
using iText.Pdfua.Checkers;
using iText.Pdfua.Exceptions;
using iText.Pdfua.Logs;

namespace iText.Pdfua {
Expand All @@ -50,11 +52,11 @@ public PdfUADocument(PdfWriter writer, PdfUAConfig config)
/// <param name="properties">The properties for the PDF document.</param>
/// <param name="config">The configuration for the PDF/UA document.</param>
public PdfUADocument(PdfWriter writer, DocumentProperties properties, PdfUAConfig config)
: base(ConfigureWriterProperties(writer), properties) {
: base(ConfigureWriterProperties(writer, config.GetConformance()), properties) {
this.pdfConformance = new PdfConformance(config.GetConformance());
SetupUAConfiguration(config);
ValidationContainer validationContainer = new ValidationContainer();
PdfUA1Checker checker = new PdfUA1Checker(this);
PdfUAChecker checker = GetCorrectCheckerFromConformance(config.GetConformance());
validationContainer.AddChecker(checker);
this.GetDiContainer().Register(typeof(ValidationContainer), validationContainer);
this.pdfPageFactory = new PdfUAPageFactory(checker);
Expand All @@ -81,14 +83,14 @@ public PdfUADocument(PdfReader reader, PdfWriter writer, StampingProperties prop
}
SetupUAConfiguration(config);
ValidationContainer validationContainer = new ValidationContainer();
PdfUA1Checker checker = new PdfUA1Checker(this);
PdfUAChecker checker = GetCorrectCheckerFromConformance(config.GetConformance());
validationContainer.AddChecker(checker);
this.GetDiContainer().Register(typeof(ValidationContainer), validationContainer);
this.pdfPageFactory = new PdfUAPageFactory(checker);
}

private static PdfWriter ConfigureWriterProperties(PdfWriter writer) {
writer.GetProperties().AddPdfUaXmpMetadata(PdfUAConformance.PDF_UA_1);
private static PdfWriter ConfigureWriterProperties(PdfWriter writer, PdfUAConformance uaConformance) {
writer.GetProperties().AddPdfUaXmpMetadata(uaConformance);
if (writer.GetPdfVersion() != null && !writer.GetPdfVersion().Equals(PdfVersion.PDF_1_7)) {
ITextLogManager.GetLogger(typeof(iText.Pdfua.PdfUADocument)).LogWarning(MessageFormatUtil.Format(PdfUALogMessageConstants
.WRITER_PROPERTIES_PDF_VERSION_WAS_OVERRIDDEN, PdfVersion.PDF_1_7));
Expand All @@ -98,12 +100,40 @@ private static PdfWriter ConfigureWriterProperties(PdfWriter writer) {
}

private void SetupUAConfiguration(PdfUAConfig config) {
//basic configuration
// Basic configuration.
this.SetTagged();
this.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
this.GetCatalog().SetLang(new PdfString(config.GetLanguage()));
PdfDocumentInfo info = this.GetDocumentInfo();
info.SetTitle(config.GetTitle());
}

/// <summary>
/// Gets correct
/// <see cref="iText.Pdfua.Checkers.PdfUAChecker"/>
/// for specified PDF/UA conformance.
/// </summary>
/// <param name="uaConformance">the conformance for which checker is needed</param>
/// <returns>the correct PDF/UA checker</returns>
private PdfUAChecker GetCorrectCheckerFromConformance(PdfUAConformance uaConformance) {
PdfUAChecker checker;
switch (uaConformance.GetPart()) {
case "1": {
checker = new PdfUA1Checker(this);
break;
}

case "2": {
checker = new PdfUA2Checker(this);
break;
}

default: {
throw new ArgumentException(PdfUAExceptionMessageConstants.CANNOT_FIND_PDF_UA_CHECKER_FOR_SPECIFIED_CONFORMANCE
);
}
}
return checker;
}
}
}
95 changes: 94 additions & 1 deletion itext/itext.pdfua/itext/pdfua/PdfUAPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,118 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Pdfua.Checkers;

namespace iText.Pdfua {
//\cond DO_NOT_DOCUMENT
internal class PdfUAPage : PdfPage {
private readonly PdfUA1Checker checker;
private readonly PdfUAChecker checker;

/// <summary>
/// Creates new
/// <see cref="PdfUAPage"/>
/// instance.
/// </summary>
/// <param name="pdfObject">
/// the
/// <see cref="iText.Kernel.Pdf.PdfDictionary"/>
/// object on which the
/// <see cref="PdfUAPage"/>
/// will be based
/// </param>
/// <param name="checker">
///
/// <see cref="iText.Pdfua.Checkers.PdfUA1Checker"/>
/// to check the requirements of the PDF/UA-1 standard
/// </param>
[System.ObsoleteAttribute(@"in favour of PdfUAPage(iText.Kernel.Pdf.PdfDictionary, iText.Pdfua.Checkers.PdfUAChecker)"
)]
protected internal PdfUAPage(PdfDictionary pdfObject, PdfUA1Checker checker)
: base(pdfObject) {
this.checker = checker;
}

/// <summary>
/// Creates new
/// <see cref="PdfUAPage"/>
/// instance.
/// </summary>
/// <param name="pdfDocument">
/// the
/// <see cref="iText.Kernel.Pdf.PdfDocument"/>
/// object which will contain the
/// <see cref="PdfUAPage"/>
/// </param>
/// <param name="pageSize">
///
/// <see cref="iText.Kernel.Geom.PageSize"/>
/// the size of the
/// <see cref="PdfUAPage"/>
/// </param>
/// <param name="checker">
///
/// <see cref="iText.Pdfua.Checkers.PdfUA1Checker"/>
/// to check the requirements of the PDF/UA-1 standard
/// </param>
[System.ObsoleteAttribute(@"in favour of PdfUAPage(iText.Kernel.Pdf.PdfDocument, iText.Kernel.Geom.PageSize, iText.Pdfua.Checkers.PdfUAChecker)"
)]
protected internal PdfUAPage(PdfDocument pdfDocument, PageSize pageSize, PdfUA1Checker checker)
: base(pdfDocument, pageSize) {
this.checker = checker;
}

/// <summary>
/// Creates new
/// <see cref="PdfUAPage"/>
/// instance.
/// </summary>
/// <param name="pdfObject">
/// the
/// <see cref="iText.Kernel.Pdf.PdfDictionary"/>
/// object on which the
/// <see cref="PdfUAPage"/>
/// will be based
/// </param>
/// <param name="checker">
///
/// <see cref="iText.Pdfua.Checkers.PdfUAChecker"/>
/// to check the requirements of the PDF/UA standard
/// </param>
protected internal PdfUAPage(PdfDictionary pdfObject, PdfUAChecker checker)
: base(pdfObject) {
this.checker = checker;
}

/// <summary>
/// Creates new
/// <see cref="PdfUAPage"/>
/// instance.
/// </summary>
/// <param name="pdfDocument">
/// the
/// <see cref="iText.Kernel.Pdf.PdfDocument"/>
/// object which will contain the
/// <see cref="PdfUAPage"/>
/// </param>
/// <param name="pageSize">
///
/// <see cref="iText.Kernel.Geom.PageSize"/>
/// the size of the
/// <see cref="PdfUAPage"/>
/// </param>
/// <param name="checker">
///
/// <see cref="iText.Pdfua.Checkers.PdfUAChecker"/>
/// to check the requirements of the PDF/UA standard
/// </param>
protected internal PdfUAPage(PdfDocument pdfDocument, PageSize pageSize, PdfUAChecker checker)
: base(pdfDocument, pageSize) {
this.checker = checker;
}

public override void Flush(bool flushResourcesContentStreams) {
if (GetDocument().IsClosing()) {
base.Flush(flushResourcesContentStreams);
Expand Down
15 changes: 14 additions & 1 deletion itext/itext.pdfua/itext/pdfua/PdfUAPageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Pdfua.Checkers;
Expand All @@ -28,7 +29,7 @@ namespace iText.Pdfua {
//\cond DO_NOT_DOCUMENT
/// <summary>The class implements PDF page factory which is used for creating correct PDF/UA documents.</summary>
internal class PdfUAPageFactory : IPdfPageFactory {
private readonly PdfUA1Checker checker;
private readonly PdfUAChecker checker;

/// <summary>
/// Instantiates a new
Expand All @@ -37,10 +38,22 @@ internal class PdfUAPageFactory : IPdfPageFactory {
/// <see cref="iText.Pdfua.Checkers.PdfUA1Checker"/>.
/// </summary>
/// <param name="checker">the PDF/UA checker</param>
[System.ObsoleteAttribute(@"in favour of PdfUAPageFactory(iText.Pdfua.Checkers.PdfUAChecker)")]
public PdfUAPageFactory(PdfUA1Checker checker) {
this.checker = checker;
}

/// <summary>
/// Instantiates a new
/// <see cref="PdfUAPageFactory"/>
/// instance based on
/// <see cref="iText.Pdfua.Checkers.PdfUAChecker"/>.
/// </summary>
/// <param name="checker">the PDF/UA checker</param>
public PdfUAPageFactory(PdfUAChecker checker) {
this.checker = checker;
}

/// <param name="pdfObject">
/// the
/// <see cref="iText.Kernel.Pdf.PdfDictionary"/>
Expand Down
Loading

0 comments on commit cbd38c8

Please sign in to comment.