Skip to main content
Skip table of contents

Changing OCGs

Examples in response to questions such as:


addocg

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.layer.PdfLayer;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.properties.TextAlignment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AddOCG {
    public static final String DEST = "./target/sandbox/stamper/add_ocg.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddOCG().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());

        PdfLayer nested = new PdfLayer("Nested layers", pdfDoc);
        PdfLayer nested1 = new PdfLayer("Nested layer 1", pdfDoc);
        PdfLayer nested2 = new PdfLayer("Nested layer 2", pdfDoc);
        nested2.setLocked(true);
        nested.addChild(nested1);
        nested.addChild(nested2);

        canvas.beginLayer(nested);
        Canvas canvasModel = new Canvas(canvas, pdfDoc.getDefaultPageSize());
        canvasModel.showTextAligned("nested layers", 50, 765, TextAlignment.LEFT, 0);
        canvas.endLayer();

        canvas.beginLayer(nested1);
        canvasModel.showTextAligned("nested layers 1", 100, 800, TextAlignment.LEFT, 0);
        canvas.endLayer();

        canvas.beginLayer(nested2);
        canvasModel.showTextAligned("nested layers 2", 100, 750, TextAlignment.LEFT, 0);
        canvas.endLayer();

        PdfLayer group = PdfLayer.createTitle("Grouped layers", pdfDoc);
        PdfLayer layer1 = new PdfLayer("Group: layer 1", pdfDoc);
        PdfLayer layer2 = new PdfLayer("Group: layer 2", pdfDoc);
        group.addChild(layer1);
        group.addChild(layer2);

        canvas.beginLayer(layer1);
        canvasModel.showTextAligned("layer 1 in the group", 50, 700, TextAlignment.LEFT, 0);
        canvas.endLayer();

        canvas.beginLayer(layer2);
        canvasModel.showTextAligned("layer 2 in the group", 50, 675, TextAlignment.LEFT, 0);
        canvas.endLayer();

        PdfLayer radiogroup = PdfLayer.createTitle("Radio group", pdfDoc);
        PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", pdfDoc);
        radio1.setOn(true);
        PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", pdfDoc);
        radio2.setOn(false);
        PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", pdfDoc);
        radio3.setOn(false);
        radiogroup.addChild(radio1);
        radiogroup.addChild(radio2);
        radiogroup.addChild(radio3);
        List<PdfLayer> options = new ArrayList<>();
        options.add(radio1);
        options.add(radio2);
        options.add(radio3);
        PdfLayer.addOCGRadioGroup(pdfDoc, options);

        canvas.beginLayer(radio1);
        canvasModel.showTextAligned("option 1", 50, 600, TextAlignment.LEFT, 0);
        canvas.endLayer();

        canvas.beginLayer(radio2);
        canvasModel.showTextAligned("option 2", 50, 575, TextAlignment.LEFT, 0);
        canvas.endLayer();

        canvas.beginLayer(radio3);
        canvasModel.showTextAligned("option 3", 50, 550, TextAlignment.LEFT, 0);
        canvas.endLayer();

        PdfLayer not_printed = new PdfLayer("not printed", pdfDoc);
        not_printed.setOnPanel(false);
        not_printed.setPrint("Print", false);

        canvas.beginLayer(not_printed);
        canvasModel.showTextAligned("PRINT THIS PAGE", 300, 700,
                TextAlignment.CENTER, (float) Math.toRadians(90));
        canvas.endLayer();

        PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", pdfDoc);
        zoom.setOnPanel(false);
        zoom.setZoom(0.75f, 1.25f);

        canvas.beginLayer(zoom);
        canvasModel
                .showTextAligned("Only visible if the zoomfactor is between 75 and 125%",
                        30, 530, TextAlignment.LEFT, (float) Math.toRadians(90));
        canvas.endLayer();

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.IO.Util;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Layer;
using iText.Layout;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddOCG 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_ocg.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddOCG().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage());
            
            PdfLayer nested = new PdfLayer("Nested layers", pdfDoc);
            PdfLayer nested1 = new PdfLayer("Nested layer 1", pdfDoc);
            PdfLayer nested2 = new PdfLayer("Nested layer 2", pdfDoc);
            nested2.SetLocked(true);
            nested.AddChild(nested1);
            nested.AddChild(nested2);
            
            canvas.BeginLayer(nested);
            Canvas canvasModel = new Canvas(canvas, pdfDoc.GetDefaultPageSize());
            canvasModel.ShowTextAligned("nested layers", 50, 765, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            canvas.BeginLayer(nested1);
            canvasModel.ShowTextAligned("nested layers 1", 100, 800, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            canvas.BeginLayer(nested2);
            canvasModel.ShowTextAligned("nested layers 2", 100, 750, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            PdfLayer group = PdfLayer.CreateTitle("Grouped layers", pdfDoc);
            PdfLayer layer1 = new PdfLayer("Group: layer 1", pdfDoc);
            PdfLayer layer2 = new PdfLayer("Group: layer 2", pdfDoc);
            group.AddChild(layer1);
            group.AddChild(layer2);
            
            canvas.BeginLayer(layer1);
            canvasModel.ShowTextAligned("layer 1 in the group", 50, 700, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            canvas.BeginLayer(layer2);
            canvasModel.ShowTextAligned("layer 2 in the group", 50, 675, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            PdfLayer radiogroup = PdfLayer.CreateTitle("Radio group", pdfDoc);
            PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", pdfDoc);
            radio1.SetOn(true);
            PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", pdfDoc);
            radio2.SetOn(false);
            PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", pdfDoc);
            radio3.SetOn(false);
            radiogroup.AddChild(radio1);
            radiogroup.AddChild(radio2);
            radiogroup.AddChild(radio3);
            IList<PdfLayer> options = new List<PdfLayer>();
            options.Add(radio1);
            options.Add(radio2);
            options.Add(radio3);
            PdfLayer.AddOCGRadioGroup(pdfDoc, options);
            
            canvas.BeginLayer(radio1);
            canvasModel.ShowTextAligned("option 1", 50, 600, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            canvas.BeginLayer(radio2);
            canvasModel.ShowTextAligned("option 2", 50, 575, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            canvas.BeginLayer(radio3);
            canvasModel.ShowTextAligned("option 3", 50, 550, TextAlignment.LEFT, 0);
            canvas.EndLayer();
            
            PdfLayer not_printed = new PdfLayer("not printed", pdfDoc);
            not_printed.SetOnPanel(false);
            not_printed.SetPrint("Print", false);
            
            canvas.BeginLayer(not_printed);
            canvasModel.ShowTextAligned("PRINT THIS PAGE", 300, 700, TextAlignment.CENTER, 
                    (float)MathUtil.ToRadians(90));
            canvas.EndLayer();
            
            PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", pdfDoc);
            zoom.SetOnPanel(false);
            zoom.SetZoom(0.75f, 1.25f);
            
            canvas.BeginLayer(zoom);
            canvasModel.ShowTextAligned("Only visible if the zoomfactor is between 75 and 125%", 
                    30, 530, TextAlignment.LEFT, (float)MathUtil.ToRadians(90));
            canvas.EndLayer();
            
            pdfDoc.Close();
        }
    }
}


changeocg

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.layer.PdfLayer;

import java.io.File;
import java.util.List;

public class ChangeOCG {
    public static final String DEST = "./target/sandbox/stamper/change_ocg.pdf";
    public static final String SRC = "./src/main/resources/pdfs/ocg.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ChangeOCG().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        List<PdfLayer> layers = pdfDoc.getCatalog().getOCProperties(true).getLayers();
        for (PdfLayer layer : layers) {
            if ("Nested layer 1".equals(layer.getPdfObject().get(PdfName.Name).toString())) {
                layer.setOn(false);
                break;
            }
        }

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Layer;

namespace iText.Samples.Sandbox.Stamper 
{
    public class ChangeOCG {
        public static readonly String DEST = "results/sandbox/stamper/change_ocg.pdf";
        public static readonly String SRC = "../../../resources/pdfs/ocg.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new ChangeOCG().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            IList<PdfLayer> layers = pdfDoc.GetCatalog().GetOCProperties(true).GetLayers();
            foreach (PdfLayer layer in layers) 
            {
                if ("Nested layer 1".Equals(layer.GetPdfObject().Get(PdfName.Name).ToString())) 
                {
                    layer.SetOn(false);
                    break;
                }
            }
            
            pdfDoc.Close();
        }
    }
}


changeocgorder

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfCatalog;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfObject;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.layer.PdfOCProperties;

import java.io.File;

public class ChangeOCGOrder {
    public static final String DEST = "./target/sandbox/stamper/change_ocg_order.pdf";
    public static final String SRC = "./src/main/resources/pdfs/ocg.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ChangeOCGOrder().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        PdfCatalog catalog = pdfDoc.getCatalog();
        PdfOCProperties ocProps = catalog.getOCProperties(true);
        PdfDictionary occd = (PdfDictionary) ocProps.getPdfObject().get(PdfName.D);
        PdfArray order = occd.getAsArray(PdfName.Order);

        PdfObject nestedLayers = order.get(0);
        PdfObject nestedLayerArray = order.get(1);
        PdfObject groupedLayers = order.get(2);
        PdfObject radioGroup = order.get(3);
        order.set(0, radioGroup);
        order.set(1, nestedLayers);
        order.set(2, nestedLayerArray);
        order.set(3, groupedLayers);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Layer;

namespace iText.Samples.Sandbox.Stamper 
{
    public class ChangeOCGOrder 
    {
        public static readonly String DEST = "results/sandbox/stamper/change_ocg_order.pdf";
        public static readonly String SRC = "../../../resources/pdfs/ocg.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new ChangeOCGOrder().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            PdfCatalog catalog = pdfDoc.GetCatalog();
            PdfOCProperties ocProps = catalog.GetOCProperties(true);
            PdfDictionary occd = (PdfDictionary)ocProps.GetPdfObject().Get(PdfName.D);
            PdfArray order = occd.GetAsArray(PdfName.Order);
            
            PdfObject nestedLayers = order.Get(0);
            PdfObject nestedLayerArray = order.Get(1);
            PdfObject groupedLayers = order.Get(2);
            PdfObject radioGroup = order.Get(3);
            order.Set(0, radioGroup);
            order.Set(1, nestedLayers);
            order.Set(2, nestedLayerArray);
            order.Set(3, groupedLayers);
            
            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.