StampImage.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Numerics
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Pdf.Annotations
  10. Imports GrapeCity.Documents.Pdf.Graphics
  11. Imports GrapeCity.Documents.Drawing
  12. Imports GCTEXT = GrapeCity.Documents.Text
  13. Imports GCDRAW = GrapeCity.Documents.Drawing
  14.  
  15. '' This sample demonstrates how to add a custom appearance stream
  16. '' to an annotation using a FormXObject.
  17. '' In the sample, an existing PDF is loaded, and then we loop over
  18. '' the document's pages. On each page, a StampAnnotation is created, and a FormXObject
  19. '' which is assigned to the annotation's normal default appearance stream.
  20. '' A semi-transparent PNG image is then drawn on the FormXObject's
  21. '' Graphics using regular GcGraphics features (Transform and DrawImage).
  22. Public Class StampImage
  23. Function CreatePDF(ByVal stream As Stream) As Integer
  24. Dim doc = New GcPdfDocument()
  25.  
  26. '' Load an existing PDF to which we will add a stamp annotation
  27. '' (see LoadPDF for details on loading documents):
  28. Dim jsFile = Path.Combine("Resources", "PDFs", "The-Rich-History-of-JavaScript.pdf")
  29. Using fs = New FileStream(jsFile, FileMode.Open, FileAccess.Read)
  30. doc.Load(fs)
  31. Dim rect = New RectangleF(PointF.Empty, doc.Pages(0).Size)
  32.  
  33. '' Create a FormXObject to use as the stamp appearance:
  34. Dim fxo = New FormXObject(doc, rect)
  35. '' Get an image from the resources, and draw it on the FormXObject's graphics
  36. '' (see Transformations for details on using GcGraphics.Transform):
  37. Dim img = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "draft-copy-450x72.png"))
  38. Dim center = New Vector2(fxo.Bounds.Width / 2, fxo.Bounds.Height / 2)
  39. fxo.Graphics.Transform =
  40. Matrix3x2.CreateRotation((-55 * Math.PI) / 180.0F, center) *
  41. Matrix3x2.CreateScale(6, center)
  42. fxo.Graphics.DrawImage(img, fxo.Bounds, Nothing, ImageAlign.CenterImage)
  43.  
  44. '' Loop over pages, add a stamp to each page:
  45. For Each page In doc.Pages
  46. '' Create a StampAnnotation over the whole page:
  47. Dim stamp = New StampAnnotation() With
  48. {
  49. .Icon = StampAnnotationIcon.Draft.ToString(),
  50. .Name = "draft",
  51. .page = page,
  52. .rect = rect,
  53. .UserName = "Jaime Smith"
  54. }
  55. '' Re-use the same FormXObject on all pages:
  56. stamp.AppearanceStreams.Normal.Default = fxo
  57. Next
  58. ''
  59. doc.Save(stream)
  60. End Using
  61. '' Done:
  62. Return doc.Pages.Count
  63. End Function
  64. End Class
  65.