RichMedia.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.Pdf.Annotations;
  11. using DsPdfWeb.Demos.Common;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // This demo shows how to add video and audio clips to a PDF using RichMediaAnnotation.
  16. public class RichMedia
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var doc = new GcPdfDocument();
  21. var page = doc.NewPage();
  22.  
  23. var videoPath = Path.Combine("Resources", "Video", "waterfall.mp4");
  24. var audioPath0 = Path.Combine("Resources", "Sounds", "city-noises.mp3");
  25. var audioPath1 = Path.Combine("Resources", "Sounds", "dong.wav");
  26. const int smallGap = 18;
  27. const int largeGap = 36;
  28.  
  29. // Add a video which plays when the page becomes visible:
  30. var rc = Util.AddNote("The following video plays automatically when the page becomes visible:", page);
  31. var rma = new RichMediaAnnotation();
  32. var videoEfs = EmbeddedFileStream.FromFile(doc, videoPath);
  33. var videoFileSpec = FileSpecification.FromEmbeddedStream(Path.GetFileName(videoPath), videoEfs);
  34. rma.SetVideo(videoFileSpec);
  35. rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
  36. rma.ActivationCondition = RichMediaAnnotationActivation.PageBecomesVisible;
  37. rma.DeactivationCondition = RichMediaAnnotationDeactivation.PageBecomesInvisible;
  38. rma.ShowNavigationPane = true;
  39. rma.Page = page;
  40. rma.Rect = new RectangleF(rc.X, rc.Bottom + smallGap, 72 * 5, 72 * 5 * 0.56f);
  41.  
  42. // Add the same video clip which plays in a popup window (reuse the FileSpecification):
  43. rc = Util.AddNote("The same video that plays in a popup window:", page, new RectangleF(rc.X, rma.Rect.Bottom + largeGap, rc.Width, 0));
  44. rma = new RichMediaAnnotation();
  45. rma.SetVideo(videoFileSpec);
  46. rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Windowed;
  47. rma.ActivationCondition = RichMediaAnnotationActivation.UserAction;
  48. rma.Page = page;
  49. rma.Rect = new RectangleF(rc.X, rc.Bottom + smallGap, 144, 144);
  50.  
  51. // Add a couple of audio clips:
  52. rc = Util.AddNote("Two audio clips that play when clicked:", page, new RectangleF(rc.X, rma.Rect.Bottom + largeGap, rc.Width, 0));
  53. rma = new RichMediaAnnotation();
  54. var audioEfs0 = EmbeddedFileStream.FromFile(doc, audioPath0);
  55. var audioFileSpec0 = FileSpecification.FromEmbeddedStream(Path.GetFileName(audioPath0), audioEfs0);
  56. rma.SetAudio(audioFileSpec0);
  57. rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
  58. rma.ActivationCondition = RichMediaAnnotationActivation.UserAction;
  59. rma.Page = page;
  60. rma.Rect = new RectangleF(rc.X, rc.Bottom + smallGap, 72, 36);
  61. //
  62. rma = new RichMediaAnnotation();
  63. var audioEfs1 = EmbeddedFileStream.FromFile(doc, audioPath1);
  64. var audioFileSpec1 = FileSpecification.FromEmbeddedStream(Path.GetFileName(audioPath1), audioEfs1);
  65. rma.SetAudio(audioFileSpec1);
  66. rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
  67. rma.ActivationCondition = RichMediaAnnotationActivation.UserAction;
  68. rma.Page = page;
  69. rma.Rect = new RectangleF(rc.X + 144, rc.Bottom + smallGap, 72, 36);
  70.  
  71. // Done:
  72. doc.Save(stream);
  73. return doc.Pages.Count;
  74. }
  75. }
  76. }
  77.