ActionSound1.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © 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
{
    // このサンプルは、いくつかのプッシュボタンを含む PDF を作成し、
    // 各ボタンの MouseDown イベントを音声ファイルを再生する ActionSound に関連付けます。
    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(
                "このサンプルは、いくつかのプッシュボタンを含む PDF を作成し、" +
                "各ボタンの MouseDown イベントを音声ファイルを再生する ActionSound に関連付けます。",
                page);

            var resolution = page.Graphics.Resolution;
            var g = page.Graphics;
            var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
            // レイアウト設定
            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;

            // MouseDown イベントで ActionSound が鳴るボタンを追加
            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))
                {
                    // 必要に応じてサウンドのプロパティを調整
                    Volume = 0.5f,
                };
                btn.Widget.Events.MouseDown = sound;
                btn.Widget.Highlighting = HighlightingMode.Invert;
                doc.AcroForm.Fields.Add(btn);
                ip.Y += dY;
            }

            // PDF ドキュメントを保存します。
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}