RedactCopyImages.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Pdf.Annotations;
  11.  
  12. namespace DsPdfWeb.Demos
  13. {
  14. // This sample demonstrates the use of RedactOptions.CopyImagesOnRedact property.
  15. // By default, if all or part of an image falls within a redact area, and the image
  16. // appears elsewhere in the PDF, all locations are affected, i.e. the image will look
  17. // redacted throughout the document.
  18. // The RedactOptions.CopyImagesOnRedact property allows you to change this behavior.
  19. // If that property is true (it is false by default), the image is copied prior to
  20. // applying the redact, and only the copy under the redacted area is affected by the
  21. // redact.
  22. // This sample loads a two page PDF with the same image appearing three times
  23. // on the first, and three times on the second page.
  24. // Two redact annotations are then added to the document, both cover different parts
  25. // of the first image location on the first page.
  26. // The first redact uses red overlay fill color, and (by default) affects the image
  27. // in all locations, note the red rectangle in the top left part of the image
  28. // in all locations throughout the document.
  29. // The second redact uses blue overlay, and sets CopyImagesOnRedact to true, so
  30. // that only the instance of the image under the actual redact area is affected,
  31. // note the blue rectangle in the lower right part of the first image only.
  32. // The PDF used by this sample was generated by ImageTransparency.
  33. public class RedactCopyImages
  34. {
  35. public int CreatePDF(Stream stream)
  36. {
  37. var doc = new GcPdfDocument();
  38. // Load a PDF that contains the same image in several locations:
  39. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "ImageTransparency.pdf"));
  40. doc.Load(fs);
  41. // Duplicate the first page:
  42. doc.Pages.ClonePage(0, 1);
  43.  
  44. // This redact will affect all image locations:
  45. var redact0 = new RedactAnnotation()
  46. {
  47. Page = doc.Pages[0],
  48. Rect = new RectangleF(50, 50, 50, 50),
  49. OverlayFillColor = Color.Red,
  50. };
  51. // This redact will affect only the image at the actual redact location:
  52. var redact1 = new RedactAnnotation()
  53. {
  54. Page = doc.Pages[0],
  55. Rect = new RectangleF(120, 120, 50, 50),
  56. OverlayFillColor = Color.Blue,
  57. };
  58.  
  59. // Apply redact0 with default options:
  60. doc.Redact(redact0);
  61. // Apply redact1 with CopyImagesOnRedact on:
  62. doc.Redact(redact1, new RedactOptions() { CopyImagesOnRedact = true });
  63.  
  64. // Save the PDF:
  65. doc.Save(stream);
  66. return doc.Pages.Count;
  67. }
  68. }
  69. }
  70.