FileAttachments.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 GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Pdf.Annotations
  9. Imports GrapeCity.Documents.Pdf.Actions
  10. Imports GrapeCity.Documents.Text
  11. Imports GrapeCity.Documents.Drawing
  12.  
  13. '' This sample demonstrates how to create file attachment annotations on a page.
  14. '' See also the DocAttachments sample that demonstrates document level file attachments.
  15. Public Class FileAttachments
  16. Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim doc = New GcPdfDocument()
  18. Dim page = doc.NewPage()
  19. Dim g = page.Graphics
  20.  
  21. Dim rc = Util.AddNote(
  22. "Some files from the sample's Resources/Images folder are attached to this page." + vbLf +
  23. "Some viewers may not show attachments, so we draw rectangles to indicate their (usually clickable) locations.",
  24. page)
  25. Dim ip = New PointF(rc.X, rc.Bottom + 9)
  26. Dim attSize = New SizeF(36, 12)
  27. Dim gap = 8
  28. Dim files As String() = {
  29. "tudor.jpg",
  30. "sea.jpg",
  31. "puffins.jpg",
  32. "lavender.jpg",
  33. "skye.jpg",
  34. "fiord.jpg",
  35. "newfoundland.jpg"
  36. }
  37. For Each fn In files
  38. Dim File = Path.Combine("Resources", "Images", fn)
  39. Dim faa = New FileAttachmentAnnotation() With {
  40. .Color = Color.FromArgb(&HFFC540A5),
  41. .UserName = "Jaime Smith",
  42. .Rect = New RectangleF(ip.X, ip.Y, attSize.Width, attSize.Height),
  43. .Contents = "Attached file: " + File,
  44. .Icon = FileAttachmentAnnotationIcon.Paperclip,
  45. .File = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, File))
  46. }
  47. page.Annotations.Add(faa)
  48. g.FillRectangle(faa.Rect, Color.FromArgb(&HFF40C5A3))
  49. g.DrawRectangle(faa.Rect, Color.FromArgb(&HFF6040C5))
  50. ip.Y += attSize.Height + gap
  51. Next
  52. ''
  53. '' Done:
  54. doc.Save(stream)
  55. Return doc.Pages.Count
  56. End Function
  57. End Class
  58.