Embed sound file in PDF by creating sound action

A Sound action is used to embed and play a sound file in PDF document. In Spire.PDF, we can create a sound action by using the PdfSoundAction class. Attributes like sound, volume and repeat can be specified for the sound action.

Refer below code example:

Step 1: Create a new PDF document and add a page to it.

PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();

Step 2: Create a sound action and set its attributes.

PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
soundAction.Sound.Bits = 15;
soundAction.Sound.Channels = PdfSoundChannels.Stereo;
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
soundAction.Volume = 0.8f;
soundAction.Repeat = true;

Step 3: Set the sound action to be executed when the PDF document is opened.

document.AfterOpenAction = soundAction;

Step 4: Save and close the PDF document.

document.SaveToFile("Output.pdf");
document.Close();

Full code:

using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;

namespace PDF_Sound_Action
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PDF document
            PdfDocument document = new PdfDocument();
            //Add a page
            PdfPageBase page = document.Pages.Add();

            //Create a sound action
            PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
            soundAction.Sound.Bits = 15;
            soundAction.Sound.Channels = PdfSoundChannels.Stereo;
            soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
            soundAction.Volume = 0.8f;
            soundAction.Repeat = true;

            // Set the sound action to be executed when the PDF document is opened
            document.AfterOpenAction = soundAction;

            //Save and close the PDF document
            document.SaveToFile("Output.pdf");
            document.Close();
        }
    }
}