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 Oct 5, 2024
2 parents 88564e6 + 0f8556a commit f1cf35c
Show file tree
Hide file tree
Showing 54 changed files with 1,916 additions and 421 deletions.
109 changes: 109 additions & 0 deletions itext.tests/itext.kernel.tests/itext/kernel/pdf/layer/PdfLayerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ You should have received a copy of the GNU Affero General Public License
using iText.IO.Source;
using iText.Kernel.Exceptions;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Kernel.Utils;
using iText.Test;

Expand Down Expand Up @@ -357,6 +360,112 @@ public virtual void TestInStamperMode2() {
sourceFolder + "cmp_output_layered.pdf", destinationFolder, "diff"));
}

[NUnit.Framework.Test]
public virtual void TestReadAllLayersFromPage1() {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "input_layered.pdf"), CompareTool.CreateTestPdfWriter
(destinationFolder + "output_layered_2.pdf"));
PdfCanvas canvas = new PdfCanvas(pdfDoc, 1);
//create layer on page
PdfLayer newLayer = new PdfLayer("appended", pdfDoc);
canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 18);
PdfLayerTestUtils.AddTextInsideLayer(newLayer, canvas, "APPENDED CONTENT", 200, 600);
IList<PdfLayer> layersFromCatalog = pdfDoc.GetCatalog().GetOCProperties(true).GetLayers();
NUnit.Framework.Assert.AreEqual(13, layersFromCatalog.Count);
PdfPage page = pdfDoc.GetPage(1);
ICollection<PdfLayer> layersFromPage = page.GetPdfLayers();
NUnit.Framework.Assert.AreEqual(11, layersFromPage.Count);
pdfDoc.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "output_layered_2.pdf"
, sourceFolder + "cmp_output_layered_2.pdf", destinationFolder, "diff"));
}

[NUnit.Framework.Test]
public virtual void TestReadAllLayersFromDocumentWithComplexOCG() {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "input_complex_layers.pdf"), CompareTool
.CreateTestPdfWriter(destinationFolder + "output_complex_layers.pdf"));
IList<PdfLayer> layersFromCatalog = pdfDoc.GetCatalog().GetOCProperties(true).GetLayers();
NUnit.Framework.Assert.AreEqual(12, layersFromCatalog.Count);
PdfPage page = pdfDoc.GetPage(1);
ICollection<PdfLayer> layersFromPage = page.GetPdfLayers();
NUnit.Framework.Assert.AreEqual(10, layersFromPage.Count);
pdfDoc.Close();
}

//Read OCGs from different locations (annotations, content streams, xObjects) test block
[NUnit.Framework.Test]
public virtual void TestReadOcgFromStreamProperties() {
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
using (PdfDocument document = new PdfDocument(new PdfWriter(outputStream))) {
PdfPage page = document.AddNewPage();
PdfResources pdfResource = page.GetResources();
pdfResource.AddProperties(new PdfLayer("name", document).GetPdfObject());
pdfResource.MakeIndirect(document);
ICollection<PdfLayer> layersFromPage = page.GetPdfLayers();
NUnit.Framework.Assert.AreEqual(1, layersFromPage.Count);
}
}
}

[NUnit.Framework.Test]
public virtual void TestReadOcgFromAnnotation() {
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
using (PdfDocument fromDocument = new PdfDocument(new PdfWriter(outputStream))) {
PdfPage page = fromDocument.AddNewPage();
PdfAnnotation annotation = new PdfTextAnnotation(new Rectangle(50, 10));
annotation.SetLayer(new PdfLayer("name", fromDocument));
page.AddAnnotation(annotation);
ICollection<PdfLayer> layersFromPage = page.GetPdfLayers();
NUnit.Framework.Assert.AreEqual(1, layersFromPage.Count);
}
}
}

[NUnit.Framework.Test]
public virtual void TestReadOcgFromFlushedAnnotation() {
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
using (PdfDocument fromDocument = new PdfDocument(new PdfWriter(outputStream))) {
PdfPage page = fromDocument.AddNewPage();
PdfAnnotation annotation = new PdfTextAnnotation(new Rectangle(50, 10));
annotation.SetLayer(new PdfLayer("name", fromDocument));
page.AddAnnotation(annotation);
annotation.Flush();
ICollection<PdfLayer> layersFromPage = page.GetPdfLayers();
NUnit.Framework.Assert.AreEqual(1, layersFromPage.Count);
}
}
}

[NUnit.Framework.Test]
public virtual void TestReadOcgFromApAnnotation() {
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
using (PdfDocument fromDocument = new PdfDocument(new PdfWriter(outputStream))) {
PdfPage page = fromDocument.AddNewPage();
PdfAnnotation annotation = new PdfTextAnnotation(new Rectangle(50, 10));
PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(50, 10));
formXObject.SetLayer(new PdfLayer("someName1", fromDocument));
formXObject.MakeIndirect(fromDocument);
PdfDictionary nDict = new PdfDictionary();
nDict.Put(PdfName.ON, formXObject.GetPdfObject());
annotation.SetAppearance(PdfName.N, nDict);
formXObject = new PdfFormXObject(new Rectangle(50, 10));
formXObject.SetLayer(new PdfLayer("someName2", fromDocument));
PdfResources formResources = formXObject.GetResources();
formResources.AddProperties(new PdfLayer("someName3", fromDocument).GetPdfObject());
formXObject.MakeIndirect(fromDocument);
PdfDictionary rDict = new PdfDictionary();
rDict.Put(PdfName.OFF, formXObject.GetPdfObject());
annotation.SetAppearance(PdfName.R, rDict);
formXObject = new PdfFormXObject(new Rectangle(50, 10));
formXObject.SetLayer(new PdfLayer("someName4", fromDocument));
formXObject.MakeIndirect(fromDocument);
annotation.SetAppearance(PdfName.D, formXObject.GetPdfObject());
page.AddAnnotation(annotation);
ICollection<PdfLayer> layersFromPage = page.GetPdfLayers();
NUnit.Framework.Assert.AreEqual(4, layersFromPage.Count);
}
}
}

//TODO DEVSIX-8490 remove this test when implemented
[NUnit.Framework.Test]
public virtual void AddSecondParentlayerTest() {
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit f1cf35c

Please sign in to comment.