'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations

'' This sample shows how to use the Page.AdjustCoordinates() method
'' to convert visual coordinates (measured from the top left corner
'' of the page according to GcPdf rules) to correct coordinates in a PDF
'' that was loaded into GcPdfDocument And may have arbitrary unknown 
'' transformations applied to its pages. The adjusted coordinates can be
'' used to position annotations (redact in this case) etc.
'' The PDF used in this sample has one page containing the scan of a sample invoice
'' that was rotated 90 degrees clockwise. The PDF page Is rotated 270 degrees to
'' compensate (so that visually the page has correct portrait orientation).
Public Class AdjustCoords
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "invoice-scan-rot270.pdf"))

            If doc.Pages.Count <> 1 Then
                Throw New Exception("Unexpected: sample invoice should have exactly one page.")
            End If

            Dim page = doc.Pages(0)

            '' The bounding rectangle of the area to redact,
            '' measured from the top left corner of the PDF page.
            '' If this rectangle Is used as Is, it will miss the target content:
            Dim rectToRedact = New RectangleF(20, 170, 200, 40)

            '' The adjusted bounding rectangle, taking into consideration
            '' any possible page transformations (rotation in this case):
            Dim adjusted = page.AdjustCoordinates(rectToRedact)

            '' Note If 'rectToRedact' is used instead of 'adjusted',
            '' the redact will miss the target (customer name/address):
            Dim redact = New RedactAnnotation() With
                    .Rect = adjusted,
                    .OverlayFillColor = Color.Orange,
                    .OverlayText = "Redacted",
                    .page = page
            '' Apply the redact:

            '' Done
            Return doc.Pages.Count
        End Using
    End Function
End Class