// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.

using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Layers;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.Graphics;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;

namespace GcPdfWeb.Samples
    // This sample creates a multi-layer PDF document from a set of PDFs
    // each of which shows a certain part of an electrical plan of a house.
    // Each PDF is added as a separate layer. The resulting PDF provides
    // optional content that allows the user to selectively see parts of
    // the electrical wiring of a house (e.g. just the HVAC setup, or
    // just the outlets, etc.).
    public class HousePlanLayers
        public void CreatePDF(Stream stream)
            // The list of PDF names' parts identifying their semantics:
            List<string> fnames = new List<string>()
            // The common base name:
            var fbase = "how_to_read_electrical_plans_";
            // The directory containing the PDFs:
            var dir = Path.Combine("Resources", "PDFs");

            GcPdfDocument doc = null;
            Page page = null;
            GcPdfGraphics g = null;
            var disposables = new List<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 (int i = 0; i < fnames.Count; ++i)
                var iname = fnames[i];
                var idoc = new GcPdfDocument();
                var ifs = File.OpenRead(Path.Combine(dir, fbase + iname));
                if (i == 0)
                    doc = idoc;
                    page = idoc.Pages.Last;
                    g = page.Graphics;
                    doc.OptionalContent.SetLayerDefaultState(iname, false);
                    g.DrawPdfPage(idoc.Pages[0], page.Bounds);

            // Save the PDF:

            // Dispose file streams:
            disposables.ForEach(d_ => d_.Dispose());