HousePlanAllLayers.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.Linq;
  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. // Note that this example is similar to HousePlanLayers, but unlike
  26. // that sample, here ALL content is added as layers (in HousePlanLayers,
  27. // the full electrical plan content does not belong to any layer).
  28. public class HousePlanAllLayers
  29. {
  30. public int CreatePDF(Stream stream)
  31. {
  32. // The list of PDF names' parts identifying their semantics:
  33. var fnames = new List<string>()
  34. {
  35. "full_electrical_plan.pdf",
  36. "all_outlets.pdf",
  37. "data_plan_and_detectors.pdf",
  38. "HVAC_with_wiring.pdf",
  39. "lighting_plan.pdf",
  40. "lighting_plan_with_wiring.pdf",
  41. "security_system_plan.pdf",
  42. };
  43. // The common base name:
  44. var fbase = "how_to_read_electrical_plans_";
  45. // The directory containing the PDFs:
  46. var dir = Path.Combine("Resources", "PDFs");
  47.  
  48. var doc = new GcPdfDocument();
  49. var page = doc.Pages.Add();
  50. var g = page.Graphics;
  51. var disposables = new List<IDisposable>();
  52. // Combine all PDFs into a single document as layers on the first page:
  53. for (int i = 0; i < fnames.Count; ++i)
  54. {
  55. var iname = fnames[i];
  56. var idoc = new GcPdfDocument();
  57. var ifs = File.OpenRead(Path.Combine(dir, fbase + iname));
  58. idoc.Load(ifs);
  59. disposables.Add(ifs);
  60. doc.OptionalContent.AddLayer(iname);
  61. doc.OptionalContent.SetLayerDefaultState(iname, false);
  62. g.BeginLayer(iname);
  63. g.DrawPdfPage(idoc.Pages[0], page.Bounds);
  64. g.EndLayer();
  65. }
  66. // Make the last layer visible by default:
  67. doc.OptionalContent.SetLayerDefaultState(fnames.Last(), true);
  68.  
  69. // Save the PDF:
  70. doc.Save(stream);
  71.  
  72. // Dispose file streams:
  73. disposables.ForEach(d_ => d_.Dispose());
  74. return doc.Pages.Count;
  75. }
  76. }
  77. }
  78.