FileAttachments.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Pdf.Annotations;
  12. using GrapeCity.Documents.Pdf.Actions;
  13.  
  14. namespace DsPdfWeb.Demos.Basics
  15. {
  16. // This sample demonstrates how to create file attachment annotations on a page.
  17. // See also the DocAttachments sample that demonstrates document level file attachments.
  18. public class FileAttachments
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. var doc = new GcPdfDocument();
  23. var page = doc.NewPage();
  24. var g = page.Graphics;
  25.  
  26. var rc = Common.Util.AddNote(
  27. "Some files from the sample's Resources/Images folder are attached to this page.\n" +
  28. "Some viewers may not show attachments, so we draw rectangles to indicate their (usually clickable) locations.",
  29. page);
  30. var ip = new PointF(rc.X, rc.Bottom + 9);
  31. var attSize = new SizeF(36, 12);
  32. var gap = 8;
  33. var files = new string[]
  34. {
  35. "tudor.jpg",
  36. "sea.jpg",
  37. "puffins.jpg",
  38. "lavender.jpg",
  39. "skye.jpg",
  40. "fiord.jpg",
  41. "newfoundland.jpg"
  42. };
  43. foreach (string fn in files)
  44. {
  45. var file = Path.Combine("Resources", "Images", fn);
  46. var faa = new FileAttachmentAnnotation()
  47. {
  48. Color = Color.FromArgb(unchecked((int)0xFFc540a5)),
  49. UserName = "Jaime Smith",
  50. Rect = new RectangleF(ip.X, ip.Y, attSize.Width, attSize.Height),
  51. Contents = "Attached file: " + file,
  52. Icon = FileAttachmentAnnotationIcon.Paperclip,
  53. File = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, file)),
  54. };
  55. page.Annotations.Add(faa);
  56. g.FillRectangle(faa.Rect, Color.FromArgb(unchecked((int)0xFF40c5a3)));
  57. g.DrawRectangle(faa.Rect, Color.FromArgb(unchecked((int)0xFF6040c5)));
  58. ip.Y += attSize.Height + gap;
  59. }
  60. // Done:
  61. doc.Save(stream);
  62. return doc.Pages.Count;
  63. }
  64. }
  65. }
  66.