ActionSound1.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using GrapeCity.Documents.Pdf;
  6. using GrapeCity.Documents.Pdf.AcroForms;
  7. using GrapeCity.Documents.Pdf.Actions;
  8. using GrapeCity.Documents.Pdf.Annotations;
  9. using GrapeCity.Documents.Text;
  10. using System.Drawing;
  11. using System.IO;
  12.  
  13. namespace DsPdfWeb.Demos
  14. {
  15. // This sample creates a PDF with some pushbuttons,
  16. // and associates each button's MouseDown event
  17. // with an ActionSound that plays a sound file.
  18. public class ActionSound1
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. string[] soundPaths =
  23. {
  24. Path.Combine("Resources", "Sounds", "ding.aiff"),
  25. Path.Combine("Resources", "Sounds", "dong.wav"),
  26. };
  27. var doc = new GcPdfDocument();
  28. var page = doc.NewPage();
  29.  
  30. var rc = Common.Util.AddNote(
  31. "This sample creates a PDF with some pushbuttons, " +
  32. "and associates each button's MouseDown event " +
  33. "with an ActionSound that plays a sound file.",
  34. page);
  35.  
  36. var resolution = page.Graphics.Resolution;
  37. var g = page.Graphics;
  38. var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
  39. // Prep a simple layout:
  40. var ip = new PointF(resolution, rc.Bottom + resolution / 2);
  41. var fldOffset = resolution * 2.5f;
  42. var fldHeight = tf.FontSize * 1.2f;
  43. var dY = resolution * 0.4f;
  44.  
  45. // Add the buttons with ActionSound on MouseDown events:
  46. foreach (var soundPath in soundPaths)
  47. {
  48. var btn = new PushButtonField();
  49. btn.Widget.Page = page;
  50. btn.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, resolution, fldHeight);
  51. btn.Widget.ButtonAppearance.Caption = Path.GetFileNameWithoutExtension(soundPath);
  52. var sound = new ActionSound(SoundObject.FromFile(soundPath))
  53. {
  54. // Adjust sound properties if/as needed:
  55. Volume = 0.5f,
  56. };
  57. btn.Widget.Events.MouseDown = sound;
  58. btn.Widget.Highlighting = HighlightingMode.Invert;
  59. doc.AcroForm.Fields.Add(btn);
  60. ip.Y += dY;
  61. }
  62.  
  63. // Done:
  64. doc.Save(stream);
  65. return doc.Pages.Count;
  66. }
  67. }
  68. }
  69.