RemoveSquareImages.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Text;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using System.Linq;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This demo shows how to remove images that have a certain aspect ratio
  17. // (square, width same as height) from a PDF using the GcPdfDocument.RemoveImages() method.
  18. public class RemoveSquareImages
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf"));
  23. var doc= new GcPdfDocument();
  24. doc.Load(fs);
  25. // Get the list of images in the document:
  26. var imageInfos = doc.GetImages();
  27. // Remove non-square images from the list:
  28. for (int i = imageInfos.Count - 1; i >= 0; i--)
  29. {
  30. if (imageInfos[i].Image.Width != imageInfos[i].Image.Height)
  31. imageInfos.RemoveAt(i);
  32. }
  33. // Remove images left in the list from the PDF:
  34. doc.RemoveImages(imageInfos);
  35. // Done:
  36. doc.Save(stream);
  37. return doc.Pages.Count;
  38. }
  39. }
  40. }
  41.