'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
Imports System.IO
Imports System.Drawing
Imports System.Numerics
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Graphics
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This sample demonstrates how to add a custom appearance stream
'' to an annotation using a FormXObject.
'' In the sample, an existing PDF is loaded, and then we loop over
'' the document's pages. On each page, a StampAnnotation is created, and a FormXObject
'' which is assigned to the annotation's normal default appearance stream.
'' A semi-transparent PNG image is then drawn on the FormXObject's
'' Graphics using regular GcGraphics features (Transform and DrawImage).
Public Class StampImage
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()

        '' Load an existing PDF to which we will add a stamp annotation
        '' (see LoadPDF for details on loading documents):
        Dim jsFile = Path.Combine("Resources", "PDFs", "The-Rich-History-of-JavaScript.pdf")
        Using fs = New FileStream(jsFile, FileMode.Open, FileAccess.Read)
            Dim rect = New RectangleF(PointF.Empty, doc.Pages(0).Size)

            '' Create a FormXObject to use as the stamp appearance:
            Dim fxo = New FormXObject(doc, rect)
            '' Get an image from the resources, and draw it on the FormXObject's graphics
            '' (see Transformations for details on using GcGraphics.Transform):
            Dim img = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "draft-copy-450x72.png"))
            Dim center = New Vector2(fxo.Bounds.Width / 2, fxo.Bounds.Height / 2)
            fxo.Graphics.Transform =
                Matrix3x2.CreateRotation((-55 * Math.PI) / 180.0F, center) *
                Matrix3x2.CreateScale(6, center)
            fxo.Graphics.DrawImage(img, fxo.Bounds, Nothing, ImageAlign.CenterImage)

            '' Loop over pages, add a stamp to each page:
            For Each page In doc.Pages
                '' Create a StampAnnotation over the whole page:
                Dim stamp = New StampAnnotation() With
                        .Icon = StampAnnotationIcon.Draft.ToString(),
                        .Name = "draft",
                        .page = page,
                        .rect = rect,
                        .UserName = "Jaime Smith"
                '' Re-use the same FormXObject on all pages:
                stamp.AppearanceStreams.Normal.Default = fxo
        End Using
        '' Done:
        Return doc.Pages.Count
    End Function
End Class