ActionSound1.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Pdf.Actions;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Text;
using System.Drawing;
using System.IO;

namespace DsPdfWeb.Demos
{
    // This sample creates a PDF with some pushbuttons, 
    // and associates each button's MouseDown event 
    // with an ActionSound that plays a sound file.
    public class ActionSound1
    {
        public int CreatePDF(Stream stream)
        {
            string[] soundPaths =
            {
                Path.Combine("Resources", "Sounds", "ding.aiff"),
                Path.Combine("Resources", "Sounds", "dong.wav"),
            };
            var doc = new GcPdfDocument();
            var page = doc.NewPage();

            var rc = Common.Util.AddNote(
                "This sample creates a PDF with some pushbuttons, " +
                "and associates each button's MouseDown event " +
                "with an ActionSound that plays a sound file.",
                page);

            var resolution = page.Graphics.Resolution;
            var g = page.Graphics;
            var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
            // Prep a simple layout:
            var ip = new PointF(resolution, rc.Bottom + resolution / 2);
            var fldOffset = resolution * 2.5f;
            var fldHeight = tf.FontSize * 1.2f;
            var dY = resolution * 0.4f;

            // Add the buttons with ActionSound on MouseDown events:
            foreach (var soundPath in soundPaths)
            {
                var btn = new PushButtonField();
                btn.Widget.Page = page;
                btn.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, resolution, fldHeight);
                btn.Widget.ButtonAppearance.Caption = Path.GetFileNameWithoutExtension(soundPath);
                var sound = new ActionSound(SoundObject.FromFile(soundPath))
                {
                    // Adjust sound properties if/as needed:
                    Volume = 0.5f,
                };
                btn.Widget.Events.MouseDown = sound;
                btn.Widget.Highlighting = HighlightingMode.Invert;
                doc.AcroForm.Fields.Add(btn);
                ip.Y += dY;
            }

            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}