HousePlanLayers.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5.  
  6. using System;
  7. using System.IO;
  8. using System.Drawing;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Pdf.Layers;
  12. using GrapeCity.Documents.Pdf.Annotations;
  13. using GrapeCity.Documents.Pdf.Graphics;
  14. using GrapeCity.Documents.Text;
  15. using GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // This sample creates a multi-layer PDF document from a set of PDFs
  20. // each of which shows a certain part of an electrical plan of a house.
  21. // Each PDF is added as a separate layer. The resulting PDF provides
  22. // optional content that allows the user to selectively see parts of
  23. // the electrical wiring of a house (e.g. just the HVAC setup, or
  24. // just the outlets, etc.).
  25. public class HousePlanLayers
  26. {
  27. public int CreatePDF(Stream stream)
  28. {
  29. // The list of PDF names' parts identifying their semantics:
  30. var fnames = new List<string>()
  31. {
  32. "full_electrical_plan.pdf",
  33. "all_outlets.pdf",
  34. "data_plan_and_detectors.pdf",
  35. "HVAC_with_wiring.pdf",
  36. "lighting_plan.pdf",
  37. "lighting_plan_with_wiring.pdf",
  38. "security_system_plan.pdf",
  39. };
  40. // The common base name:
  41. var fbase = "how_to_read_electrical_plans_";
  42. // The directory containing the PDFs:
  43. var dir = Path.Combine("Resources", "PDFs");
  44.  
  45. GcPdfDocument doc = null;
  46. Page page = null;
  47. GcPdfGraphics g = null;
  48. var disposables = new List<IDisposable>();
  49. // Combine all PDFs into a single document.
  50. // The first PDF is used as the base,
  51. // additional PDFs are added as optional content (layers):
  52. for (int i = 0; i < fnames.Count; ++i)
  53. {
  54. var iname = fnames[i];
  55. var idoc = new GcPdfDocument();
  56. var ifs = File.OpenRead(Path.Combine(dir, fbase + iname));
  57. idoc.Load(ifs);
  58. disposables.Add(ifs);
  59. if (i == 0)
  60. {
  61. doc = idoc;
  62. page = idoc.Pages.Last;
  63. g = page.Graphics;
  64. }
  65. else
  66. {
  67. doc.OptionalContent.AddLayer(iname);
  68. doc.OptionalContent.SetLayerDefaultState(iname, false);
  69. g.BeginLayer(iname);
  70. g.DrawPdfPage(idoc.Pages[0], page.Bounds);
  71. g.EndLayer();
  72. }
  73. }
  74. // Save the PDF:
  75. doc.Save(stream);
  76.  
  77. // Dispose file streams:
  78. disposables.ForEach(d_ => d_.Dispose());
  79. return doc.Pages.Count;
  80. }
  81. }
  82. }
  83.