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 2, 2024
2 parents 504ebb5 + 14aba17 commit a5cd1e6
Show file tree
Hide file tree
Showing 32 changed files with 141 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.commons.tests/itext.commons.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.forms.tests/itext.forms.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.io.tests/itext.io.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<ItemGroup>
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
</ItemGroup>
<PropertyGroup>
<NoWarn>1701;1702;1591;1570;1572;1573;1574;1580;1584;1658</NoWarn>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.kernel.tests/itext.kernel.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,17 +544,17 @@ public virtual void CopyDFieldsToEmptyDocumentTest() {
NUnit.Framework.Assert.AreEqual("Off1", off.GetAsDictionary(0).GetAsString(PdfName.Name).ToUnicodeString()
);
NUnit.Framework.Assert.IsNull(dDict.GetAsArray(PdfName.Creator));
NUnit.Framework.Assert.AreEqual("OCConfigName0", dDict.GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.IsNull(dDict.GetAsName(PdfName.BaseState));
NUnit.Framework.Assert.AreEqual("Name", dDict.GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.AreEqual(PdfName.ON, dDict.GetAsName(PdfName.BaseState));
PdfArray asArray = dDict.GetAsArray(PdfName.AS);
NUnit.Framework.Assert.AreEqual(1, asArray.Size());
NUnit.Framework.Assert.AreEqual(1, asArray.GetAsDictionary(0).GetAsArray(PdfName.Category).Size());
NUnit.Framework.Assert.AreEqual(PdfName.Print, asArray.GetAsDictionary(0).GetAsArray(PdfName.Category).GetAsName
(0));
NUnit.Framework.Assert.AreEqual("noPrint1", asArray.GetAsDictionary(0).GetAsArray(PdfName.OCGs).GetAsDictionary
(0).GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.IsNull(dDict.GetAsName(PdfName.Intent));
NUnit.Framework.Assert.IsNull(dDict.GetAsName(PdfName.ListMode));
NUnit.Framework.Assert.AreEqual(PdfName.View, dDict.GetAsName(PdfName.Intent));
NUnit.Framework.Assert.AreEqual(PdfName.VisiblePages, dDict.GetAsName(PdfName.ListMode));
}

[NUnit.Framework.Test]
Expand Down Expand Up @@ -665,8 +665,8 @@ public virtual void CopyDFieldsToDocumentWithDDictTest() {
NUnit.Framework.Assert.AreEqual("from_Off1", off.GetAsDictionary(2).GetAsString(PdfName.Name).ToUnicodeString
());
NUnit.Framework.Assert.IsNull(dDict.GetAsArray(PdfName.Creator));
NUnit.Framework.Assert.AreEqual("OCConfigName0", dDict.GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.IsNull(dDict.GetAsName(PdfName.BaseState));
NUnit.Framework.Assert.AreEqual("Name", dDict.GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.AreEqual(PdfName.ON, dDict.GetAsName(PdfName.BaseState));
PdfArray asArray = dDict.GetAsArray(PdfName.AS);
NUnit.Framework.Assert.AreEqual(1, asArray.Size());
NUnit.Framework.Assert.AreEqual(1, asArray.GetAsDictionary(0).GetAsArray(PdfName.Category).Size());
Expand All @@ -677,8 +677,8 @@ public virtual void CopyDFieldsToDocumentWithDDictTest() {
(0).GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.AreEqual("from_noPrint1", asArray.GetAsDictionary(0).GetAsArray(PdfName.OCGs).GetAsDictionary
(1).GetAsString(PdfName.Name).ToUnicodeString());
NUnit.Framework.Assert.IsNull(dDict.GetAsName(PdfName.Intent));
NUnit.Framework.Assert.IsNull(dDict.GetAsName(PdfName.ListMode));
NUnit.Framework.Assert.AreEqual(PdfName.View, dDict.GetAsName(PdfName.Intent));
NUnit.Framework.Assert.AreEqual(PdfName.VisiblePages, dDict.GetAsName(PdfName.ListMode));
}

// Copy OCGs from different locations (OCMDs, annotations, content streams, xObjects) test block
Expand Down Expand Up @@ -976,12 +976,13 @@ private static byte[] GetDocumentWithAllDFields() {
off2.SetOn(false);
pdfResource.MakeIndirect(fromDocument);
PdfOCProperties ocProperties = fromDocument.GetCatalog().GetOCProperties(true);
PdfDictionary dDictionary = ocProperties.GetPdfObject().GetAsDictionary(PdfName.D);
// Creator (will be not copied)
ocProperties.GetPdfObject().Put(PdfName.Creator, new PdfString("CreatorName", PdfEncodings.UNICODE_BIG));
dDictionary.Put(PdfName.Creator, new PdfString("CreatorName", PdfEncodings.UNICODE_BIG));
// Name (will be automatically changed)
ocProperties.GetPdfObject().Put(PdfName.Name, new PdfString("Name", PdfEncodings.UNICODE_BIG));
dDictionary.Put(PdfName.Name, new PdfString("Name", PdfEncodings.UNICODE_BIG));
// BaseState (will be not copied)
ocProperties.GetPdfObject().Put(PdfName.BaseState, PdfName.OFF);
dDictionary.Put(PdfName.BaseState, PdfName.ON);
// AS (will be automatically changed)
PdfArray asArray = new PdfArray();
PdfDictionary dict = new PdfDictionary();
Expand All @@ -993,14 +994,14 @@ private static byte[] GetDocumentWithAllDFields() {
ocgs.Add(locked1.GetPdfObject());
dict.Put(PdfName.OCGs, ocgs);
asArray.Add(dict);
ocProperties.GetPdfObject().Put(PdfName.AS, asArray);
dDictionary.Put(PdfName.AS, asArray);
PdfLayer noPrint1 = new PdfLayer("noPrint1", fromDocument);
pdfResource.AddProperties(noPrint1.GetPdfObject());
noPrint1.SetPrint("Print", false);
// Intent (will be not copied)
ocProperties.GetPdfObject().Put(PdfName.Intent, PdfName.Design);
dDictionary.Put(PdfName.Intent, PdfName.View);
// ListMode (will be not copied)
ocProperties.GetPdfObject().Put(PdfName.ListMode, PdfName.VisiblePages);
dDictionary.Put(PdfName.ListMode, PdfName.VisiblePages);
}
fromDocBytes = outputStream.ToArray();
}
Expand Down
13 changes: 13 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfCopyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ public virtual void CopyPagesLinkAnnotationTest() {
));
}

[NUnit.Framework.Test]
public virtual void CopyDocWithFullDDictionary() {
String outFileName = destinationFolder + "copyDocWithDDictionary.pdf";
String cmpFileName = sourceFolder + "cmp_copyDocWithDDictionary.pdf";
PdfDocument inPdf = new PdfDocument(new PdfReader(sourceFolder + "DocWithDDictionary.pdf"));
PdfDocument outPdf = new PdfDocument(new PdfWriter(outFileName));
inPdf.CopyPagesTo(1, 1, outPdf);
inPdf.Close();
outPdf.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
));
}

private IList<PdfAnnotation> GetPdfAnnotations(PdfDocument pdfDoc) {
int number = pdfDoc.GetNumberOfPages();
List<PdfAnnotation> annotations = new List<PdfAnnotation>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public virtual void TestInStamperMode1() {
(destinationFolder + "output_copy_layered.pdf"));
pdfDoc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "output_copy_layered.pdf"
, sourceFolder + "input_layered.pdf", destinationFolder, "diff"));
, sourceFolder + "cmp_output_copy_layered.pdf", destinationFolder, "diff"));
}

