【Java】Add/ Delete Layers in PDF
Published: 2020-12-28 07:32:03

PDF layers allow some content in the PDF to be selectively viewed or hidden. They are commonly used for items such as CAD drawings, layered artwork, maps, etc. This article will show you how to add and delete Layers in a PDF document by using Free Spire.PDF for Java. With this 3rd party free API, you can also add many different types of layers such as line, image, string, rectangle to PDF document.

**Installation**
**Method 1:** Download the [Free Spire.PDF for Java](https://www.e-iceblue.com/Download/pdf-for-java-free.html) and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.
```xml
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
```

**Add Layers:**
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfBrushes;
import com.spire.pdf.graphics.PdfCanvas;
import com.spire.pdf.graphics.PdfPen;
import com.spire.pdf.graphics.layer.PdfLayer;

import java.awt.geom.Point2D;

public class AddLayers {
    public static void main(String[] args){
        //Instantiate a PdfDocument object
        PdfDocument pdf = new PdfDocument();
        //Add a page
        PdfPageBase page = pdf.getPages().add();

        //Add 3 layers to the page
        PdfLayer layer = pdf.getLayers().addLayer("red line1");
        PdfCanvas canvas1 = layer.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas1.drawLine(new PdfPen(PdfBrushes.getRed(), 1), new Point2D.Float(50, 350), new Point2D.Float(200, 350));
        layer = pdf.getLayers().addLayer("blue line1");
        PdfCanvas canvas2 = layer.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas2.drawLine(new PdfPen(PdfBrushes.getBlue(), 1), new Point2D.Float(50, 450), new Point2D.Float(200, 450));
        layer = pdf.getLayers().addLayer("green line1");
        PdfCanvas canvas3 = layer.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas3.drawLine(new PdfPen(PdfBrushes.getGreen(), 1), new Point2D.Float(50,550), new Point2D.Float(200, 550));

        //Save the resultant document
        pdf.saveToFile("output/addLayers.pdf");
        pdf.close();
    }
}
```
![](https://dev-to-uploads.s3.amazonaws.com/i/qdlgej07jww2swletj4c.jpg)

**Delete Layers:**
```java
import com.spire.pdf.PdfDocument;

public class DeleteLayers {
    public static void main(String[] args){
        //Load a PDF document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("output/addLayers.pdf");

        //Remove a layer by name
        pdf.getLayers().removeLayer("red line1");

        //Save the resultant document
        pdf.saveToFile("output/deleteLayer.pdf");
        pdf.close();
    }
}
```
![](https://dev-to-uploads.s3.amazonaws.com/i/ikz44lo3xto8gk3vfwjs.jpg)