DocAttachments.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.Linq;
  8. using System.Text;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Shows how to attach files to a PDF document.
  16. // In this sample we attach a few photos and two PDFs, including an AcroForm.
  17. // See also the FileAttachments sample that demonstrates file attachment annotations,
  18. // which are files associated with a specific location on a page.
  19. public class DocAttachments
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. var doc = new GcPdfDocument();
  24. var page = doc.NewPage();
  25. var files = new (string, string)[]
  26. {
  27. ( "Images", "tudor.jpg" ),
  28. ( "Images", "sea.jpg" ),
  29. ( "Images", "puffins.jpg" ),
  30. ( "Images", "lavender.jpg" ),
  31. ( "Images", "skye.jpg" ),
  32. ( "Images", "fiord.jpg" ),
  33. ( "Images", "newfoundland.jpg" ),
  34. ( "PDFs", "HelloWorld.pdf" ),
  35. ( "PDFs", "FormFields.pdf" )
  36. };
  37. var sb = new StringBuilder();
  38. foreach (var f in files)
  39. sb.AppendLine(f.Item2);
  40. Common.Util.AddNote(
  41. "Several images and PDFs are attached to this document:\n\n" +
  42. sb.ToString(), page);
  43. foreach (var f in files)
  44. {
  45. var file = Path.Combine("Resources", f.Item1, f.Item2);
  46. var fspec = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, file));
  47. doc.EmbeddedFiles.Add(file, fspec);
  48. }
  49. // Done:
  50. doc.Save(stream);
  51. return doc.Pages.Count;
  52. }
  53. }
  54. }
  55.