// 完毕:
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 DsPdfWeb.Demos
{
// 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 int CreatePDF(Stream stream)
{
// The list of PDF names' parts identifying their semantics:
var fnames = new List<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:
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));
idoc.Load(ifs);
disposables.Add(ifs);
if (i == 0)
{
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();
}
}
// 保存 PDF:
doc.Save(stream);
// Dispose file streams:
disposables.ForEach(d_ => d_.Dispose());
return doc.Pages.Count;
}
}
}