AdjustCoords.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS 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 DsPdf 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"))
            doc.Load(fs)

            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:
            doc.Redact(redact)

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