Java add, hide / show, delete PDF layers

This paper introduces how to operate PDF layer. It can be divided into adding layers (including adding lines, shapes, strings, pictures, etc.), hiding or displaying layers, deleting layers, etc. Refer to the following Java code examples for details.

Tools: free flame.pdf for Java (free version)

Jar package download and import: available through Download on official website , after downloading, unzip and import Sprie.Pdf.jar under the lib folder into the Java program; or Maven warehouse download and import . The import effect is as follows:

 

Java example 1: adding layers

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.graphics.PdfPen;
import com.spire.pdf.graphics.layer.PdfLayer;

import java.awt.*;
import java.awt.geom.Point2D;

public class Addlayer {
    public static void main(String[]args){
        //Create a new one PDF Document and add a page
        PdfDocument pdf = new PdfDocument();
        pdf.getPages().add();

        //Add layer 1 as line
        PdfLayer layer1 = pdf.getLayers().addLayer("line");//Add layer and specify layer name
        PdfCanvas canvas1 = layer1.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas1.drawLine(new PdfPen(PdfBrushes.getRed(), 2.5), new Point2D.Float(0, 0), new Point2D.Float(150, 0));//Draw the line layer (specify the coordinates of the first and last two points of the line, and apply the brush)

        //Create layer 2 as shape
        PdfLayer layer2 = pdf.getLayers().addLayer("Ellipse");//Add layer and specify layer name
        PdfCanvas canvas2 = layer2.createGraphics(pdf.getPages().get(0).getCanvas());
        PdfPen pen = new PdfPen( new PdfRGBColor(Color.black),2.5f);//Create a brush (specify brush color, thickness)
        PdfBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.PINK));//Create brush 1 (set brush color to pink)
        canvas2.drawEllipse(pen, brush1, 65, 50, 80, 80);//Draw the shape layer (draw the circle to the specified page position, and apply the brush, brush)

        //Create layer 3 as a text string
        PdfLayer layer3 = pdf.getLayers().addLayer("character string");//Add layer and specify layer name
        PdfCanvas canvas3 = layer3.createGraphics(pdf.getPages().get(0).getCanvas());
        PdfTrueTypeFont font1= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,14),true);//Create font
        PdfBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));//Create format brush 2 (set brush color to blue)
        canvas3.drawString("String String String", font1, brush2, 100, 150);//Draw string layer (specify string to specified page location, and apply font and brush)

        //Create layer 4 as picture
        PdfLayer layer4 = pdf.getLayers().addLayer("picture");//Add layer and specify layer name
        PdfCanvas canvas4 = layer4.createGraphics(pdf.getPages().get(0).getCanvas());
        canvas4.drawImage(PdfImage.fromFile("tp.png"), 30, 190, 150, 135);//Draw picture layer (load picture, draw to specified page position and specify picture size)

        //Save document
        pdf.saveToFile("Addlayers.pdf",FileFormat.PDF);
        pdf.dispose();
    }

}

Layer adding effect:

 

Java example 2: hide / show layers

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.layer.PdfVisibility;


public class HideOrShowLayer {
    public static void main(String[] args) {
        //Load document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Addlayers.pdf");

        pdf.getLayers().get(0).setVisibility(PdfVisibility.Off);//Set layer hide
        //pdf.getLayers().get(2).setVisibility(PdfVisibility.On);//Set layer display

        //Save document
        pdf.saveToFile("HideOrShowLayer.pdf", FileFormat.PDF);
        pdf.dispose();
    }
}

Layer hide / show effect:

Java example 3: deleting layers

import com.spire.pdf.*;

public class RemoveLayer {
    public static void main(String[] args) {
        //Load document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Addlayers.pdf");

        //Delete layer based on layer name key
        pdf.getLayers().removeLayer("character string");

        //Save document
        pdf.saveToFile("RemoveLayer.pdf",FileFormat.PDF);
        pdf.dispose();
    }
}

Layer delete effect:

 

(end)

Keywords: Java Maven

Added by Allenport on Tue, 28 Apr 2020 17:47:27 +0300