RemoveImageLocation.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 some instances (locations) of an image
  17. // from a PDF using the GcPdfDocument.RemoveImages() method.
  18. public class RemoveImageLocation
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "ImageTransparency.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. // For each image with multiple locations, remove the leftmost location:
  28. foreach (var ii in imageInfos)
  29. {
  30. if (ii.Locations.Count > 1)
  31. {
  32. int iLeft = -1;
  33. for (int i = 0; i < ii.Locations.Count; i++)
  34. {
  35. if (iLeft == -1 || ii.Locations[i].PageBounds.ToRect().Left < ii.Locations[iLeft].PageBounds.ToRect().Left)
  36. iLeft = i;
  37. }
  38. // Remove the locations we want to keep:
  39. for (int i = ii.Locations.Count - 1; i >= 0; i--)
  40. if (i != iLeft)
  41. ii.Locations.RemoveAt(i);
  42. // Remove the locations that are left:
  43. doc.RemoveImages([ii]);
  44. }
  45. }
  46. // Done:
  47. doc.Save(stream);
  48. return doc.Pages.Count;
  49. }
  50. }
  51. }
  52.