Thursday, 30 April 2020

Add and Delete Layers in PDF in Java


PDF layer is a feature which allows some content to be made visible or invisible in the PDF. Here I’ll use Java code to show you how to manipulate PDF layers (create and delete layers).

Before getting started, please download the latest version of Free Spire.PDF for Java pack, unzip it and import Spire.Pdf.jar in the “lib” folder to your project as a dependency.

If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.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</artifactId>
        <version>2.6.3</version>
    </dependency>
</dependencies>

Example 1 Add Layers
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();
    }
}
 

Example 2 Delete Layers
import com.spire.pdf.PdfDocument;


public class DeleteLayer {

    public static void main(String[] args) {

        //load a PDF document

        PdfDocument pdf = new PdfDocument();

        pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\addLayers.pdf");

        //remove a layer by name

        pdf.getLayers().removeLayer("red line1");

        //save the resultant document

        pdf.saveToFile("output/deleteLayer.pdf");

        pdf.close();

    }

}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...