SoundAnnotations.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.  
  12. namespace DsPdfWeb.Demos.Basics
  13. {
  14. // This sample shows how to add sound annotations to a PDF document.
  15. public class SoundAnnotations
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. var doc = new GcPdfDocument();
  20. var page = doc.NewPage();
  21. var noteWidth = 72 * 3;
  22. var gap = 8;
  23. // User names for annotations' authors:
  24. var user1 = "Aiff Ding";
  25. var user2 = "Wav Dong";
  26.  
  27. var rc = Common.Util.AddNote(
  28. "This sample demonstrates adding sound annotations using DsPdf. " +
  29. "The track associated with an annotation can be played in a viewer that supports it. " +
  30. "PDF supports AIFF and WAV tracks in sound annotations.",
  31. page);
  32.  
  33. // AIFF sound annotation:
  34. var ip = new PointF(rc.X, rc.Bottom + gap);
  35. rc = Common.Util.AddNote("A red sound annotation is placed to the right of this note. Double click the icon to play the sound.",
  36. page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
  37. var aiffAnnot = new SoundAnnotation()
  38. {
  39. UserName = user1,
  40. Contents = "Sound annotation with an AIFF track.",
  41. Rect = new RectangleF(rc.Right, rc.Top, 24, 24),
  42. Icon = SoundAnnotationIcon.Speaker,
  43. Color = Color.Red,
  44. Sound = SoundObject.FromFile(Path.Combine("Resources", "Sounds", "ding.aiff"), AudioFormat.Aiff)
  45. };
  46. page.Annotations.Add(aiffAnnot);
  47.  
  48. // WAV sound annotation:
  49. ip = new PointF(rc.X, rc.Bottom + gap);
  50. rc = Common.Util.AddNote("A blue sound annotation is placed to the right of this note. Double click the icon to play the sound.",
  51. page, new RectangleF(ip.X, ip.Y, noteWidth, 100));
  52. var wavAnnot = new SoundAnnotation()
  53. {
  54. UserName = user2,
  55. Contents = "Sound annotation with a WAV track.",
  56. Rect = new RectangleF(rc.Right, rc.Top, 24, 24),
  57. Icon = SoundAnnotationIcon.Mic,
  58. Color = Color.Blue,
  59. Sound = SoundObject.FromFile(Path.Combine("Resources", "Sounds", "dong.wav"), AudioFormat.Wav)
  60. };
  61. page.Annotations.Add(wavAnnot);
  62.  
  63. // Done:
  64. doc.Save(stream);
  65. return doc.Pages.Count;
  66. }
  67. }
  68. }
  69.