RemoveLayers.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Linq
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Layers
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Graphics
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

Public Class RemoveLayers
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "house-plan-all-layers.pdf"))
            Dim doc = New GcPdfDocument()
            doc.Load(fs)

            ' Remove all layers except the last one with their content:
            Dim layers = doc.OptionalContent.Groups.Take(doc.OptionalContent.Groups.Count - 1).ToArray()
            doc.OptionalContent.RemoveLayers(True, layers)
            ' Remove the single remaining layer, leaving its content in place:
            doc.OptionalContent.RemoveLayer(doc.OptionalContent.Groups(0))

            ' Save the PDF:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class