HousePlanAllLayers.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 HousePlanAllLayers
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        ' The list of PDF names' parts identifying their semantics:
        Dim fnames As String() = {
            "full_electrical_plan.pdf",
            "all_outlets.pdf",
            "data_plan_and_detectors.pdf",
            "HVAC_with_wiring.pdf",
            "lighting_plan.pdf",
            "lighting_plan_with_wiring.pdf",
            "security_system_plan.pdf"
        }
        ' The common base name:
        Dim fbase = "how_to_read_electrical_plans_"
        ' The directory containing the PDFs:
        Dim dir = Path.Combine("Resources", "PDFs")

        Dim doc = New GcPdfDocument()
        Dim page = doc.Pages.Add()
        Dim g = page.Graphics
        Dim disposables = New List(Of IDisposable)()
        ' Combine all PDFs into a single document as layers on the first page:
        For i = 0 To fnames.Length - 1
            Dim iname = fnames(i)
            Dim idoc = New GcPdfDocument()
            Dim ifs = File.OpenRead(Path.Combine(dir, fbase & iname))
            idoc.Load(ifs)
            disposables.Add(ifs)
            doc.OptionalContent.AddLayer(iname)
            doc.OptionalContent.SetLayerDefaultState(iname, False)
            g.BeginLayer(iname)
            g.DrawPdfPage(idoc.Pages(0), page.Bounds)
            g.EndLayer()
        Next
        ' Make the last layer visible by default:
        doc.OptionalContent.SetLayerDefaultState(fnames.Last(), True)

        ' Save the PDF:
        doc.Save(stream)

        ' Dispose file streams:
        disposables.ForEach(Sub(d_) d_.Dispose())
        Return doc.Pages.Count
    End Function
End Class