RedactCopyImages.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf.Annotations

'' 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 Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        '' Load a PDF that contains the same image in several locations:
        Using 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:
            Dim redact0 = New RedactAnnotation() With {
                .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:
            Dim redact1 = New RedactAnnotation() With {
                .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() With {.CopyImagesOnRedact = True})

            '' Done:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class