IText edit or remove the layer on PDF

As it turned out in comments, the layers in question are what iText indeed calls layers but what actually in PDF lingo is called optional content groups.

There indeed is a utility class for removing such layers in the iText Xtra package (not the extrajars, but itext-xtra.jar): com.itextpdf.text.pdf.ocg.OCGRemover which makes use of the class OCGParser in the same package.

/**
 * Class that knows how to remove OCG layers.
 */
public class OCGRemover
{
    /**
     * Removes layers from a PDF document
     * @param reader    a PdfReader containing a PDF document
     * @param layers    a sequence of names of OCG layers
     * @throws IOException
     */
    public void removeLayers(PdfReader reader, String... layers) throws IOException
    [...]
}

After applying that method to a PdfReader you obviously have to save the changes, e.g. by means of a PdfStamper.

Leave a Comment