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