//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.Annotations;
namespace DsPdfWeb.Demos
{
// This sample demonstrates the use of RedactOptions.CopyImagesOnRedact property.
// By default, if all or part of an image falls within a redact area, and the image
// appears elsewhere in the PDF, all locations are affected, i.e. the image will look
// redacted throughout the document.
// The RedactOptions.CopyImagesOnRedact property allows you to change this behavior.
// If that property is true (it is false by default), the image is copied prior to
// applying the redact, and only the copy under the redacted area is affected by the
// redact.
// This sample loads a two page PDF with the same image appearing three times
// on the first, and three times on the second page.
// Two redact annotations are then added to the document, both cover different parts
// of the first image location on the first page.
// The first redact uses red overlay fill color, and (by default) affects the image
// in all locations, note the red rectangle in the top left part of the image
// in all locations throughout the document.
// The second redact uses blue overlay, and sets CopyImagesOnRedact to true, so
// that only the instance of the image under the actual redact area is affected,
// note the blue rectangle in the lower right part of the first image only.
// The PDF used by this sample was generated by ImageTransparency.
public class RedactCopyImages
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
// Load a PDF that contains the same image in several locations:
using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "ImageTransparency.pdf"));
doc.Load(fs);
// Duplicate the first page:
doc.Pages.ClonePage(0, 1);
// This redact will affect all image locations:
var redact0 = new RedactAnnotation()
{
Page = doc.Pages[0],
Rect = new RectangleF(50, 50, 50, 50),
OverlayFillColor = Color.Red,
};
// This redact will affect only the image at the actual redact location:
var redact1 = new RedactAnnotation()
{
Page = doc.Pages[0],
Rect = new RectangleF(120, 120, 50, 50),
OverlayFillColor = Color.Blue,
};
// Apply redact0 with default options:
doc.Redact(redact0);
// Apply redact1 with CopyImagesOnRedact on:
doc.Redact(redact1, new RedactOptions() { CopyImagesOnRedact = true });
// Save the PDF:
doc.Save(stream);
return doc.Pages.Count;
}
}
}