PdfLayerMembershipTest.java

/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2026 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/>.
 */
package com.itextpdf.kernel.pdf.layer;

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.utils.CompareTool;
import com.itextpdf.test.ExtendedITextTest;
import com.itextpdf.test.TestUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("IntegrationTest")
public class PdfLayerMembershipTest extends ExtendedITextTest{

    public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/kernel/pdf/layer/PdfLayerMembershipTest/";
    public static final String DESTINATION_FOLDER = TestUtil.getOutputPath() + "/kernel/pdf/layer/PdfLayerMembershipTest/";

    @BeforeAll
    public static void beforeClass() {
        createDestinationFolder(DESTINATION_FOLDER);
    }

    @AfterAll
    public static void afterClass() {
        CompareTool.cleanup(DESTINATION_FOLDER);
    }

    @Test
    public void enabledVisibilityPolicyAllOnTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "enabledVisibilityPolicyAllOnTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAllOn = new PdfLayerMembership(pdfDoc);
        layerMembershipAllOn.addLayer(allLayers.get(1));
        layerMembershipAllOn.addLayer(allLayers.get(2));
        layerMembershipAllOn.setVisibilityPolicy(PdfName.AllOn);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAllOn, canvas, "visibilityPolicyAllOnTest", 200, 500);


        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }


    @Test
    public void disabledVisibilityPolicyAllOnTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "disabledVisibilityPolicyAllOnTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAllOn = new PdfLayerMembership(pdfDoc);
        layerMembershipAllOn.addLayer(allLayers.get(1));
        layerMembershipAllOn.addLayer(allLayers.get(0));
        layerMembershipAllOn.setVisibilityPolicy(PdfName.AllOn);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAllOn, canvas, "visibilityPolicyAllOnTest", 200, 500);


        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void enabledVisibilityPolicyAllOffTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "enabledVisibilityPolicyAllOffTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAllOff = new PdfLayerMembership(pdfDoc);
        layerMembershipAllOff.addLayer(allLayers.get(0));
        layerMembershipAllOff.addLayer(allLayers.get(3));
        layerMembershipAllOff.setVisibilityPolicy(PdfName.AllOff);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAllOff, canvas, "visibilityPolicyAllOffTest", 200, 500);

        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void disabledVisibilityPolicyAllOffTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "disabledVisibilityPolicyAllOffTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAllOff = new PdfLayerMembership(pdfDoc);
        layerMembershipAllOff.addLayer(allLayers.get(0));
        layerMembershipAllOff.addLayer(allLayers.get(1));
        layerMembershipAllOff.setVisibilityPolicy(PdfName.AllOff);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAllOff, canvas, "visibilityPolicyAllOffTest", 200, 500);

        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void enabledVisibilityPolicyAnyOnTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "enabledVisibilityPolicyAnyOnTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAnyOn = new PdfLayerMembership(pdfDoc);
        layerMembershipAnyOn.addLayer(allLayers.get(0));
        layerMembershipAnyOn.addLayer(allLayers.get(1));
        layerMembershipAnyOn.setVisibilityPolicy(PdfName.AnyOn);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAnyOn, canvas, "visibilityPolicyAnyOnTest", 200, 500);


        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }


    @Test
    public void disabledVisibilityPolicyAnyOnTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "disabledVisibilityPolicyAnyOnTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAnyOn = new PdfLayerMembership(pdfDoc);
        layerMembershipAnyOn.addLayer(allLayers.get(0));
        layerMembershipAnyOn.addLayer(allLayers.get(3));
        layerMembershipAnyOn.setVisibilityPolicy(PdfName.AnyOn);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAnyOn, canvas, "visibilityPolicyAnyOnTest", 200, 500);


        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }


    @Test
    public void enabledVisibilityPolicyAnyOffTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "enabledVisibilityPolicyAnyOffTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAnyOn = new PdfLayerMembership(pdfDoc);
        layerMembershipAnyOn.addLayer(allLayers.get(0));
        layerMembershipAnyOn.addLayer(allLayers.get(1));
        layerMembershipAnyOn.setVisibilityPolicy(PdfName.AnyOff);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAnyOn, canvas, "visibilityPolicyAnyOffTest", 200, 500);

        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void disabledVisibilityPolicyAnyOffTest() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "disabledVisibilityPolicyAnyOffTest.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAnyOn = new PdfLayerMembership(pdfDoc);
        layerMembershipAnyOn.addLayer(allLayers.get(1));
        layerMembershipAnyOn.addLayer(allLayers.get(2));
        layerMembershipAnyOn.setVisibilityPolicy(PdfName.AnyOff);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAnyOn, canvas, "visibilityPolicyAnyOffTest", 200, 500);

        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void enabledVisualExpressionTest01() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "enabledVisualExpressionTest01.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAnyOn = new PdfLayerMembership(pdfDoc);

        // create expression with the AND operator as the first operand
        PdfVisibilityExpression expression = new PdfVisibilityExpression(PdfName.And);
        // add an empty dictionary as the second operand
        expression.addOperand(allLayers.get(1));
        // create a nested expression with the OR operator and two empty dictionaries as operands
        PdfVisibilityExpression nestedExpression = new PdfVisibilityExpression(PdfName.Or);
        nestedExpression.addOperand(allLayers.get(0));
        nestedExpression.addOperand(allLayers.get(2));
        // add another expression as the third operand
        expression.addOperand(nestedExpression);


        layerMembershipAnyOn.addLayer(allLayers.get(0));
        layerMembershipAnyOn.addLayer(allLayers.get(1));
        layerMembershipAnyOn.setVisibilityExpression(expression);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAnyOn, canvas, "visualExpressionTest01", 200, 500);

        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void disabledVisualExpressionTest01() throws IOException, InterruptedException {
        String srcPdf = "sourceWithDifferentLayers.pdf";
        String destPdf = "disabledVisualExpressionTest01.pdf";
        String cmpPdf = "cmp_" + destPdf;

        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SOURCE_FOLDER + srcPdf),
                CompareTool.createTestPdfWriter(DESTINATION_FOLDER + destPdf));

        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
        canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 18);

        List<PdfLayer> allLayers = pdfDoc.getCatalog().getOCProperties(true).getLayers();

        PdfLayerMembership layerMembershipAnyOn = new PdfLayerMembership(pdfDoc);

        // create expression with the AND operator as the first operand
        PdfVisibilityExpression expression = new PdfVisibilityExpression(PdfName.And);
        // add an empty dictionary as the second operand
        expression.addOperand(allLayers.get(1));
        // create a nested expression with the AND operator and two empty dictionaries as operands
        PdfVisibilityExpression nestedExpression = new PdfVisibilityExpression(PdfName.And);
        nestedExpression.addOperand(allLayers.get(0));
        nestedExpression.addOperand(allLayers.get(2));
        // add another expression as the third operand
        expression.addOperand(nestedExpression);


        layerMembershipAnyOn.addLayer(allLayers.get(0));
        layerMembershipAnyOn.addLayer(allLayers.get(1));
        layerMembershipAnyOn.setVisibilityExpression(expression);

        PdfLayerTestUtils.addTextInsideLayer(layerMembershipAnyOn, canvas, "visualExpressionTest01", 200, 500);

        pdfDoc.close();
        Assertions.assertNull(new CompareTool().compareByContent(DESTINATION_FOLDER + destPdf,
                SOURCE_FOLDER + cmpPdf, DESTINATION_FOLDER));
    }

    @Test
    public void layerPropertiesPersistenceTest() throws IOException {
        String filename = DESTINATION_FOLDER + "layerPropertiesPersistence.pdf";

        // Create document with layers
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(filename));
        PdfPage page = pdfDocument.addNewPage();

        // Create a layer that is ON
        PdfLayer layerOn = new PdfLayer("LayerOn", pdfDocument);
        layerOn.setOn(true);
        layerOn.setOnPanel(true);
        layerOn.setLocked(true);

        PdfLayer childLayer = new PdfLayer("ChildLayer", pdfDocument);
        childLayer.setOn(false);
        layerOn.addChild(childLayer);

        // Create a layer that is OFF
        PdfLayer layerOff = new PdfLayer("LayerOff", pdfDocument);
        layerOff.setOn(false);
        layerOff.setOnPanel(false);
        layerOff.setLocked(true);

        PdfCanvas canvas = new PdfCanvas(page);

        canvas.beginLayer(layerOn);
        canvas.setFillColor(ColorConstants.RED);
        canvas.rectangle(100, 100, 200, 200);
        canvas.fill();
        canvas.endLayer();

        canvas.beginLayer(layerOff);
        canvas.setFillColor(ColorConstants.BLUE);
        canvas.rectangle(350, 100, 200, 200);
        canvas.fill();
        canvas.endLayer();

        pdfDocument.close();

        // Reopen the document and verify layer states are persisted
        PdfDocument reopenedDoc = new PdfDocument(new PdfReader(filename), new PdfWriter(new ByteArrayOutputStream()));

        Assertions.assertEquals(2, reopenedDoc.getPage(1).getPdfLayers().size());

        PdfLayerMembership reopenedLayerMembership = new PdfLayerMembership(reopenedDoc);
        for (PdfLayer layer : reopenedDoc.getCatalog().getOCProperties(false).getLayers()) {
            reopenedLayerMembership.addLayer(layer);
        }

        // Find the layers by name and verify their states
        PdfLayer reopenedLayerOn = null;
        PdfLayer reopenedLayerOff = null;
        for (PdfLayer layer : reopenedLayerMembership.getLayers()) {
            String layerName = layer.getPdfObject().getAsString(PdfName.Name).getValue();
            if ("LayerOn".equals(layerName)) {
                reopenedLayerOn = layer;
            } else if ("LayerOff".equals(layerName)) {
                reopenedLayerOff = layer;
            }
        }

        Assertions.assertNotNull(reopenedLayerOn, "LayerOn should exist after reopening");
        Assertions.assertNotNull(reopenedLayerOff, "LayerOff should exist after reopening");

        Assertions.assertTrue(reopenedLayerOn.isOn());
        Assertions.assertTrue(reopenedLayerOn.isOnPanel());
        Assertions.assertTrue(reopenedLayerOn.isLocked());
        Assertions.assertEquals(1, reopenedLayerOn.getChildren().size());
        Assertions.assertFalse(reopenedLayerOn.getChildren().get(0).isOn());

        Assertions.assertFalse(reopenedLayerOff.isOn());
        Assertions.assertFalse(reopenedLayerOff.isOnPanel());
        Assertions.assertTrue(reopenedLayerOff.isLocked());

        reopenedDoc.close();
    }

}