We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Get layers from pdf files in Jave - Page.getLayers always return null

PDF_Layers.pdf (37.1 KB)

Aspose Team,
We’re trying to get layers in pages of PDF files with the Page.getLayers method, but the returned result is always null even though there are layers in the page. The version of Aspose PDF java is 20.6. Following is the sample code. First a document is created and layers are added to the first page. Page.getLayers can get the layers information. Then the document is saved and reopened. Page.getLayers cannot get the layers information. PDF studio shows that the saved file has layers. Attached is the saved pdf file.

import com.aspose.pdf.*;
import java.util.ArrayList;
import java.util.List;

public class CheckPDFLayers {
public static void main(String[] args) {
try {
String pdfFile = “/home/ubuntu/winshare/Sample_Files/PDF_Test/PDF_Layers.pdf”;

        // Create a document and add layers
        Document doc = new Document();
        Page page = doc.getPages().add();
        Layer layer = new Layer("oc1", "Red Line");
        layer.getContents().add(new com.aspose.pdf.operators.SetRGBColorStroke(1, 0, 0));
        layer.getContents().add(new com.aspose.pdf.operators.MoveTo(500, 700));
        layer.getContents().add(new com.aspose.pdf.operators.LineTo(400, 700));
        layer.getContents().add(new com.aspose.pdf.operators.Stroke());
        page.setLayers(new ArrayList());
        layer = new Layer("oc2", "Green Line");
        layer.getContents().add(new com.aspose.pdf.operators.SetRGBColorStroke(0, 1, 0));
        layer.getContents().add(new com.aspose.pdf.operators.MoveTo(500, 750));
        layer.getContents().add(new com.aspose.pdf.operators.LineTo(400, 750));
        layer.getContents().add(new com.aspose.pdf.operators.Stroke());
        layer = new Layer("oc3", "Blue Line");
        layer.getContents().add(new com.aspose.pdf.operators.SetRGBColorStroke(0, 0, 1));
        layer.getContents().add(new com.aspose.pdf.operators.MoveTo(500, 800));
        layer.getContents().add(new com.aspose.pdf.operators.LineTo(400, 800));
        layer.getContents().add(new com.aspose.pdf.operators.Stroke());

        // Check layers before saving.

        // Save the file.

        // Open the saved file and check layers.
        Document pdfDocument = new Document(pdfFile);
    catch(Exception ex) {

private static void checkLayers(Document document) {
    for(Page page : document.getPages()) {
        List<Layer> layers = page.getLayers();
        if (layers != null) {
            System.out.println("Page number " + page.getNumber() + " has " + layers.size() + " layers");
            for (com.aspose.pdf.Layer layer : layers) {
        else {
            System.out.println("Page number " + page.getNumber() + " does not contain any layer");



We were able to reproduce the issue in our environment and logged it as PDFJAVA-39570 in our issue management system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

I tried in 20.11 but the problem is still there.
How can I solve the problem ?
How can I get the status of PDFJAVA-39570 ?


You are still seeing the issue because it has not been yet resolved. Please note that we will update you within this forum thread as soon as the logged ticket is fixed. Please give us some time.

We apologize for your inconvenience.

The issues you have found earlier (filed as PDFJAVA-39570) have been fixed in Aspose.PDF for Java 22.1.