[NUnit.Framework.Test]
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion itext.tests/itext.layout.tests/itext.layout.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.pdfa.tests/itext.pdfa.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.pdftest.tests/itext.pdftest.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.pdfua.tests/itext.pdfua.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.sign.tests/itext.sign.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<ItemGroup>
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion itext.tests/itext.svg.tests/itext.svg.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<ItemGroup>
</ItemGroup>
<ItemGroup>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public sealed class KernelLogMessageConstant {
public const String FORMFIELD_ANNOTATION_WILL_NOT_BE_FLATTENED = "Form field annotation flattening is not "
+ "supported. Use the PdfAcroForm#flattenFields() method instead.";

public const String INVALID_DDICTIONARY_FIELD_VALUE = "The default configuration dictionary field {0}" + " has a value of {1}, which is not the required value for this field. The field will not be processed.";

private KernelLogMessageConstant() {
}
//Private constructor will prevent the instantiation of this class directly
Expand Down
43 changes: 39 additions & 4 deletions itext/itext.kernel/itext/kernel/pdf/OcgPropertiesCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ You should have received a copy of the GNU Affero General Public License
using iText.Commons;
using iText.Commons.Utils;
using iText.IO.Font;
using iText.Kernel.Logs;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Layer;

namespace iText.Kernel.Pdf {
internal sealed class OcgPropertiesCopier {
Expand Down Expand Up @@ -251,19 +253,20 @@ private static void CopyDDictionary(ICollection<PdfIndirectReference> fromOcgsTo
toOcProperties.Put(PdfName.D, new PdfDictionary());
}
PdfDictionary toDDict = toOcProperties.GetAsDictionary(PdfName.D);
// The Name field is not copied because it will be given when flushing the PdfOCProperties
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDStringField(PdfName.Name, fromDDict, toDDict);
// Delete the Creator field because the D dictionary are changing
toDDict.Remove(PdfName.Creator);
// The BaseState field is not copied because for dictionary D BaseState should have the value ON, which is the default
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDNameField(PdfName.BaseState, fromDDict, toDDict);
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.ON, fromOcgsToCopy, fromDDict, toDDict, toDocument
);
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.OFF, fromOcgsToCopy, fromDDict, toDDict, toDocument
);
// The Intent field is not copied because for dictionary D Intent should have the value View, which is the default
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDNameField(PdfName.Intent, fromDDict, toDDict);
// The AS field is not copied because it will be given when flushing the PdfOCProperties
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.Order, fromOcgsToCopy, fromDDict, toDDict, toDocument
);
// The ListModel field is not copied because it only affects the visual presentation of the layers
// The ListMode field is copied, but it only affects the visual presentation of the layers
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDNameField(PdfName.ListMode, fromDDict, toDDict);
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.RBGroups, fromOcgsToCopy, fromDDict, toDDict,
toDocument);
iText.Kernel.Pdf.OcgPropertiesCopier.CopyDArrayField(PdfName.Locked, fromOcgsToCopy, fromDDict, toDDict, toDocument
Expand All @@ -278,6 +281,38 @@ private static void AttemptToAddObjectToArray(ICollection<PdfIndirectReference>
}
}

