RemoveLayers.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 example shows how to remove layers and their content
  20. // from a PDF with multiple layers.
  21. // The input PDF for this sample was created by HousePlanAllLayers.
  22. public class RemoveLayers
  23. {
  24. public int CreatePDF(Stream stream)
  25. {
  26. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "house-plan-all-layers.pdf"));
  27. var doc = new GcPdfDocument();
  28. doc.Load(fs);
  29.  
  30. // Remove all layers except the last one with their content:
  31. var layers = doc.OptionalContent.Groups.Take(doc.OptionalContent.Groups.Count - 1).ToArray();
  32. doc.OptionalContent.RemoveLayers(true, layers);
  33. // Remove the single remaining layer, leaving its content in place:
  34. doc.OptionalContent.RemoveLayer(doc.OptionalContent.Groups[0]);
  35.  
  36. // Save the PDF:
  37. doc.Save(stream);
  38. return doc.Pages.Count;
  39. }
  40. }
  41. }
  42.