How to add layer on existing page?

        var document = new Document("somatosensory.pdf");
        var page = document.Pages[1];

        var layer = new Layer("oc1", "green line");
        layer.Contents.Add(new Aspose.Pdf.Operators.SetRGBColorStroke(0, 1, 0));
        layer.Contents.Add(new Aspose.Pdf.Operators.MoveTo(500, 750));
        layer.Contents.Add(new Aspose.Pdf.Operators.LineTo(400, 750));
        layer.Contents.Add(new Aspose.Pdf.Operators.Stroke());
        page.Layers.Add(layer);

        document.Save("somatosensory-updated.pdf");

i don’t see the green line on the first page of the PDF. help appreciated

somatosensory.pdf (141.9 KB)

somatosensory-updated.pdf (142.0 KB)

@compagniedsdr
It seems that in your case you are trying to add layer out of bounds of Page’s media box which in your case is limited to 567x702
If you change you code to something like this:

var layer = new Layer("oc1", "green line");
layer.Contents.Add(new Aspose.Pdf.Operators.SetRGBColorStroke(0, 1, 0));
layer.Contents.Add(new Aspose.Pdf.Operators.MoveTo(500, 600));
layer.Contents.Add(new Aspose.Pdf.Operators.LineTo(400, 700));
layer.Contents.Add(new Aspose.Pdf.Operators.Stroke());
page.Layers.Add(layer);

you will get the following result where green line can be seen on top of page 1
layer_add_issue.pdf (142.1 KB)

thank you. when i open a PDF file, why is the layer count 0? does this mean every page starts with no layers? how do i make a page into a layer?

@compagniedsdr
Yes, that’s correct
In PDF page and layer are different entities
You can consider layer as some sort of element grouping tool
Currently I found no such functionality as direct conversion of page contents into layer
You can clone page contents from page into layer like in following code:

var doc1 = new Document(input);
var page1 = doc1.Pages[1];

var layer = new Layer("oc1", "green line");
foreach (var item in page1.Contents)
{
	layer.Contents.Add(item);
}
page1.Layers.Add(layer);
doc1.Save(output);

I tried to do that and then this is the file I got
somatosensory-layered.pdf (41.8 KB)

The text is not cloned properly

@compagniedsdr
I tested in source document and it works normally there
however it breaks if you create new document and try to transfer content there
issue seems tied to fonts that are not present in new version of document
I’ll try to contact developers for explanation how to solve this scenario, if I’'ll get no anwser today then I’ll add new ticket for this issue

@compagniedsdr
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56995

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.