Skip to main content Skip to footer

Adding Sounds to a PDF in .NET Core

Adding Sound to a PDF?

With the latest trends in technology, there are many options to add dynamic content to documents to make them interactive. For example, buttons, movies, sound clips, hyperlinks, and bookmarks, all add interactivity to your documents. We are happy to announce that GrapeCity Documents for PDF (GcPdf) now provides a SoundAnnotation (from version 2.1.0.260) class to allow adding sound to a page. The sound that you add to your PDF can come from an existing audio file or from a microphone recording.

Supported Audio Formats

Incorporating audio into a PDF file can add a substantial layer of depth and refine the standard of your document. Adding sound annotations can be solely done to provide comments in a document, describe the contents of the document, or to act as a supplement to a visual interface. The supported sound formats are: .AU, AIFF, and WAV formats. Also, the systems should have a fitting hardware and software for playing audio files.

Adding audio (of a high production quality) to strategically placed sound annotations will result in the generated PDF document having an increased accessibility. This allows the PDF content to become available for all end-users.

Adding Sounds to PDF using GcPdf

The SoundAnnotation Class

SoundAnnotation class is used to place annotation at a specific location on a page, that is represented by a certain visual element, say, a speaker or a microphone. This element is specified using the Icon property of SoundAnnotation. The Sound property is used to set the audio file, using the SoundObject class.

In GcPdf, an instance of a SoundAnnotation class is created and the related properties are set, based on the requirement. This instance is added to the Annotations collection of a page.

Note: The visual element within the PDF should be double-clicked to hear the corresponding audio.

How to Add Audio to a PDF:

Step 1. Load an existing PDF

           GcPdfDocument pdfDocument = new GcPdfDocument();
                        FileStream fs = new FileStream(Path.Combine("Resources", "GcPdf_TestFile.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
            pdfDocument.Load(fs);    

Step 2. Create a SoundAnnotation and add it to the page at the required location

  // WAV sound annotation:
            SoundAnnotation wavAnnot1 = new SoundAnnotation()
            {
                Contents = "Sound annotation with a WAV track reading out the text.",
                Rect = new RectangleF(rect1.Right,rect1.Top, 24, 24),
                Icon = SoundAnnotationIcon.Speaker,
                Color = Color.Red,
                Sound = SoundObject.FromFile(Path.Combine("Resources", "Track_1.wav"), AudioFormat.Wav)
            };
            page.Annotations.Add(wavAnnot1);

Step 3. Save the PDF

  doc.Save(“GcPdf_SoundAnnotation.pdf”);

That's it. Let us know how you used SoundAnnotation in your applications. We would love to hear from you!

Download the sample

Esha Dhir

Associate Software Engineer
comments powered by Disqus