HousePlanLayers.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
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 HousePlanLayers
    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 As GcPdfDocument = Nothing
        Dim page As Page = Nothing
        Dim g As GcPdfGraphics = Nothing
        Dim disposables = New List(Of IDisposable)()
        ' Combine all PDFs into a single document.
        ' The first PDF is used as the base,
        ' additional PDFs are added as optional content (layers):
        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)
            If i = 0 Then
                doc = idoc
                page = idoc.Pages.Last
                g = page.Graphics
            Else
                doc.OptionalContent.AddLayer(iname)
                doc.OptionalContent.SetLayerDefaultState(iname, False)
                g.BeginLayer(iname)
                g.DrawPdfPage(idoc.Pages(0), page.Bounds)
                g.EndLayer()
            End If
        Next
        ' Save the PDF:
        doc.Save(stream)

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