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

namespace DsPdfWeb.Demos.Basics
{
    // This demo shows how to add video and audio clips to a PDF using RichMediaAnnotation.
    public class RichMedia
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();

            var videoPath = Path.Combine("Resources", "Video", "waterfall.mp4");
            var audioPath0 = Path.Combine("Resources", "Sounds", "city-noises.mp3");
            var audioPath1 = Path.Combine("Resources", "Sounds", "dong.wav");
            const int smallGap = 18;
            const int largeGap = 36;

            // Add a video which plays when the page becomes visible:
            var rc = Util.AddNote("The following video plays automatically when the page becomes visible:", page);
            var rma = new RichMediaAnnotation();
            var videoEfs = EmbeddedFileStream.FromFile(doc, videoPath);
            var videoFileSpec = FileSpecification.FromEmbeddedStream(Path.GetFileName(videoPath), videoEfs);
            rma.SetVideo(videoFileSpec);
            rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
            rma.ActivationCondition = RichMediaAnnotationActivation.PageBecomesVisible;
            rma.DeactivationCondition = RichMediaAnnotationDeactivation.PageBecomesInvisible;
            rma.ShowNavigationPane = true;
            rma.Page = page;
            rma.Rect = new RectangleF(rc.X, rc.Bottom + smallGap, 72 * 5, 72 * 5 * 0.56f);

            // Add the same video clip which plays in a popup window (reuse the FileSpecification):
            rc = Util.AddNote("The same video that plays in a popup window:", page, new RectangleF(rc.X, rma.Rect.Bottom + largeGap, rc.Width, 0));
            rma = new RichMediaAnnotation();
            rma.SetVideo(videoFileSpec);
            rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Windowed;
            rma.ActivationCondition = RichMediaAnnotationActivation.UserAction;
            rma.Page = page;
            rma.Rect = new RectangleF(rc.X, rc.Bottom + smallGap, 144, 144);

            // Add a couple of audio clips:
            rc = Util.AddNote("Two audio clips that play when clicked:", page, new RectangleF(rc.X, rma.Rect.Bottom + largeGap, rc.Width, 0));
            rma = new RichMediaAnnotation();
            var audioEfs0 = EmbeddedFileStream.FromFile(doc, audioPath0);
            var audioFileSpec0 = FileSpecification.FromEmbeddedStream(Path.GetFileName(audioPath0), audioEfs0);
            rma.SetAudio(audioFileSpec0);
            rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
            rma.ActivationCondition = RichMediaAnnotationActivation.UserAction;
            rma.Page = page;
            rma.Rect = new RectangleF(rc.X, rc.Bottom + smallGap, 72, 36);
            //
            rma = new RichMediaAnnotation();
            var audioEfs1 = EmbeddedFileStream.FromFile(doc, audioPath1);
            var audioFileSpec1 = FileSpecification.FromEmbeddedStream(Path.GetFileName(audioPath1), audioEfs1);
            rma.SetAudio(audioFileSpec1);
            rma.PresentationStyle = RichMediaAnnotationPresentationStyle.Embedded;
            rma.ActivationCondition = RichMediaAnnotationActivation.UserAction;
            rma.Page = page;
            rma.Rect = new RectangleF(rc.X + 144, rc.Bottom + smallGap, 72, 36);

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