private static void CopyDNameField(PdfName fieldToCopy, PdfDictionary fromDict, PdfDictionary toDict) {
PdfName fromName = fromDict.GetAsName(fieldToCopy);
if (fromName == null || toDict.GetAsName(fieldToCopy) != null) {
return;
}
if (PdfOCProperties.CheckDDictonaryFieldValue(fieldToCopy, fromName)) {
toDict.Put(fieldToCopy, fromName);
}
else {
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Kernel.Pdf.OcgPropertiesCopier));
String warnText = MessageFormatUtil.Format(KernelLogMessageConstant.INVALID_DDICTIONARY_FIELD_VALUE, fieldToCopy
, fromName);
logger.LogWarning(warnText);
}
}

private static void CopyDStringField(PdfName fieldToCopy, PdfDictionary fromDict, PdfDictionary toDict) {
PdfString fromString = fromDict.GetAsString(fieldToCopy);
if (fromString == null || toDict.GetAsString(fieldToCopy) != null) {
return;
}
if (PdfOCProperties.CheckDDictonaryFieldValue(fieldToCopy, fromString)) {
toDict.Put(fieldToCopy, fromString);
}
else {
ILogger logger = ITextLogManager.GetLogger(typeof(iText.Kernel.Pdf.OcgPropertiesCopier));
String warnText = MessageFormatUtil.Format(KernelLogMessageConstant.INVALID_DDICTIONARY_FIELD_VALUE, fieldToCopy
, fromString);
logger.LogWarning(warnText);
}
}

private static void CopyDArrayField(PdfName fieldToCopy, ICollection<PdfIndirectReference> fromOcgsToCopy,
PdfDictionary fromDict, PdfDictionary toDict, PdfDocument toDocument) {
if (fromDict.GetAsArray(fieldToCopy) == null) {
Expand Down
8 changes: 7 additions & 1 deletion itext/itext.kernel/itext/kernel/pdf/PdfCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class PdfCatalog : PdfObjectWrapper<PdfDictionary> {
// If this flag is false all outline operations will be ignored
private bool outlineMode;

private bool ocgCopied = false;

/// <summary>
/// Create
/// <see cref="PdfCatalog"/>
Expand Down Expand Up @@ -494,7 +496,11 @@ public virtual iText.Kernel.Pdf.PdfCatalog Remove(PdfName key) {
/// </summary>
/// <returns>boolean indicating if the dictionary needs to be reconstructed</returns>
protected internal virtual bool IsOCPropertiesMayHaveChanged() {
return ocProperties != null;
return ocProperties != null || ocgCopied;
}

internal virtual void SetOcgCopied(bool ocgCopied) {
this.ocgCopied = ocgCopied;
}

internal virtual PdfPagesTree GetPageTree() {
Expand Down
1 change: 1 addition & 0 deletions itext/itext.kernel/itext/kernel/pdf/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,7 @@ public virtual IList<PdfPage> CopyPagesTo(IList<int> pagesToCopy, iText.Kernel.P
// Copying OCGs should go after copying LinkAnnotations
if (GetCatalog() != null && GetCatalog().GetPdfObject().GetAsDictionary(PdfName.OCProperties) != null) {
OcgPropertiesCopier.CopyOCGProperties(this, toDocument, page2page);
toDocument.GetCatalog().SetOcgCopied(true);
}
// It's important to copy tag structure after link annotations were copied, because object content items in tag
// structure are not copied in case if their's OBJ key is annotation and doesn't contain /P entry.
Expand Down
Loading

0 comments on commit a5cd1e6

Please sign in to comment.